简体   繁体   English

如何从 Web 浏览器控件设置纸张尺寸和边距打印

[英]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 .我正在尝试从 winform 应用程序中的 Web 浏览器控件进行打印。问题是它将letter设置为默认纸张大小,但我需要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();
    }

To change the Margin size you have to edit the (HKCU) registry before printing:要更改边距大小,您必须在打印前编辑 (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);
}

Dont forget to set it back to the default.不要忘记将其设置回默认值。

Ref Microsoft KB Article 参考 Microsoft 知识库文章


To change the Paper size you have to edit the (HKCU) registry in another place before printing:要更改纸张尺寸,您必须在打印前在其他地方编辑 (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);

Ref MSDN参考 MSDN

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.但我可以通过@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.我发现对于从 WebBrowser 控件进行打印,它使用了我们所知道的 Internet Explorer,但一开始它使用的是资源管理器 7,我不得不将其更改为默认的资源管理器 11。 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.因此,您必须更改默认打印机预览。您将看到预览会以这种方式显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM