19

I finally figured out how to print transformed XML without prompting the user or showing an IE window, but now I need to specify a number of copies and possibly other printer settings.

Is there a way to programmatically change printer settings on a WebBrowser control?

The code in question:

private static void PrintReport(string reportFilename)
{
    WebBrowser browser = new WebBrowser();

    browser.DocumentCompleted += browser_DocumentCompleted;

    browser.Navigate(reportFilename);
}

private static void browser_DocumentCompleted
    (object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;

    if (null == browser)
    {
        return;
    }

    browser.Print();

    browser.Dispose();
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Chris Doggett
  • 19,959
  • 4
  • 61
  • 86

4 Answers4

17

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.

Tod
  • 2,070
  • 21
  • 27
Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
  • Any problems with that approach if the users are locked-down non-Admins? Which registry keys do I need to set? – Chris Doggett Apr 03 '09 at 15:56
  • So long as the user is able to change the settings in the Page Setup dialog, you can edit the registry settings on the fly. I'll look up the keys and put them in my answer. – Austin Salonen Apr 03 '09 at 19:01
  • Any way to change the printer name or number of copies via that method? That's what I really need to do, but the registry keys only really have header, footer, and margins. If not, I'll go ahead and accept your answer and force the users to just pick their damn printer and number of copies. – Chris Doggett Apr 06 '09 at 18:27
  • 3
    Thanks. This turned out to be exactly what I needed after dropping one of the nulls from the InvokeMethod call. Works perfectly. – Chris Doggett Apr 15 '09 at 16:40
5
            string strKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
        bool bolWritable = true;

        RegistryKey oKey = Registry.CurrentUser.OpenSubKey(strKey, bolWritable);
        Console.Write(strKey);

        if (stringToPrint.Contains("Nalog%20za%20sluzbeno%20putovanje_files"))
        {
            oKey.SetValue("margin_bottom", 15);
            oKey.SetValue("margin_top", 0.19);
        }
        else
        {
            //Return onld walue
            oKey.SetValue("margin_bottom", 0.75);
            oKey.SetValue("margin_top", 0.75);
        }
2

you need to change registry settings via code to change settings for internet explorer or the web browser control. check out the link below, it describes how to do so, also if there's more options you need to alter using the registry, then use regedit.exe to find what other keys internet explorer has.

http://support.microsoft.com/kb/236777

ps: you should note that any changes you make via your code to internet explorer's registry settings will persist on your system/user account.

LamdaComplex
  • 871
  • 1
  • 8
  • 21
lilmoe
  • 21
  • 1
  • That MSDN page is atrocious. It doesn't show the actual values for those registry keys. Also what it states there evidently is not entirely true. There's a way to do (some of it) w/o changing it on the global scale: https://support.microsoft.com/en-us/kb/267240 – c00000fd Dec 09 '15 at 22:06
0

This worked well for me, however I am on .NET 3.5

this.webBrowser1.ShowPrintDialog();
cessationoftime
  • 896
  • 8
  • 17
  • 4
    That'll work, too, but for what I needed 3 years ago, it had to be completely programmatic, with no user interaction. The funny part was when I spent months writing all the code to do report printing, then the higher-ups decided printing shouldn't be allowed, just viewing. – Chris Doggett Dec 05 '12 at 04:47