How to Programmatically Change Printer Settings with the Webbrowser Control

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.

Programmatically changing the destination printer for a WinForms WebBrowser control

Given what you've said, perhaps if you restart the process which contains the web browser control (or the process which is the web browser control), after you change the default printer? That's the kind of thing I see happening here, for example.


I suppose it would be possible to fork off a background process that does the actual printing, but I'm really hoping for a simpler solution.

Forking was my first thought towards a probably-simplest solution.

Some other alternatives are as follows.

1). IE, which the webbrowser control is wrapping, exposes APIs via ActiveX. One of its/those APIs might let you specify the destination printer.

2). Some executables (I don't know about IE) have printto entries in the registry. For example, Acrobat Reader has an entry whose value is as follows:

""C:\Program Files\Adobe\Reader 9.0\Reader\AcroRd32.exe""  /t "%1" "%2" "%3" "%4"

That's used for specifying the syntax of the command-line which you can use to print and specify a (non-default) printer. You can also Google for printto, see e.g. PrintTo command in the ShellExecute.

3). I have implemented an HTML control for .NET of my own, which doesn't depend on IE. You say that your HTML (and CSS I presume) are simple, so perhaps I can render it, either out of the box or with only a little extra development effort. I don't support printing, but printing is quite easy for a user control to implement. Getting me to implement that for you would cost you several hundred but, who knows, maybe it's worth it to you. It would be quite a light-weight solution, and perhaps well supported. You could email me if you want to discuss that further.

4). You might also find other controls, similar to mine, more or less famous/expensive; or other applications, e.g. OpenOffice etc etc.

5). You could consider converting the HTML (somehow) to another format (e.g. PDF) for which you have an application which gives you better support for printing.

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.

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

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.

Set the printer to landscape for the WebBrowser Control

This is not possible. While the user is capable of setting printer settings (and even able to select a different printer) using the user interface, this is not possible programatically.

Some "hacks" exist where users set values in the registery directly (for the Internet Explorer keys), but mistakes here could be problematic.

Refer to the following Microsoft Knowledge base article: http://support.microsoft.com/kb/313723
Also a connect issue on this: https://connect.microsoft.com/VisualStudio/feedback/details/678232
And another related StackOverflow question: How do I programatically change printer settings with the WebBrowser control?

WPF WebBrowser: changing IE print dialog properties programmatically

The only way to modify the print settings without modifying the registry is via Print Templates and it seems like no one has really used them from the .NET web browser control.

However, this answer has additional resources regarding print templates and their usage from C++/win32.

If you're open to using ActiveX for printing the page, you could use the SHDocVw.WebBrowser which then gives you access to the ability to specify the print template, as noted in this answer



Related Topics



Leave a reply



Submit