0

我有一个要打印的自定义 NSView。在使用 NSView 和打印选项进行设置后,我进行了以下调用:

NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView: printView printInfo: printInfo];

[NSPrintOperation setCurrentOperation: printOperation];
[printView beginDocument];

NSGraphicsContext* theContext = printOperation.context;

“theContext”始终为零。如果我忽略这一点,当我拨打这个电话时:

[printView beginPageInRect: rect atPlacement: location];

我得到一个例外,说:“[General] lockFocus/unlockFocus 发送到不在窗口中的视图”

如果我将其注释掉,我会收到大约十亿条消息:“CGContextDrawPath:无效上下文 0x0。如果您想查看回溯,请设置 CG_CONTEXT_SHOW_BACKTRACE 环境变量。” 打开回溯只会显示我所有的绘图都是导致它的原因。

如果我查看视图的“DrawRect:”函数中的图形上下文:

NSGraphicsContext *graphicsContext = [NSGraphicsContext currentContext];
CGContextRef      context = [[NSGraphicsContext currentContext] graphicsPort];

graphicsContext 和 context 都为零。

那么,我该怎么做才能获得有效的打印上下文?我看到有一个 NSPrintOperation 方法 createContext,但文档说不要直接调用它,如果我忽略它,它没有帮助,并且向打印机拍摄大约八个空作业。

最新版本的代码,仍然导致空上下文:

NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView: printView printInfo: printInfo];

[printView setCurrentForm: [formSet objectAtIndex: 0]];

NSInteger pageCounter = 0;
formHeight = 0;
formWidth = 0;

for (AFVForm *oneForm in formSet)
{
    printView.verticalOffset = formHeight;
    NSRect  rect = NSMakeRect(0, 0, oneForm.pageWidth, oneForm.pageHeight);
    NSPoint location = [printView locationOfPrintRect: rect];

    formHeight += [oneForm pageHeight];
    if ([oneForm pageWidth] > formWidth)
        formWidth = [oneForm pageWidth];
    pageCounter++;
    printView.currentForm = oneForm;
    [printView setPrintMode: YES];

    [printView drawRect: NSZeroRect];

    [printView setPrintMode: NO];
}

[printOperation setShowsPrintPanel:YES];
[printOperation runOperationModalForWindow: [self window] delegate: nil didRunSelector: nil contextInfo: nil];
4

2 回答 2

0

beginDocumentbeginPageInRect:atPlacement:在打印会话开始和每页开始时调用。如果需要,请覆盖这些方法,但不要调用它们。不要调用setCurrentOperation,只需创建一个NSPrintOperation并调用runOperationor runOperationModalForWindow:delegate:didRunSelector:contextInfo:

请参阅Mac 打印编程指南

编辑,例如:

打印视图.h

@interface PrintView : NSView

@property (strong) NSArray *forms;

@end

打印视图.m

@interface PrintView ()

@property (weak) NSTextField *titleField;
@property (weak) NSDictionary *currentForm;

@end


@implementation PrintView

- (instancetype)initWithFrame:(NSRect)frameRect {
    if (self = [super initWithFrame:frameRect]) {
        // add a title text field
        NSTextField *textField = [[NSTextField alloc] initWithFrame:NSMakeRect(25.0, 225.0, 250.0, 25.0)];
        textField.alignment = NSTextAlignmentCenter;
        [self addSubview:textField];
        self.titleField = textField;
    }
    return self;
}

- (BOOL)knowsPageRange:(NSRangePointer)range {
    range->location = 1;
    range->length = self.forms.count;
    return YES;
}

- (NSRect)rectForPage:(NSInteger)page {
    return self.bounds;
}

- (void)beginPageInRect:(NSRect)rect atPlacement:(NSPoint)location {
    [super beginPageInRect:rect atPlacement:location];
    NSPrintOperation *printOperation = [NSPrintOperation currentOperation];
    self.currentForm = self.forms[printOperation.currentPage - 1];
    // set the title
    [self.titleField setStringValue:
        [NSString stringWithFormat:@"Form ‘%@’ page %li",
            self.currentForm[@"title"], (long)printOperation.currentPage]];
}

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];
    // Drawing code here.
    // draw a colored frame
    NSColor *formColor = self.currentForm[@"color"];
    NSRect rect = self.bounds;
    NSInsetRect(rect, 20.0, 20.0);
    [formColor set];
    NSFrameRect(rect);
}

@end

别的地方

- (IBAction)printAction:(id)sender {
    PrintView *printView = [[PrintView alloc] initWithFrame:NSMakeRect(0.0, 0.0, 300.0, 300.0)];
    printView.forms = @[
            @{@"title":@"Form A", @"color":[NSColor redColor]},
            @{@"title":@"Form B", @"color":[NSColor greenColor]},
            @{@"title":@"Form C", @"color":[NSColor blueColor]},
        ];
    NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:printView];
    [printOperation setShowsPrintPanel:YES];
    [printOperation runOperationModalForWindow:[self window] delegate:nil didRunSelector:NULL contextInfo:NULL];
}
于 2019-03-15T22:17:46.300 回答
-1

一个 Swift 示例,位于您的 NSView 子类中。如果没有帮助,我深表歉意:

         func letsPrint() {
             let pInfo = NSPrintInfo.shared
             let d = pInfo.dictionary()
             d["NSLastPage"] = 1
             super.printView(self)
         }
         override func knowsPageRange(_ range: NSRangePointer) -> Bool {
             return true
         }
         override func rectForPage(_ page: Int) -> NSRect {
             if page > 1 { return NSZeroRect }
             return NSRect(x: 0, y: 0, width: 612, height: 792)
         }

此示例打印 1 页,8.5 x 11,因此 rectForPage 中的常量

LetPrint 连接到应用程序菜单的第一响应者。

于 2019-03-16T00:49:06.380 回答