Archive

Posts Tagged ‘webbrowser’

Print html page in .net windows application

You can use the WebBrowser control to do so. It will allow you to show HTML inside your WinForms.

The DocumentText proprety will allow you to set a String that represent the HTML you want to show.

For example:

webBrowser.DocumentText = "<html><body><p>I like StackOverflow</p><body></html>";

Afterward if you want to print the page, you’ll have to wait until the Document is completed and call the Print method of the WebBrowser. MSDN shows an easy way to do 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.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}