Webbrowser Print Settings

WebBrowser print settings

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:

  • Beyond Print Preview: Print Customization for Internet Explorer 5.5
  • Print Preview 2: The Continuing Adventures of Internet Explorer 5.5 Print Customization
  • Print Templates, Part I
  • View templates for HTML source documents
  • Demystifying printing with the Microsoft WebBrowser control and ShowHTMLDialogEx
  • Add support to print & preview HTML in a dialog-based MFC app
  • IDM_PRINT
  • IDM_PRINTPREVIEW
  • MSKB: How to print custom headers and footers for a WebBrowser control in Internet Explorer

How do I programmatically change printer settings with the WebBrowser control?

The only method I've had success with is modifying the registry on the fly (and changing them back to not affect anything else).

You can find the settings you need at "Software\Microsoft\Internet Explorer\PageSetup" under CurrentUser.

To change the printer, you can use this:

using System.Management

public static bool SetDefaultPrinter(string defaultPrinter)
{
using (ManagementObjectSearcher objectSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer"))
{
using (ManagementObjectCollection objectCollection = objectSearcher.Get())
{
foreach (ManagementObject mo in objectCollection)
{
if (string.Compare(mo["Name"].ToString(), defaultPrinter, true) == 0)
{
mo.InvokeMethod("SetDefaultPrinter", null, null);
return true;
}
}
}
}
return false;
}


As for the number of copies, you can always put the WebBrowser.Print in a while loop.

How to set Paper Size and Margins printing from a web browser control

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.

How can I print the contents of a WebBrowser control?

MSDN page about printing windows forms web browser control:
http://msdn.microsoft.com/en-us/library/b0wes9a3(v=vs.90).aspx

private void PrintHelpPage()
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();

// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);

// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();

// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}

Printing From WebBrowser control prints to wrong printer after setting default

I created a form with nothing on it. The action of launching/closing this form made it go to the right printer... I have no idea why that works, but there must be some function that could be called to simulate whatever action is taking place there.

How to print background image and styles in WebBrowser control

There is a Print Background Colors and Images setting in Page Setup dialog which is shared between Web Browser Control and Internet Explorer.

Page setup settings for Microsoft Internet Explorer are stored in the following registry key:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\PageSetup

Print Background Colors and Images value is stored in Print_Background key which can be yes or no. You can change the setting using code, but just keep in mind, these values are system-wide settings and will affect all instances of the WebBrowser control and Internet Explorer for the current user:

using (var key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
@"Software\Microsoft\Internet Explorer\PageSetup", true))
{
key.SetValue("Print_Background", "yes", Microsoft.Win32.RegistryValueKind.String);
}

Here is the test html that I used:

<html>
<head>
<style>
body { background-image: url("https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png"); }
</style>
</head>
<body>
</body>
</html>


Related Topics



Leave a reply



Submit