This is a simple code example on how you could use C# to check your default printer queue for a document you have sent to the printer. The code is designed to stop checking, once it has found the item within the queue.
Query the queue
This checks your local PC for a printer Queue of the printer name you have provided
public static PrintQueue GetPrinterQueue(string name, ref bool found)
{
PrintServer myPrintServer = new PrintServer();
PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
var pq = myPrintQueues.FirstOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
if (pq != null)
{
}
return pq;
}
Get the printer items from the queue
Now it has a printer queue, it will look through the queue and if it find a document which has the file name or document title, It will return true
public static PrintSystemJobInfo GetPrinterJob(PrintQueue pq, string documentName, string documentTitle, ref bool found)
{
if (pq.NumberOfJobs > 0)
{
pq.Refresh();
Console.WriteLine("Looking for document " + documentName + " or Document title " + documentTitle) ;
try
{
var jb = pq.GetPrintJobInfoCollection().OrderByDescending(o=>o.TimeJobSubmitted).FirstOrDefault(x => x.Name.Equals(documentName, StringComparison.OrdinalIgnoreCase));
if(jb == null && String.IsNullOrEmpty(documentTitle))
{
Utilities.DebugSolution.SetMessageUniqueFile("Printing", "Could not find using document name so will try document title "+ documentTitle);
jb = pq.GetPrintJobInfoCollection().OrderByDescending(o => o.TimeJobSubmitted).FirstOrDefault(x => x.Name.Contains(documentTitle));
}
if (jb != null)
{
Console.WriteLine("Document found");
Console.WriteLine("Job Name: " + jb.JobName);
Console.WriteLine("Job Status: " + jb.JobStatus.ToString());
Console.WriteLine("Document Name: " + jb.Name);
Console.WriteLine("Document Number of pages: " + jb.NumberOfPages.ToString());
Console.WriteLine("Document Date Time submitted: " + jb.TimeJobSubmitted.ToString("dd/MM/yyyy hh:mm:ss"));
bool differentJob = (jb.TimeJobSubmitted > _startJobTime);
Console.WriteLine(String.Format("Job Started on {0} and This document was submitted at {1}. This is a different document {2}",
_startJobTime.ToString("dd/MM/yyyy hh:mm:ss"), jb.TimeJobSubmitted.ToString("dd/MM/yyyy hh:mm:ss"), differentJob.ToString()));
found = differentJob;
}
if(!found)
{
Console.WriteLine("Required document was NOT within current queue");
Console.WriteLine( "Documents currently in the Queue");
foreach (var item in pq.GetPrintJobInfoCollection())
{
Console.WriteLine(" - " + item.Name);
}
Console.WriteLine("");
}
return jb;
}catch(Exception e)
{
Console.WriteLine("Utitlies.Printer.GetPrinterJob", e);
}
return null;
}
Look within the queue
This is the controller method, which will look for a document within the printer queue
public static bool JobOnQueue(string printerName, string documentName, string documentTitle)
{
bool found = false;
var pq = GetPrinterQueue(printerName, ref found);
if (found)
{
found = false;
var item = GetPrinterJob(pq, documentName, documentTitle, ref found);
if (item != null)
{
if (item.JobIdentifier > 0 && item.PositionInPrintQueue > 0 && !item.IsSpooling)
return true;
}
}
return false;
}
The main process method
This will check the printer queue, using the above method. It will only look for 30 seconds.
If the item has not been found, it will give the user the choice to keep on looking or cancel
public void TrackPrintedDocument()
{
try
{
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
FileInfo file = new FileInfo(TempPDFPath);
Stopwatch timeTaken = new Stopwatch();
timeTaken.Start();
while (!Utilities.Printer.JobOnQueue(pd.PrinterSettings.PrinterName, file.Name, DocumentTitle))
{
if (timeTaken.Elapsed.Seconds > 30)
{
if (MessageBox.Show("Could not find the item, do you want to continue", "Item not found", MessageBoxButtons.YesNo, MessageBoxIcon.Error).Equals(DialogResult.No))
break;
else
timeTaken.Restart();
}
}
}
catch (Exception e)
{
}
}