0

我正在尝试使用iTextSharpdll 从我的项目中的 Windows 应用程序打印 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,则该过程可以正常工作...(打印效果很好)。但是如果打开WaitForExit(..)了任何 pdf,则方法不会等待该过程退出..因此该过程也运行快速并且当我在打印后删除文件时,如果打印报告的计数器(次数..)不止一次,它会给出错误..

我也曾经Timer放慢这个过程,但它不起作用。我不知道为什么。也使用了睡眠命令..但它使主线程进入睡眠状态,因此也不适合我。

请建议我一些非常可靠的方法来做到这一点..:)

4

2 回答 2

2

我不知道您尝试从哪种应用程序打印,但打印文档或 pdf 的一种真正简单的方法是将文件复制到打印机队列,它会为您处理一切。非常可靠。只需在机器上安装 adobe reader 和正确的打印机驱动程序即可。示例代码:

        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 回答