Question

I'm absolutely new to printing in .NET. I would like to print a page that is displayed in WebBrowser control. How do I do that?

Was it helpful?

Solution

MSDN has an article about this, however their code example demonstrates how use the WebBrowser control to print a Web page without displaying it. :

How to: Print with a WebBrowser Control

The c# code:

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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top