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

前端 未结 2 835
忘了有多久
忘了有多久 2020-12-17 23:30

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.

2条回答
  •  Happy的楠姐
    2020-12-18 00:20

    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

提交回复
热议问题