0

自動保存用の PDF Creator セットアップであるデフォルトのプリンターに HTML レポートを一括印刷しようとしています。HTML ファイルを Internet Explorer 経由でロードし、そこからユーザー プロンプトなしで印刷します。

私が抱えている問題は、プログラムがループして 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 に答える