【问题标题】:How to set Paper Size and Margins printing from a web browser control如何从 Web 浏览器控件设置纸张大小和边距打印
【发布时间】:2018-03-31 20:56:07
【问题描述】:

我正在尝试从 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();
    }

【问题讨论】:

标签: c# .net winforms printing webbrowser-control


【解决方案1】:

要更改边距大小,您必须在打印前编辑 (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);
}

不要忘记将其设置回默认值。

Ref Microsoft KB Article


要更改纸张大小,您必须在打印前在其他位置编辑 (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

【讨论】:

  • 非常感谢您的回复。我认为解决方案正在使用您的解决方案。但我想我错过了一些东西。当我添加这些行,然后我添加我的 wa.ShowPrintPreviewDialog();有一个打印空白打印预览对话框出来,它是空的,之后调节器打印预览出现之前的问题。我错过了什么?
  • 你能在这里检查一下可能的原因吗support.microsoft.com/en-au/help/973479/…
  • 实际上,边距按照您的方式工作得很好。但方向不起作用和页面大小。我检查了我的 IE 设置并按照他们说的做了。
  • 我们似乎在相反的时间工作,我在澳大利亚,即将结束这一天。让我明天早上继续工作。
  • 这很奇怪,因为代码反映了文档所说的内容:msdn.microsoft.com/en-us/library/ms905101.aspx
【解决方案2】:

好吧,我尝试了很多东西,但最后我发现无法轻松地从代码中对打印机设置进行编程。但我可以通过@jeremy 的回答来做到这一点。 我发现对于从 WebBrowser 控件进行打印,它使用我们所知道的 Internet Explorer,但一开始它使用的是 explorer 7,我不得不将其更改为默认的 explorer 11。 然后我看到它资源管理器没有自己的打印设置。它使用默认的打印机设置。 因此,您必须更改默认打印机预览。您将看到预览将以这种方式显示。

【讨论】:

    猜你喜欢
    • 2012-08-31
    • 1970-01-01
    • 2014-10-12
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多