gpt4 book ai didi

macos - Cocoa osx PDFView NSPrintOperation PrintPanel 不显示页面预览

转载 作者:行者123 更新时间:2023-12-03 16:19:24 32 4
gpt4 key购买 nike

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

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

这是我的问题的一个例子:

1- 打印面板显示空白页面预览。接下来我按显示详细信息。

enter image description here

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

enter image description here

这是我的代码:

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

最佳答案

来自this链接:

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

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

编辑:您还可以看到this答案类似,但代码行数较少。

关于macos - Cocoa osx PDFView NSPrintOperation PrintPanel 不显示页面预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080344/

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