Print html document from Windows Service without print dialog

First off, here’s the code:

using System.Reflection;
using System.Threading;
using SHDocVw;

namespace HTMLPrinting
{
  public class HTMLPrinter
  {
    private bool documentLoaded;
    private bool documentPrinted;

    private void ie_DocumentComplete(object pDisp, ref object URL)
    {
      documentLoaded = true;
    }

    private void ie_PrintTemplateTeardown(object pDisp)
    {
      documentPrinted = true;
    }

    public void Print(string htmlFilename)
    {
      documentLoaded = false;
      documentPrinted = false;

      InternetExplorer ie = new InternetExplorerClass();
      ie.DocumentComplete += new DWebBrowserEvents2_DocumentCompleteEventHandler(ie_DocumentComplete);
      ie.PrintTemplateTeardown += new DWebBrowserEvents2_PrintTemplateTeardownEventHandler(ie_PrintTemplateTeardown);

      object missing = Missing.Value;

      ie.Navigate(htmlFilename, ref missing, ref missing, ref missing, ref missing);
      while (!documentLoaded && ie.QueryStatusWB(OLECMDID.OLECMDID_PRINT) != OLECMDF.OLECMDF_ENABLED)
        Thread.Sleep(100);

      ie.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref missing, ref missing);
      while (!documentPrinted)
        Thread.Sleep(100);

      ie.DocumentComplete -= ie_DocumentComplete;
      ie.PrintTemplateTeardown -= ie_PrintTemplateTeardown;
      ie.Quit();
    }
  }
}
  1. You can access the SHDocVw namespace by adding a reference to ‘Microsoft Internet Controls’, found on the COM tab of the Add Reference dialog.
  2. More information on the InternetExplorer object can be found on MSDN.
  3. The Navigate() method will load the HTML file. The other parameters allow you to specify optional parameters, such as flags and headers.
  4. We can’t print until the document is loaded. Here, I enter a loop waiting until the DocumentComplete event is called, upon which a flag is set notifying us that navigation has completed. Note that DocumentComplete is called whenever navigation is finished – upon success or failure.
  5. Once the documentLoaded flag is set, the printing status is queried via QueryStatusWB() until printing is enabled.
  6. Printing is started with the ExecWB() call. The OLECMDID_PRINT command is specified, along with the option OLECMDEXECOPT_DONTPROMPTUSER to automatically print without user interaction. An important note is that this will print to the default printer. To specify a printer, you will have to set the default printer (in code, you could call SetDefaultPrinter()). The two final parameters allow optional input and output parameters.
  7. We don’t want to quit until printing is complete, so once again a loop is entered. After the PrintTemplateTeardown event is fired, the documentPrinted flag is set. The objects can then be cleaned up.

Leave a Comment