2
votes

Is it possible to change the orientation on the printer when using the webbrowser control? I need to change it to landscape. If I need to change the printer defaults for the printer itself that would be ok to as I would just set them back after I was done. (That's what I currently have to do with printing to a non-default printer).

I currently use this to temporarealy set the default printer, then set it back when I'm done with my print job...

private string SetDefaultPrinter(string newDefaultPrinter)
{
    //Get the list of configured printers: 
    string strQuery = "SELECT * FROM Win32_Printer";
    string currentDefault = string.Empty;

    System.Management.ObjectQuery oq = new System.Management.ObjectQuery(strQuery);
    System.Management.ManagementObjectSearcher query1 = new System.Management.ManagementObjectSearcher(oq);
    System.Management.ManagementObjectCollection queryCollection1 = query1.Get();
    System.Management.ManagementObject newDefault = null;

    foreach (System.Management.ManagementObject mo in queryCollection1)
    {
        System.Management.PropertyDataCollection pdc = mo.Properties;
        if ((bool)mo["Default"])
        {
            currentDefault = mo["Name"].ToString().Trim();

            if (newDefaultPrinter == null || newDefaultPrinter == string.Empty)
            {
                //Just need to know the default name 
                break;
            }
        }
        else if (mo["Name"].ToString().Trim() == newDefaultPrinter)
        {
            //Save this for later
            newDefault = mo;
        }
    }

    //Reset the default printer
    if (newDefault != null) 
    {
        //Execute the method 
        System.Management.ManagementBaseObject outParams = newDefault.InvokeMethod("SetDefaultPrinter", null, null);
    }

    return currentDefault;
}

Anyone know how to change the orientation?

2
Thanks Jay, yea I found that one and tried to check out the application in the given answer on that question. Never could get the application in the project to work. But with that question I never did see an answer that actually answered the question. I found a few other question such as mine on SO as well but non of them referenced c#, or non-wpf directly. I also wanted to add the bit about my current code to set the default printer. ;)Arvo Bowen
Sorry, I didn't notice your non-WPF condition. I originally answered that there was no way to do what you're asking and referenced kb236777. I realized though I never really checked into this and so deleted my answer.Jay Riggs
:) I was hoping that that had to be SOME WAY to set the default settings on a printer... I don't really care about WebBrowser at this point... I figure I can just set the printer defaults, then print.Arvo Bowen

2 Answers

1
votes

You can do this by using IE print templates. There are not too much documentation on this, but here below is another stack-overflow post that suggests some useful links on this regards, and it actually helped me a lot:

WebBrowser print settings

The most useful part was suggesting to view the standard IE print template by navigating to the below URL inside IE:

res://ieframe.dll/preview.dlg

And also you can view a related JavaScript file by navigating to the below URL inside IE:

res://ieframe.dll/preview.js

Those two files helped me a lot to understand what is going on in the background, and by changing the "Printer.orientation" value inside the "preview.js" file, I could successfully change the orientation of the printed HTML document.

0
votes

//EDIT: I was testing it wrong. The documentation referring to this registry key is for windows CE... So the correct answer is that it is not possible, as "explained" in the documentation: http://support.microsoft.com/kb/236777

A possible workaround is to rotate the whole page through css (transform:rotate(90deg)), but the relative position keeps being the old one, so for multiple pages is just a mess.

It is incredible that something so basic can't be done...

//OLD ANSWER: I was looking for the same and finally found that you really can't change the printer settings (page orientation, header, footer, margins...) directly with the webbrowser component, the only way to do it is changing the registry key to set the default behaviour of internet explorer.

For the page orientation it would be:

Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true).SetValue("PageOrientation", 2);

You should keep the old value and restore it after printing.