2013-07-08 61 views
8

所以我目前正在尝试打印一个PDF,我已经加载到一个Cocoa应用程序中的WebView,它的大小在如果你想看到你拥有的所有东西滚动。问题是,每当它打印,只打印什么是目前显示在网页视图,而不是网页的全部,用这样的代码:打印Cocoa中的WebView的全部内容,不只是显示

[[[[WebView mainFrame] frameView] documentView] print:sender]; 

我不知道如果我只是想打印它的错误部分,或者只是需要以另一种方式来解决这个问题,并且非常感谢一些帮助。

+0

尝试printDocumentView –

+0

作为'[[[WebView中的主机] frameView] printDocumentView]'?没有做任何事情,甚至没有发生错误。我还应该提到,我已经确定该问题只影响pdf并将在上面进行编辑。 – Zoz

回答

14

我发现这是一个老问题,但由于目前还没有任何答案,我以为我会为任何人提供这个搜索。

NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo]; 

// This is your chance to modify printInfo if you need to change 
// the page orientation, margins, etc 
[printInfo setOrientation:NSLandscapeOrientation]; 

NSPrintOperation *printOperation = [yourWebView.mainFrame.frameView 
            printOperationWithPrintInfo:printInfo]; 

// Open the print dialog 
[printOperation runOperation]; 

// If you want your print window to appear as a sheet instead of a popup, 
// use this method instead of -[runOperation] 
[printOperation runOperationModalForWindow:yourWindow 
            delegate:self 
          didRunSelector:@selector(printDidRun) 
           contextInfo:nil]; 
+2

我不得不使用'NSPaperOrientationLandscape'而不是'NSLandscapeOrientation' – Todd

+2

我发现要获得完整的HTML,需要打印documentView:yourWebView.mainFrame.frameView.documentView – SarahR

0

以下雨燕代码工作对我来说:

NSPrintOperation(view: webView.mainFrame.frameView.documentView).runOperation()