I have a printer (HP PageWide MFP P57750) connected to a private network by a ethernet cable. In a simple C# Web App, user sends a pdf file to the printer and set the number of copies to be printed. At the end of the print, the app should notify the end of the job.
I try using the EndPrint event of PrintDocument but it seems to fire in a wrong time, cause printer is still printing.
I also try using PrintQueue with PrintJobInfoCollection but it returns the "Printing" PrintJobStatus just the first time. Then, I wait for 5 seconds and recheck the status but there are no jobs in the queue while the printer is still printing!
I see in Control Panel -> Devices And Printers the progress of jobs. The print lasts just a few seconds even if there are many copies and the printer is still printing. I think the job gets deleted after client sends the file to be printed to the printer.
How can i catch the real end of printing?
PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile(filePath);
pdf.PrinterName = CicloStampa.Properties.Settings.Default.NomeStampante;
pdf.PrintDocument.PrinterSettings.Copies = (short)numeroCopie;
pdf.PrintDocument.Print();
PrintServer myPrintServer = new PrintServer();
PrintQueue pq = myPrintServer.GetPrintQueue(CicloStampa.Properties.Settings.Default.NomeStampante);
var printing = true;
while (printing)
{
pq.Refresh();
PrintJobInfoCollection pCollection = pq.GetPrintJobInfoCollection();
foreach (PrintSystemJobInfo job in pCollection)
{
//SpotTroubleUsingJobAttributes returns KO/OK by matching PrintJobStatus. First time returns PrintJobStatus.Priting. Second time, pCollection is empty and the printer is still printing.
var res = SpotTroubleUsingJobAttributes(job);
System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + @"\Logs\LogImportazione.txt", DateTime.Now.ToString() + " ---- \t\t\t RES: " + res + "\n");
if (res.StartsWith("KO"))
{
//DO STUFF
}
else
{
//END OF PRINT
printing = false;
}
}
if (pCollection.Count() == 0)
{
printing = false;
}
if (printing)
{
System.Threading.Thread.Sleep(5000);
}
}
You could get a collection of the jobs on the print queue then keep looping until you get the job status you are looking for.
EDIT
The PrintQueue also has properties such as IsPrinting, IsWaiting, IsBusy etc, you could look at these properties too in your while loop but these would tell you about the printer in general, not about your jobs specifically so if someone else decides to print when you do you could get misleading information