我正在尝试使用iTextSharp
dll ..
但是,我到目前为止使用的方法似乎并不可靠。(有时候它可以工作,有时却没有)
我在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命令..但它使主线程进入睡眠状态,因此也不适合我。
请建议我一些非常可靠的方法.. :)
答案 0 :(得分:1)
我不知道您尝试从哪种应用程序进行打印,但打印文档或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;
}
}
答案 1 :(得分: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);