Use this method to successfully print the HTML using as there is a bug that will cause the DocumentCompleted
Event to trigger multiple times. I have got an easy solution for it -
private class yourClassName
{
WebBrowser webBrowserForPrinting;
public void YourForm_Load()
{
webBrowserForPrinting = new webBrowserForPrinting();
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
}
int i = 0;
private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
i++;
if (i == 1)
{
((WebBrowser)sender).ShowPrintPreviewDialog();
}
else
{
((WebBrowser)sender).Dispose();
}
}
}
This method will show you the printing dialog
as well with preview. If you don't want the preview use this one instead -
private class yourClassName
{
WebBrowser webBrowserForPrinting;
public void YourForm_Load()
{
webBrowserForPrinting = new webBrowserForPrinting();
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
}
int i = 0;
private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
i++;
if (i == 1)
{
((WebBrowser)sender).ShowPrintDialog();
}
else
{
((WebBrowser)sender).Dispose();
}
}
}