[英]How to set Paper Size and Margins printing from a web browser control
我正在尝试从 winform 应用程序中的 Web 浏览器控件进行打印。问题是它将letter设置为默认纸张大小,但我需要A4 。 它还会自动设置一些边距错误,我可以手动将它们设置为更正设置,但我想以编程方式进行。
这怎么可能?
这是我要打印的代码。
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();
}
要更改边距大小,您必须在打印前编辑 (HKCU) 注册表:
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);
}
不要忘记将其设置回默认值。
要更改纸张尺寸,您必须在打印前在其他地方编辑 (HKCU) 注册表:
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);
好吧,我已经尝试了很多东西,但最后我发现无法轻松地从代码中对打印机设置进行编程。 但我可以通过@jeremy 的回答来做保证金。 我发现对于从 WebBrowser 控件进行打印,它使用了我们所知道的 Internet Explorer,但一开始它使用的是资源管理器 7,我不得不将其更改为默认的资源管理器 11。 然后我看到资源管理器没有自己的打印设置。 它使用默认打印机设置。 因此,您必须更改默认打印机预览。您将看到预览会以这种方式显示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.