如何在vb.net windows应用程序中打印html页面

Ali*_*ssa 2 .net html c# vb.net winforms

我正在使用vb.net开发一个Windows应用程序,我有一个带有占位符的简单html页面,我将页面加载到流阅读器中,替换占位符然后我需要打印html内容,任何人都有任何想法如何将html内容打印为html而不是源代码.vb.bet或c#中的PS代码都可以.谢谢

Ale*_*dre 6

您可以使用该WebBrowser控件执行此操作.它将允许您在您的内部显示HTML WinForms.

DocumentText proprety允许您设置表示要显示的HTML的String.

例如:

webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";
Run Code Online (Sandbox Code Playgroud)

之后如果要打印页面,则必须等到文档完成后再调用该Print方法WebBrowser.MSDN显示了一种简单的方法:

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)

您还应该考虑尝试使用该方法PrintDialog来确保问题不是您的打印配置.

以下是MSDN的链接: 在MSDN上使用WebBrowser控件进行打印

可能重复: 打印WebBrowser控件内容