0

I am trying to print an HTML page from the web to a PDF file in C#. The code below works if printing to a generic printer, but NOT if printing to a PDF file where a filename must be set. How can we specify printer options when calling IE from C#? Need to specify a filename, and orientation would be nice as well. Mucking with the PrinterSettings does not seem to have any effect, probably because we are calling out to IE.

The pages have to be rendered with graphics, so just printing the HTML code will not work, hence the use of IE below to render the job before printing.

The project is intended to allow Emergency Service operators to pull offline versions of web reference content which would not be available after a disaster took down the Internet. I lack the funds to pay over $500 for commercial libraries.

using System.Drawing.Printing;
using System.Reflection;
using System.Threading;
using SHDocVw;


namespace HTMLPrinting
{
    public class HTMLPrinter
    {
        private bool documentLoaded;
        private bool documentPrinted;
        public static PrinterSettings Printer_Settings = new System.Drawing.Printing.PrinterSettings();
        private void ie_DocumentComplete(object pDisp, ref object URL)
        {
            documentLoaded = true;
        }

        private void ie_PrintTemplateTeardown(object pDisp)
        {
            documentPrinted = true;
        }
        
        public bool HPrint(string htmlURL)
        {
            if (!Printer_Settings.PrinterName.Equals("Microsoft Print to PDF"))  return false;

            Printer_Settings.DefaultPageSettings.PrinterSettings.PrinterName = "Microsoft Print to PDF";
            Printer_Settings.DefaultPageSettings.PrinterSettings.PrintFileName = @"c:\temp\output.pdf";
            Printer_Settings.DefaultPageSettings.PrinterSettings.PrintToFile = true;

            documentLoaded = false;
            documentPrinted = false;

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

            object missing = Missing.Value;

            ie.Navigate(htmlURL, 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();

            return true;
        }
    }
}

Code is copied from another question Print html document from Windows Service without print dialog

I have also looked at trying to create an MHTML (.MHT) file, or saving to a Word doc, and no luck on those either.

Stiv Ostenberg
  • 607
  • 5
  • 9

0 Answers0