打印WebBrowser控件内容

agn*_*zka 3 .net c# printing

我非常喜欢用.NET打印.我想打印一个WebBrowser控件中显示的页面.我怎么做?

Ruu*_*Kok 6

MSDN有一篇关于此的文章,但是他们的代码示例演示了如何使用WebBrowser控件打印网页而不显示它.:

如何:使用WebBrowser控件进行打印

c#代码:

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)

  • 我不太确定你的意思是什么吗?WebBrowser类用于在WinForms应用程序中显示Web页面.我假设你有一个ASP.NET网站(WebForms),你想打印一个页面?您应该使用JavaScript,例如:[window.print()](https://developer.mozilla.org/en/DOM:window.print) (2认同)