8

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();
    }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
saydur rahman
  • 119
  • 1
  • 9

2 Answers2

10

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

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Thanks a lot for your reply. i think the solution is on the way using your solution. but i think i am missing something. when i am adding those lines and afterwards i am adding my wa.ShowPrintPreviewDialog(); there is a print blank print preview dialog comes out that is empty and after that the reguler print preview appears with the previous problem. what am i missing? – saydur rahman Oct 23 '17 at 05:06
  • Can you check the possible causes here https://support.microsoft.com/en-au/help/973479/unable-to-print-or-view-the-print-preview-of-a-webpage-in-internet-exp – Jeremy Thompson Oct 23 '17 at 23:04
  • Actually the margins are working pretty fine as your way. but orientations not working and page size.i checked my IE settings and did all they said. – saydur rahman Oct 24 '17 at 04:52
  • We seem to be working opposite hours I'm in Australia and am about to finish for the day.Let me work on this tomorrow morning. – Jeremy Thompson Oct 24 '17 at 05:39
  • This is odd because the code reflects what the documentation says: https://msdn.microsoft.com/en-us/library/ms905101.aspx – Jeremy Thompson Oct 24 '17 at 05:40
  • Try doing a search in your RegEdt32, try and find **paper_size** – Jeremy Thompson Oct 24 '17 at 05:42
  • If you cant find it, use [ProcessMonitor](https://learn.microsoft.com/en-us/sysinternals/downloads/procmon) to work out where the setting is stored. – Jeremy Thompson Oct 24 '17 at 05:45
  • Thanks a lot for the effort mate. I am also thinking why its not working. If i change the paths it throws error that means The path found the paper_size i think but somehow its not changing. – saydur rahman Oct 24 '17 at 06:14
0

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.

saydur rahman
  • 119
  • 1
  • 9