Cocoa osx PDFView NSPrintOperation PrintPanel不显示页面预览 [英] Cocoa osx PDFView NSPrintOperation PrintPanel not showing page preview

查看:977
本文介绍了Cocoa osx PDFView NSPrintOperation PrintPanel不显示页面预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序为Mac我有一个webview,显示一些html内容。我从该webview创建一个PDFDocument,然后我想打印该文档。所以我从文档创建一个PDFView,然后我调用NSPrintOperation printOperationWithView。显示打印面板时,除了页面预览显示为空白外,所有内容都正确显示,但如果按下详细信息按钮,面板将被刷新,并且页面预览会正确显示。

In my app for Mac I have a webview that shows some html content. I create a PDFDocument from that webview and then I want to print that document. So I create a PDFView from the document and then I call the NSPrintOperation printOperationWithView. When showing the print panel all appears correct except the page preview which appears blank, but if I press the details button the panel is refreshed and the page preview appears correctly.

我如何解决这个问题?
需要帮助。提前感谢。

How can I solve this?? Need help please. Thanks in advance.

这是我遇到的问题的示例:

This is an example of my problem:

1-打印面板显示空白页预览。

1- The print panel shows with a blank page preview. Next I press show details.

2-刷新面板后页面预览正确显示。

2- After refreshing the panel the page preview appears correctly.

这是我的代码:

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo setTopMargin:0.0];
[printInfo setBottomMargin:0.0];
[printInfo setLeftMargin:0.0];
[printInfo setRightMargin:0.0];
[printInfo setHorizontalPagination:NSFitPagination];
[printInfo setVerticalPagination:NSAutoPagination];
[printInfo setVerticallyCentered:NO];
[printInfo setHorizontallyCentered:YES];

NSData *pdfFinal = [[[[webView mainFrame] frameView] documentView] dataWithPDFInsideRect:[[[webView mainFrame] frameView] documentView].frame];

PDFDocument *doc = [[PDFDocument alloc] initWithData:pdfFinal];
PDFView *pdfView = [[PDFView alloc] init];
[pdfView setDocument:doc];

NSPrintOperation *op;
op = [NSPrintOperation printOperationWithView:pdfView.documentView printInfo:printInfo];

[op setShowsProgressPanel:YES];
[op setShowsPrintPanel:YES];
[op runOperation];


推荐答案

链接:

PDFDocument *doc = ...;

// Invoke private method.
// NOTE: Use NSInvocation because one argument is a BOOL type. Alternately, you could declare the method in a category and just call it.
BOOL autoRotate = NO; // Set accordingly.
NSMethodSignature *signature = [PDFDocument instanceMethodSignatureForSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(getPrintOperationForPrintInfo:autoRotate:)];
[invocation setArgument:&printInfo atIndex:2];
[invocation setArgument:&autoRotate atIndex:3];
[invocation invokeWithTarget:doc];

// Grab the returned print operation.
void *result;
[invocation getReturnValue:&result];

NSPrintOperation *op = (__bridge NSPrintOperation *)result;
[op setShowsPrintPanel:YES];
[op setShowsProgressPanel:YES];
[op runOperation];

这适用于从10.4到10.10(Yosemite)的OSX。

This works on OSX from 10.4 to 10.10 (Yosemite).

编辑:您还可以查看类似的答案,但具有较少的代码行。

You can also see this answer which is similar, but with less lines of code.

这篇关于Cocoa osx PDFView NSPrintOperation PrintPanel不显示页面预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆