Click here to Skip to main content
15,904,153 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 
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 
In your 2nd snippet:
(jb != null)
   Console.WriteLine("Document found");
   Console.WriteLine(Job Name: " + jb.JobName);

you forgot one quote " and it is messing with the format, besides being not compilable Wink | ;)

Please review
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

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.