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.
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.
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