0

iTextSharpプロジェクトのWindowsアプリケーションからdllを使用してpdfを印刷しようとしています..

しかし、今まで使ってきた方法は信頼できそうにありません。(効く時と効かない時がある)

プロセス全体を Process クラスに結合し、次のコードを記述しています。

                            printProcess = new Process[noOfCopies];
 // Print number of copies specified in configuration file
                            for (int counter = 0; counter < noOfCopies; counter++)
                            {
                                printProcess[counter] = new Process();

                                // Set the process information
                                printProcess[counter].StartInfo = new ProcessStartInfo()
                                {
                                    CreateNoWindow = true,
                                    Verb = "Print",
                                    FileName = pdfFilePath,
                                    ErrorDialog = false,
                                    WindowStyle = ProcessWindowStyle.Hidden,
                                    UseShellExecute = true,
                                };


    // Start the printing process and wait for exit for 7 seconds
                                    printProcess[counter].Start();

                                    // printProcess[counter].WaitForInputIdle(waitTimeForPrintOut);
                                    printProcess[counter].WaitForExit(waitTimeForPrintOut); //<--**This is line for discussion**

                                    if (!printProcess[counter].HasExited)
                                    {
                                        printProcess[counter].Kill();
                                    }
                                }
   // Delete the file before showing any message for security reason
                        File.Delete(pdfFilePath);

問題は、pdf が開いていない場合、プロセスが正常に動作することです...(うまく印刷されます)。ただし、pdf が開いている場合、WaitForExit(..)メソッドはプロセスが終了するのを待ちません。したがって、プロセスも実行されます。高速で、印刷後にファイルを削除しているため、レポートを印刷するためのカウンター(回数..)が複数回ある場合、エラーが発生します..

プロセスを遅くすることもありTimerましたが、うまくいきません。理由はわかりません。Sleepコマンドも使用しました..しかし、それはメインスレッドをスリープ状態にするため、私にも適していません。

そうするための本当に信頼できる方法を私に提案してください..:)

4

2 に答える 2

2

印刷しようとしているアプリケーションの種類はわかりませんが、ドキュメントや PDF を印刷するための本当に簡単な方法は、ファイルをプリンター キューにコピーするだけで、すべて処理されます。頼れる。マシンの印刷にアドビリーダーをインストールし、プリンターの正しいドライバーをインストールするだけです。コード例:

        var printer = PrinterHelper.GetAllPrinters().FirstOrDefault(p => p.Default);
        PrinterHelper.SendFileToPrinter("C:\\Users\\Public\\Documents\\Form - Career Advancement Request.pdf", printer.Name);

プリンター ヘルパー コード:

    public static class PrinterHelper
    {
        public class PrinterSettings
        {
            public string Name { get; set; }
            public string ServerName { get; set; }
            public string DeviceId { get; set; }
            public string ShareName { get; set; }
            public string Comment { get; set; }
            public bool Default { get; set; }
        }

    /// <summary>
    /// Sends the file to printer.
    /// </summary>
    /// <param name="filePathAndName">Name of the file path and Name of File.</param>
    /// <param name="printerName">Name of the printer with Path. E.I. \\PRINT2.company.net\P14401</param>
        public static void SendFileToPrinter(string filePathAndName, string printerName)
        {
            FileInfo file = new FileInfo(filePathAndName);
            file.CopyTo(printerName);
        }

        /// <summary>
        /// Gets all printers that have drivers installed on the calling machine.
        /// </summary>
        /// <returns></returns>
        public static List<PrinterSettings> GetAllPrinters()
        {
            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_Printer");
            ManagementObjectSearcher mos = new ManagementObjectSearcher(query);
            List<PrinterSettings> printers = new List<PrinterSettings>();

            foreach (ManagementObject mo in mos.Get())
            {
                PrinterSettings printer = new PrinterSettings();
                foreach (PropertyData property in mo.Properties)
                {
                    if (property.Name == "Name")
                        printer.Name = property.Value == null ? "" : property.Value.ToString();

                    if (property.Name == "ServerName")
                        printer.ServerName = property.Value == null ? "" : property.Value.ToString();

                    if (property.Name == "DeviceId")
                        printer.DeviceId = property.Value == null ? "" : property.Value.ToString();

                    if (property.Name == "ShareName")
                        printer.ShareName = property.Value == null ? "" : property.Value.ToString();

                    if (property.Name == "Comment")
                        printer.Comment = property.Value == null ? "" : property.Value.ToString();

                    if (property.Name == "Default")
                        printer.Default = (bool)property.Value;
                }
                printers.Add(printer);
            }

            return printers;
        }      
}
于 2012-11-02T13:28:09.923 に答える
0

これを試してみてください。.net 4 以上が必要です。そして System.Threading.Tasks

  printProcess = new Process[noOfCopies];
  // Print number of copies specified in configuration file
  Parallel.For(0, noOfCopies, i =>
  {
    printProcess[i] = new Process();

    // Set the process information
    printProcess[i].StartInfo = new ProcessStartInfo()
    {
      CreateNoWindow = true,
      Verb = "Print",
      FileName = pdfFilePath,
      ErrorDialog = false,
      WindowStyle = ProcessWindowStyle.Hidden,
      UseShellExecute = true,
    };


    // Start the printing process and wait for exit for 7 seconds
    printProcess[i].Start();

    // printProcess[counter].WaitForInputIdle(waitTimeForPrintOut);
    printProcess[counter].WaitForExit(waitTimeForPrintOut); //<--**This is line for discussion**

    if(!printProcess[i].HasExited)
    {
      printProcess[i].Kill();
    }
  });
  // Delete the file before showing any message for security reason
  File.Delete(pdfFilePath);
于 2012-11-02T13:13:54.723 に答える