如何在C#中打印HTML

6
我想在C#中使用PrintDocument打印文件。该文件是简单的HTML(我需要它,因为我需要文件中的文本位于页面内的特定位置)。
我的问题是,如何打印文件,以便不打印HTML本身(标签等),而是像在Web浏览器中显示的HTML一样?

我的蜘蛛感觉到了不妙。使用用户默认浏览器(或便携式Firefox)启动网页是否是一个选项?否则,你只能使用IE。我曾经使用过IE控件。有一次,我让它浏览一个知名网站,但那天它恰好遇到了恶意广告(http://www.google.com/safebrowsing/diagnostic?site=thesite.com显示,在13k个页面中只有一个带有恶意软件,所以这种情况非常罕见),不幸的是,由于安装了IE6,我感染了病毒,非常恼火。 - user34537
Windows表单,但这不重要。 - MoShe
2个回答

11

使用web浏览器控件,并像下面这样调用其打印方法:

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上关于如何做到这一点的文章


但是当我们需要打印到网络打印机时该怎么办呢?如果我调用浏览器的打印方法,它不会选择我需要的任意打印机。 - Katastic Voyage

0
使用此方法可以成功地打印HTML,因为存在一个错误会导致DocumentCompleted事件触发多次。我已经有了一个简单的解决方案 -
private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintPreviewDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}

这个方法将会展示给你一个带有预览的打印对话框。如果你不想要预览,可以使用这个方法代替 -

private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接