How to: Print with a WebBrowser Control

How to: Print with a WebBrowser Control


The following code example demonstrates how use the WebBrowser control to print a Web page without displaying it.

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.

    // Dispose the WebBrowser now that the task is complete. 

Compiling the Code

This example requires:

  • References to the System and System.Windows.Forms assemblies.

© 2015 Microsoft