gpt4 book ai didi

objective-c - 打印离屏 PDFView

转载 作者:太空狗 更新时间:2023-10-30 04:01:47 30 4
gpt4 key购买 nike

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

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

- (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

}

最佳答案

遇到了完全相同的问题。PDFView 需要添加到 NSWindow 以便 printWithInfo:autoRotate: 工作(至少在我的例子中),否则打印控件变成空白或不起作用。

完整代码如下:

    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];

关于objective-c - 打印离屏 PDFView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2058501/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com