Asked  7 Days ago    Answers:  5   Viewed   10 times
System.Windows.Forms.WebBrowser wb = new System.Windows.Forms.WebBrowser();

wb.DocumentStream = new FileStream("C:a.html", FileMode.Open, FileAccess.Read);
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
    Application.DoEvents();
}
wb.Print();

I know how to set the page orientation from a PrinterDocument object, but not from a WebBrowser object. Any way to do this? Thanks!

 Answers

6

First, I recommend you to use async event model:

wb.DocumentCompleted += wb_DocumentCompleted;

private void wb_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).Print();
}

To print (add the reference to Microsoft.mshtml.dll):

mshtml.IHTMLDocument2 doc = wb.Document.DomDocument as mshtml.IHTMLDocument2;
doc.execCommand("print", showUI, templatePath);

See IHTMLDocument2.execCommand, MSDN forum question and follow links.

Saturday, May 14, 2022
 
4

Well i have tried so many things but at the end i found that it is not possible to program the printer setting from the code easily. but i could do the margin by the answer of @jeremy. And i found out that For printing from WebBrowser control it uses internet explorer all we know but at the beginning it was using explorer 7 and i had to change it to explorer 11 as default. Then i saw it explorer does not have his own print settings. it uses the default printers settings. So you have to change the Default printers previews.You will see the preview will show that way.

Wednesday, August 4, 2021
 
5

Subscribe to the WebBrowser.Navigating event. Check the WebBrowserNavigatingEventArgs.Url property and set WebBrowserNavigatingEventArgs.Cancel=true if it's not wikipedia.

Monday, October 4, 2021
 
Sufi
 
4

For IE in standards mode (with a doctype, as you say) scrollTop is a property of the <html> element, not the <body>:

HtmlDocument htmlDoc = this.webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;

(A nicer way to get to the <html> element would be good, if anyone knows of one.)

Friday, October 15, 2021
 
Optimus
 
5

You are violating a hard requirement for an STA thread, it must pump a message loop. You patched your way out of trouble with the Navigate method by calling Application.DoEvents(). That pumps the message loop. But you are not doing this for InvokeClick.

Check this answer for the solution. Put the code in the DocumentCompleted event. There is no obvious way I see to decide when to stop the thread, you'd have to poll for some kind of side-effect of the click with a timer perhaps.

Friday, December 3, 2021
 
Rob N
 
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share