【问题标题】:Printing an HTML document打印 HTML 文档
【发布时间】:2014-04-29 19:38:21
【问题描述】:

我正在使用以下代码成功打印 HTML 文档:

using (WebBrowser webBrowser = new WebBrowser())
{
    webBrowser.DocumentText = text;
    while (webBrowser.ReadyState != WebBrowserReadyState.Complete)
        Application.DoEvents();

    InternetExplorer internetExplorer = (InternetExplorer)webBrowser.ActiveXInstance;
    internetExplorer.PrintTemplateTeardown += InternetExplorer_PrintTemplateTeardown;
    internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER);

    while (!documentPrinted)
        Application.DoEvents();

    internetExplorer.PrintTemplateTeardown -= InternetExplorer_PrintTemplateTeardown;
}

两个问题:

  1. 打印的纸张有页眉 (page 1 of 1) 和页脚 (about:blankdate)。没有它们我该如何打印?
  2. 打印的纸张比实际的 HTML 页面内容长得多。内容结束时如何停止打印?

【问题讨论】:

  • 谢谢,该主题提供了丰富的信息,但我无法更改正在打印的网页。有没有办法在不改变页面的情况下清除这些页眉和页脚?
  • 是的,该线程中有一个指向IDM_PRINT 的链接,允许设置自定义页眉/页脚(包括一个空的)。

标签: c# html printing webbrowser-control printing-web-page


【解决方案1】:

我找到了一个不使用自定义打印模板的解决方案。
此代码清除页眉和页脚:

    const string keyName = @"Software\Microsoft\Internet Explorer\PageSetup";

    using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
    {
        if (key != null)
        {
            key.SetValue("footer", string.Empty);
            key.SetValue("header", string.Empty);
        }
    }

为了在浏览器内容结束时在热敏打印机中剪纸,我在这一行添加了PRINT_WAITFORCOMPLETION参数:

internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Win32.PRINT_WAITFORCOMPLETION);

【讨论】:

  • 这个答案为我节省了很多时间。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2015-11-18
  • 2019-06-20
  • 2019-04-13
  • 1970-01-01
相关资源
最近更新 更多