4

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?

agnieszka
  • 14,897
  • 30
  • 95
  • 113

1 Answers1

7

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();
}
RuudKok
  • 5,252
  • 2
  • 26
  • 27
  • how can i use WebBrowser class in asp.net projects? – themhz Dec 14 '11 at 16:01
  • 2
    I'm not really sure what you mean themhz? The WebBrowser class is used to display web-pages in a WinForms application. I assume you have a ASP.NET website (WebForms) and you want to print a page? You should use JavaScript for that e.g.: [window.print()](https://developer.mozilla.org/en/DOM:window.print) – RuudKok Dec 16 '11 at 07:56
  • 1
    I forgot to add example code: Print this page! – RuudKok Dec 16 '11 at 08:06
  • 1
    @RuudKok any idea if you can set the printer name and settings, instead of just printing with the default printer? – A Khudairy Feb 12 '15 at 14:48
  • Could I ask how to print directly to a specific network printer without `printer dialog` or `save As` – Anyname Donotcare Jul 26 '19 at 15:35