2013-07-08 63 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()