如何在C#中打印html

MoS*_*She 5 html c# printing printdocument

我想在C#中使用PrintDocument打印文件.该文件是简单的HTML(我需要它,因为我需要文件中的文本位于页面中的特定位置.)

我的问题是,如何打印文件以便它不会打印HTML本身(标签等),而是打印在Web浏览器中显示的HTML?

Joh*_*nFx 11

使用Web浏览器控件并在其上调用print方法,如下所示:

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();
}
Run Code Online (Sandbox Code Playgroud)

关于这样做的MSDN文章