1

I'm using a webbrowser control in c# to print an html file. When I open the html file with my current navigator (Firefox or chrome), my page is "great", and I have to print it exactlly how I see it. But when I use a webbrowser control in c#, it's like the webbrowser add a "margin-left" property, and my document isn't centred https://i.stack.imgur.com/OTd1a.jpg I also want to remove this "page 1 of 1" and the Title. I know that the webbrowser control in c# use Internet explorer, that's why I added this in my html file:

http-equiv="X-UA-Compatible" content="IE=edge

But this doesn't solve the problem.

Keenegan
  • 91
  • 7

2 Answers2

1

This MSKB article should help with headers/footers. Regarding the rendering issues, I would expect FEATURE_BROWSER_EMULATION to have solved those, but I've already suggested that in the comments to another question of yours. Have you verified that browser emulation actually works? If it does, the following should show CSS1Compact for compatMode, and 10 or 9 for documentMode (depending on your IE version). The document must have both <!DOCTYPE html> and <meta http-equiv="X-UA-Compatible" content="IE=edge"> declarations for this to work:

dynamic document = this.webBrowser.Document.DomDocument;
dynamic navigator = document.parentWindow.navigator;
var info =
    "\n navigator.userAgent: " + navigator.userAgent +
    "\n navigator.appName: " + navigator.appName +
    "\n document.documentMode: " + document.documentMode +
    "\n document.compatMode: " + document.compatMode;
MessageBox.Show(info);

[EDITED] To control printing layout beyond @media CSS, you could implement your own Internet Explorer Print Template. That would provide full control over headers, margins, columns, etc. This feature is flexible, but not trival to implement. Some relevant resources:

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Visual studio return me a NullPointerException at the first line :/ – Keenegan Sep 30 '13 at 12:28
  • Run this code upon [DocumentCompleted](http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documentcompleted.aspx) event, when `webBrowser.Document` does exists. – noseratio Sep 30 '13 at 12:31
  • Interesting. So, the document is in standard mode, but printed view is still screwed up compared to how it looks on the screen? – noseratio Sep 30 '13 at 12:42
  • Yeah, maybe it's the printer ? – Keenegan Sep 30 '13 at 12:46
  • If Print Preview (can be invoked via right-click) looks good, then it's printer specific. Anyhow, that's as much as I can help with this issue. – noseratio Sep 30 '13 at 13:17
  • Right click the webbrowser? Or you mean using webbrowser.ShowprintDialog? – Keenegan Sep 30 '13 at 13:24
  • 1
    I meant when that specific page is loaded inside `WebBrowser` control in your own app, do right click on it and select "Print preview". – noseratio Sep 30 '13 at 13:28
  • Yeah, when I use print preview, the preview is like it's printed, but I can change "resize" the margin. – Keenegan Sep 30 '13 at 13:35
  • I've added some info on how to control margins to the answer. – noseratio Sep 30 '13 at 13:53
1

In fact the problem was with my Internet Explorer options. In fact, in the settings of the print preview, I had 50px of margin, I just had to remove it. I wasted so much time on this x/. I don't understand why I had those weirds settings, I never used IE on that computer. Well, thanks Noseratio.

Keenegan
  • 91
  • 7