Click here to Skip to main content
15,904,155 members
Articles / Programming Languages / C#

Track a Document Within a Printer Queue

Rate me:
Please Sign up or sign in to vote.
4.20/5 (5 votes)
28 Sep 2016CPOL 17.1K   14   10
How to track a document within a printer queue

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(); // Get all the printers installed on this PC
           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)
                 Console.WriteLine("Looking for document " + documentName + " or Document title " + documentTitle) ;
                    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)
                         //This is for debug and information
                         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"));

                         //Find if the document has been submitted, before this document print process was invoked
                        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;

                        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);

                    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()
                PrintDialog pd = new PrintDialog();
                pd.PrinterSettings = new PrinterSettings();

                FileInfo file = new FileInfo(TempPDFPath);
                Stopwatch timeTaken = new Stopwatch();

                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))

            catch (Exception e)




This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionHard to understand the code Pin
Brian C Hart28-Sep-16 9:21
professionalBrian C Hart28-Sep-16 9:21 
GeneralMy vote of 1 Pin
Brian C Hart28-Sep-16 9:20
professionalBrian C Hart28-Sep-16 9:20 
GeneralRe: My vote of 1 Pin
Wendelius28-Sep-16 17:44
mentorWendelius28-Sep-16 17:44 
GeneralRe: My vote of 1 Pin
Thomas Cooper28-Sep-16 18:13
Thomas Cooper28-Sep-16 18:13 
GeneralRe: My vote of 1 Pin
Wendelius28-Sep-16 18:40
mentorWendelius28-Sep-16 18:40 
GeneralRe: My vote of 1 Pin
Brian C Hart16-Mar-17 10:28
professionalBrian C Hart16-Mar-17 10:28 
I think what I am really talking about is not quite at the level of formatting, although I agree that that is an issue. However, I am mostly talking from a semantic level.

Issues that I see with your code are:
- lines are packed closely together. The code listings are in just one big "glob" each.
- Zero comments. And that would not be an issue if the code were more fluent, and I am not talking fluent API, I am talking "fluent" as in, "I can sit down and just look at the code and know what it's doing by reading it. The hell with comments." Right now, as written, the code doesn't "say" to me "what it does."

For an article like this that is meant to be explanatory, it comes across as just being very "dense code", and I am having trouble seeing the trees inside the forest. It's like being in the middle of a farm field and 300 meters away there is a stand of trees. At that distance, all the trees tend to blend in to each other somewhat. But then, if I walk up to the edge of the trees, I can see the individual plants right in front of me. This article is hard to approach because the code is packed so densely and with such a low degree of commenting or fluency, that I am seeing the forest from 300 meters away when I should be right up against the particular trees in front of me.

Brian Hart
SuggestionRe: My vote of 1 Pin
Wendelius16-Mar-17 18:35
mentorWendelius16-Mar-17 18:35 
QuestionTypo Pin
Nelek28-Sep-16 5:20
protectorNelek28-Sep-16 5:20 
AnswerRe: Typo Pin
Thomas Cooper28-Sep-16 5:31
Thomas Cooper28-Sep-16 5:31 
GeneralRe: Typo Pin
Nelek28-Sep-16 5:35
protectorNelek28-Sep-16 5:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.