Reputation: 1058
I am trying to print pdf from windows application in my project with iTextSharp
dll..
But,The methods I have used till now doesn't seems to be reliable.(Sometimes it work and sometime it doesn't)
I am combining the whole process in a Process class and writing the followin code.
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);
The problem is that if,no pdf is open then the process works fine...(It prints well).But if any pdf is open then WaitForExit(..)
method just doesn't waits for the process to exit..and hence the process runs too fast and as I am deleting the file after print it gives error if counter(no of times..) for printing report is more than once..
I have also used Timer
to slow the process but it just doesn't works.I don't know why.
Used Sleep command too..but it makes main thread to go to sleep and hence doesn't suits me either.
Please suggest me some really reliable way to do so..:)
Upvotes: 0
Views: 2806
Reputation: 938
I do not know what kind of application you are trying to print from but a real easy way to print documents or pdfs is to simply copy the file to the printer queue and it will handle everything for you. Very reliable. Just have to have adobe reader installed on the machine printing and the correct driver for the printer. Example code:
var printer = PrinterHelper.GetAllPrinters().FirstOrDefault(p => p.Default);
PrinterHelper.SendFileToPrinter("C:\\Users\\Public\\Documents\\Form - Career Advancement Request.pdf", printer.Name);
Printer Helper code:
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;
}
}
Upvotes: 2
Reputation: 2272
Try this, you need .net 4 or above. And 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);
Upvotes: 0