2011-10-06 223 views
3

我想在C#中使用PrintDocument打印文件。該文件是簡單的HTML(我需要它,因爲我需要文件中的文本位於頁面內的特定位置)。如何在C#中打印HTML#

我的問題是,如何打印文件,以便它不會打印HTML本身(標籤等),但它會顯示在Web瀏覽器中的HTML?

+0

你在做的WinForms/WPF? – sll

+0

uhhhhhhhhhhhhhhhhhhhhhhhh。我的蜘蛛感覺很刺痛。將用戶默認瀏覽器(或便攜式Firefox)啓動一個網頁是一個選項?否則,你使用IE強制使用。我還用過一次IE控件。我曾瀏覽過一個衆所周知的網站,並且那天恰好有惡意軟件廣告(http://www.google.com/safebrowsing/diagnostic?site=thesite.com在13個網頁中說只有一個惡意軟件是非常罕見的),並且不幸的是,自IE6安裝以來,我得到了病毒,我真的很煩。 – 2011-10-06 16:03:29

+0

窗體窗體,但它不是metter – MoShe

回答

9

使用web browser control並調用打印方法就可以了,像這樣:

private void PrintHelpPage() 
{ 
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser(); 

    // Add an event handler that prints the document after it loads. 
    webBrowserForPrinting.DocumentCompleted += 
     new WebBrowserDocumentCompletedEventHandler(PrintDocument); 

    // Set the Url property to load the document. 
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html"); 
} 

private void PrintDocument(object sender, 
    WebBrowserDocumentCompletedEventArgs e) 
{ 
    // Print the document now that it is fully loaded. 
    ((WebBrowser)sender).Print(); 

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose(); 
} 

MSDN Article on doing this

+0

此事件是強制性的。 – pbies

+0

但是當我們需要打印到網絡打印機時,我們會做什麼?如果我打電話給瀏覽器打印方法,它不會選擇我需要的任意打印機。 –