5

How ca i change my html document orientation(to Landscape) for printing using WebBrowser control.

var browser = new WebBrowser();
browser.DocumentCompleted += (sender, args) =>
    {
        var ws = sender as WebBrowser;
        //change paper size and orientation
        ws.Print();
    };
browser.Navigate(path);

I need to change paper size format to C5 and orientation to Landscape before printing. How can i do this without any dialogs?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Dzmitry Martavoi
  • 6,867
  • 6
  • 38
  • 59

1 Answers1

8

To control HTML printing layout beyond @media CSS with WebBrowser (both WinForms and WPF), you would need to implement your own Internet Explorer Print Template. That would provide full control over headers, margins, columns, etc.

Specifically, you're after TemplatePrinter.orientation. It isn't properly documented, but it works. The source of the standard IE print template can be viewed when navigated to res://ieframe.dll/preview.dlg.

Some other relevant resources:

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486
  • Thanks for this link: https://support.microsoft.com/en-us/kb/267240 You seem to know quite a lot about printing from IE. So do you know -- that link actually contradicts this one: https://support.microsoft.com/en-us/kb/236777 that basically says that there's no way to change footer/header in IE programmatically. So I'm curious, since the first page clearly shows how to do it, is there a way to also: A) Change page margins, and B) Specify printer when using `OLECMDEXECOPT_DONTPROMPTUSER`? – c00000fd Dec 09 '15 at 11:09
  • @c00000fd, A and B are possible not so straightforward. You'd need to roll put your own print template - check the other links, particularly [this](http://marc.durdin.net/2011/07/demystifying-printing-with-the-microsoft-webbrowser-control-and-showhtmldialogex-2). – noseratio Dec 09 '15 at 21:16
  • Oh, man, Delphi. People still use it? Any C/C++ (or at least C#) links in your arsenal? I'm basically looking to allow printing from `WebControl` to allow print customization from within the program (w/o user interface from IE.) And that stuff is so undocumented... – c00000fd Dec 09 '15 at 22:10
  • I don't have any C++ code I could post here. To an extent, this stuff *is* documented: https://msdn.microsoft.com/en-us/library/aa753358(v=vs.85).aspx – noseratio Dec 09 '15 at 22:28
  • The printer settings are stored at registry, so you can edit it as a default value for all global printing. https://www.scriptinghouse.com/2022/05/the-hidden-settings-of-internet-explorer-page-margin-fonts-default-page.html – mjb Feb 25 '23 at 06:51