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?
Asked
Active
Viewed 9,168 times
1 Answers
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
-
2I'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
-
1I 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