none
How to print Web page to XPS file silently without any Save as File dialog? RRS feed

  • Question

  • Hello,

    I've spent many days to implement this functionality, but couldn't taste any success. What I want is to created a XPS file of a web page automatically, without any GUI intervention. I'm able to automate everything but couldn't able to bypass "Save as File" dialog.

    Following is the code I'm using to generate XPS file. Please help me if you know any workaround. Thanks in advance.

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


    Ashwin Masarkar

    Tuesday, November 17, 2015 5:30 AM