打印离屏 PDF 视图

4 pdf cocoa objective-c pdfkit

我有一种情况,我想打印多页 PDF。虽然我可以使用 PDFKit 实用程序类和/或石英函数来获取信息来为 NSView 子类手动编写绘图/分页代码,但我认为更快的替代方法是创建一个离屏 PDFView 并告诉它打印自己. 当我尝试这个解决方案时,打印对话框没有消失,打印对话框右半部分的所有打印设置控件都消失了,应用程序也冻结了。

然后我使用以下方法编写了一个小型测试应用程序来说明问题。在未定义 USE_PDF_VIEW 预处理器宏的情况下编译测试程序时,空白视图显示正常。如果定义了 USE_PDF_VIEW,则文档不会打印,大多数打印对话框控件都会消失,并且应用程序会冻结。虽然我有其他方法来实现我的目标,但我很好奇为什么这个快捷方式不起作用。有什么关于可可画的我还是不明白吗?我是否在幕后闯入 Apple Voodoo Magic(tm) 使 PDFView 的行为方式与其他 NSViews 完全不同?

- (void)printMyStuff:(id)sender {

NSPrintInfo *currInfo = [NSPrintInfo sharedPrintInfo];

#ifdef USE_PDF_VIEW


    PDFView *pdfView = [[PDFView alloc] init];
    PDFDocument *pdfDoc = [[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath:@"/Users/wls/Documents/my_document.pdf"]];
    [pdfView setDocument: pdfDoc];
    [pdfView printWithInfo:currInfo autoRotate:YES];


#else

    NSView *myView = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 500, 500)];
    NSPrintOperation *myop = [NSPrintOperation printOperationWithView:myView printInfo:currInfo];
    [myop runOperation];


#endif

}
Run Code Online (Sandbox Code Playgroud)

ale*_*x-i 5

有完全相同的问题。在PDFView需要被添加到一个NSWindow为了printWithInfo:autoRotate:工作(ATLEAST在我的情况),否则打印控件变成空白页或将无法正常工作。

这是完整的代码:

    PDFView *vDoc = [[PDFView alloc] init];
    [vDoc setDocument:pdfDoc];
    [vDoc setAutoScales: YES];
    [vDoc setDisplaysPageBreaks: NO];
    NSWindow *wnd = [[NSWindow alloc] init];
    [wnd setContentSize:vDoc.frame.size];
    [wnd setContentView:vDoc];
    [vDoc printWithInfo:printInfo autoRotate:YES];
    [wnd release];
    [vDoc release];
Run Code Online (Sandbox Code Playgroud)