0

我正在尝试将 HTML 报告批量打印到我的默认打印机,这是用于自动保存的 PDF Creator 设置。我通过 Internet Explorer 加载了 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);
        }
    }
4

1 回答 1

1

您如何看待以下方法?我在用修饰符修饰的表单方法System.Windows.Forms.WebBrowser内使用控件发出请求。在该方法中,我使用推迟到以下链接的导航,直到打印文件列表包含当前浏览的文件。Form_Loadasyncawait

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))
                { }
            });
        }
    }
}
于 2012-12-19T14:36:13.017 回答