2012-12-18 126 views
0

我正在尝试将HTML报告批量打印到我的默认打印机,该打印机是用于自动保存的PDF Creator设置。我已经通过Internet Explorer加载了HTML文件,并从那里打印出来,而无需用户提示。大容量打印HTML问题假脱机文件

我遇到的问题是,当我的程序循环打印HTML文件列表时,它发现一些文档不能打印并且不打印。我在网上读过,可以使用while循环和Application.Dowork()来解决这个问题。当我偶尔执行这些操作时,我的所有文档都会打印出来,这是一个很大的改进,但这只是偶尔而不是确定的。

我的问题可能是每个线程在完成处理之前关闭吗?

如果是这样,我怎么能让线程独立运行,以便它们在处理过程中不会关闭?

下面是我用来打印文档的默认打印机代码:

foreach (var x in fileList) 
       { 

        // Printing files through IE on default printer. 
        Console.WriteLine("{0}", x); 
        SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
        IE.DocumentComplete += new SHDocVw.DWebBrowserEvents2_DocumentCompleteEventHandler(IE_DocumentComplete); 
        IE.PrintTemplateTeardown += new SHDocVw.DWebBrowserEvents2_PrintTemplateTeardownEventHandler(IE_PrintTemplateTeardown); 
        IE.Visible = true; 
        IE.Navigate2(x); 
        while (IE.ReadyState != SHDocVw.tagREADYSTATE.READYSTATE_COMPLETE) 
        { 
         System.Windows.Forms.Application.DoEvents(); 
        } 

       } 
      } 
     } 
    } 

    static void IE_PrintTemplateTeardown(object pDisp) 
    { 
     if (pDisp is SHDocVw.InternetExplorer) 
     { 
      SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp; 
      IE.Quit(); 
      System.Environment.Exit(0); 
     } 
    } 

    static void IE_DocumentComplete(object pDisp, ref object URL) 
    { 
     if (pDisp is SHDocVw.InternetExplorer) 
     { 
      SHDocVw.InternetExplorer IE = (SHDocVw.InternetExplorer)pDisp; 
      IE.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, 2); 
     } 
    } 

回答

1

你怎么看待下面的办法?我正在使用System.Windows.Forms.WebBrowser控件在async修饰符修饰的表单的方法内发出请求。在该方法中,我使用await将导航推迟到以下链接,直到打印文件列表包含当前浏览的文件列表。

namespace WindowsFormsApplication7 
{ 
    public partial class Form1 : Form 
    { 
     List<string> fileList; 
     List<string> printedFileList; 
     public Form1() 
     { 
      InitializeComponent(); 

      fileList = new List<string>(); 
      printedFileList = new List<string>(); ; 

      fileList.Add("http://www.google.de/"); 
      fileList.Add("http://www.yahoo.de/"); 

      webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted; 
     } 

     void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
     { 
      if (!printedFileList.Contains(webBrowser1.Url.AbsoluteUri)) 
       webBrowser1.Print(); 
      printedFileList.Add(webBrowser1.Url.AbsoluteUri); 
     } 

     private async void Form1_Load(object sender, EventArgs e) 
     { 
      foreach (string link in fileList) 
      { 
       webBrowser1.Navigate(link); 
       await Printed(link); 
      } 
     } 

     private Task Printed(string link) 
     { 
      return Task.Factory.StartNew(() => 
      { 
       while (!printedFileList.Contains(link)) 
       { } 
      }); 
     } 
    } 
} 
相关问题