Question

I'm printing an HTML document successfully with the following code:

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;
}

Two problems:

  1. The printed paper has an header (page 1 of 1) and a footer (about:blank and date). How can I print without them?
  2. The printed paper is much longer than the actual HTML page content. How can I stop the printing when the content ends?
Was it helpful?

Solution

I've found a solution without using a custom print template.
This code clears the header and footer:

    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);
        }
    }

In order to cut the paper in the thermal printer when the browser's content ends, I've added the PRINT_WAITFORCOMPLETION parameter to this line:

internetExplorer.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, Win32.PRINT_WAITFORCOMPLETION);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top