2011-10-06 225 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

但是当我们需要打印到网络打印机时,我们会做什么?如果我打电话给浏览器打印方法,它不会选择我需要的任意打印机。 –