2012-07-03 25 views
1

我在WebBrowser控件的打印功能中遇到問題。使用Windows.Forms.WebBrowser控件打印時頁面被剪切

首先我加載我的網頁,它呈現正確。 然後我設置頁眉/頁腳/頁邊距和正確的打印機: webbrowser printing; How do I programatically change printer settings with the WebBrowser control?

,到目前爲止的作品。然後我用myBrowser.Print();

但我的網站不打印權。只打印左上角,幾釐米,然後是滾動條。

我印我的網站與IE9,一切都是正常的。還嘗試了不同的瀏覽器模式和文檔模式。沒問題。 ,我想控制和IE瀏覽器在技術上是一樣的...

有沒有我都忘記了任何參數?

的網站,我想打印是舊的,沒有DOCTYPE。但是由於控制顯示它正確,我希望它也能正確打印。

編輯:

發現它具有與網站,不提供打印運行的JavaScript做。 有沒有辦法從操縱DOM獲取HTML?

+0

你有沒有考慮使用HTML到PDF工具,然後打印的PDF?它們比Windows窗體中的瀏覽器控件略微不那麼抽象。另外,歡迎來到堆棧溢出:) – linkerro

+0

這就是我正在開發的......問題:我們的網站是用xsl/xslt生成的,只有IE呈現這個權利(我們正在...)。所以我們必須使用這個瀏覽器控件。我現在如何解決這個問題? – Hannes

+0

如果您修復了問題,您可以自己填寫答案,並將其標記爲接受的修復程序。如果沒有,只需刪除它。 – linkerro

回答

2

對於打印功能,不處理外部文檔。網站的所有文件必須合併到一個可打印的網站中。要獲得其他文檔,必須將myBrowser.Document.DomDocument轉換爲IHTMLDocument2。從該IHTMLDocument2中,您可以提取CSS或JS將其放入html中。

例如:

void myBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     myBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentCompleted); 
     myBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentPrintable); 
     String mySource = myBrowser.DocumentText; 

     // Get the CSS 
     IHTMLDocument2 doc = (myBrowser.Document.DomDocument) as IHTMLDocument2; 
     myCSS = doc.styleSheets.item(0).cssText; 
     mySource = mySource.Replace("<link rel=\"stylesheet\" type=\"text/css\" href=\"/css/style.css\">", "<style type=\"text/css\">"+myCSS+"</style>"); 

     // Reload 
     myBrowser.DocumentText = mySource; 
    } 

    void myBrowser_DocumentPrintable(object sender, WebBrowserDocumentCompletedEventArgs e) 
    { 
     myBrowser.DocumentCompleted -= new WebBrowserDocumentCompletedEventHandler(myBrowser_DocumentPrintable); 
     myBrowser.Print(); 
    }