C# 打印HTML文档

C# 打印HTML文档,c#,html,printing,webbrowser-control,printing-web-page,C#,Html,Printing,Webbrowser Control,Printing Web Page,我正在使用以下代码成功打印HTML文档: using (WebBrowser webBrowser = new WebBrowser()) { webBrowser.DocumentText = text; while (webBrowser.ReadyState != WebBrowserReadyState.Complete) Application.DoEvents(); InternetExplorer internetExplorer = (I

我正在使用以下代码成功打印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页,共1页)和页脚(
    关于:空白
    日期
    )。没有它们我怎么能打印
  • 打印的纸张比实际的HTML页面内容长得多。当内容结束时,如何停止打印

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

        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\u WAITFORCOMPLETION
    参数:

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

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

        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\u WAITFORCOMPLETION
    参数:

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

    仅供参考:谢谢,帖子内容丰富,但我无法更改正在打印的网页。有没有办法在不改变页面的情况下清除这些页眉和页脚?是的,该线程有一个指向
    IDM_PRINT
    的链接,可以设置自定义页眉/页脚(包括空页眉/页脚)。相关:仅供参考:谢谢,该线程提供了信息,但我无法更改正在打印的网页。有没有办法在不改变页面的情况下清除这些页眉和页脚?是的,在该线程中有一个指向
    IDM_PRINT
    的链接,允许设置自定义页眉/页脚(包括空页眉/页脚)。相关:这个答案节省了我很多时间。谢谢。这个答案节省了我很多时间。非常感谢。