简体   繁体   中英

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

I am trying to print from a web browser control in a winform application.The matter is it sets letter as default paper size but I need A4 . Also it automatically sets some margins wrong, I can set them to correct settings manually but I want to do it programmatically.

How is it possible?

Here is my code to print.

private void metroButton1_Click(object sender, EventArgs e)
    {
        loadprintData();
        // Create a WebBrowser instance. 
        WebBrowser webBrowserForPrinting = new WebBrowser();

        // Add an event handler that prints the document after it loads.
        wa.DocumentCompleted +=
            new WebBrowserDocumentCompletedEventHandler(ShowPrintDocument);
        wa.ShowPrintPreviewDialog();
        reloadpage();

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

        // Dispose the WebBrowser now that the task is complete. 
        // ((WebBrowser)sender).Dispose();
        reloadpage();
    }
    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();
    }

To change the Margin size you have to edit the (HKCU) registry before printing:

string pageSetupKey = "Software\\Microsoft\\Internet Explorer\\PageSetup";
bool isWritable = true;

RegistryKey rKey = Registry.CurrentUser.OpenSubKey(pageSetupKey, isWritable);

if (stringToPrint.Contains("something"))
{
    rKey.SetValue("margin_bottom", 0.10);
    rKey.SetValue("margin_top", 0.25);
}
else
{
    //Reset old value
    rKey.SetValue("margin_bottom", 0.75);
    rKey.SetValue("margin_top", 0.75);
}

Dont forget to set it back to the default.

Ref Microsoft KB Article


To change the Paper size you have to edit the (HKCU) registry in another place before printing:

string pageSetupKey2 = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
isWritable = true;

rKey = Registry.CurrentUser.OpenSubKey(pageSetupKey2, isWritable);

// Use 1 for Portrait and 2 for Landccape 
rKey.SetValue("PageOrientation", 2, RegistryValueKind.DWord); 
// Specifies paper size. Valid settings are 1=letter, 5=Legal, 9=A4, 13=B5.Default setting is 1.
rKey.SetValue("PaperSize", 9, RegistryValueKind.DWord); 
// Specifies print quality
rKey.SetValue("PrintQuality ", 1, RegistryValueKind.DWord);

Ref MSDN

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM