Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
like image 538
saydur rahman Avatar asked Oct 19 '17 18:10

saydur rahman


People also ask

How do I set the paper size in Chrome?

Step 1: Click the three dots on the upper right corner of your Google Chrome browser to expand the More Options list. Step 2: Select Print. Step 3: Click on More Settings. Step 4: Select the correct paper size from the dropdown.

How do I change the default print settings in Chrome?

To change the printer settings in Chrome, you first open a browser and locate the page you want to print. After locating a page to print, click the three vertical dots in the top right corner of Chrome to generate a drop-down menu with options and settings controls. Click "Print" to open the printer settings.


1 Answers

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

like image 176
Jeremy Thompson Avatar answered Oct 05 '22 23:10

Jeremy Thompson