We have a program that we are using to 'print' and 'print preview' an html document that is being displayed within a C# program. We open a new webbrowser adding info using html statements and then add the contents of the original webbrowser to our new webbrowser. When we call wbMailBody1.Print(); or wbMailBody1.ShowPrintPreviewDialog(); we get an empty page. If we add the line
MessageBox.Show("");
immediately before calling wbMailBody1.Print(); or wbMailBody1.ShowPrintPreviewDialog(); it will work perfectly
the following code displays an empty page. if you check wbMailBody1.DocumentText.Length it will be 14 even if it should be the length of say 4 or 5 pages in length. If you look at wbMailBody1.DocumentText it will contain even if the string that is being assigned to wbMailBody1.DocumentText is 4 or 5 pages in length.
the GetHTML() routine uses stringBuilder to create a string using sb.Append statements.
private void PrintMessagePreview1()
{
GetHeading();
if (viewMode == Dice.BodyViewMode.HTML)
{
wbMailBody1.DocumentText = GetHTML();
wbMailBody1.ShowPrintPreviewDialog();
}
else
{
printPreviewDialog1.ShowDialog();
}
}
If you make the following change to the code (MessageBox.Show statement) then all works just the way it should.
private void PrintMessagePreview1()
{
GetHeading();
if (viewMode == Dice.BodyViewMode.HTML)
{
wbMailBody1.DocumentText = GetHTML();
MessageBox.Show("");
wbMailBody1.ShowPrintPreviewDialog();
}
else
{
printPreviewDialog1.ShowDialog();
}
}
Loading
Satish KathiPosted Jul 9, 2008, 12:20 PM
Use the print and print preview methods in Web Browser’s Document completed event.
In your first scenario (without message box) you are trying to print without loading the document, browser will take time to load the document.
In second scenario because of your Message box Browser is getting time to load the document hence it is printing.
To resolve this use print/print preview in DocumentCompleted event
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Print();
}