11

I am able to print a document, but I do not know how to get its status. I went through many resources (MSDN, Links for checking Job Status), but was not able to find an answer.

I actually want to get confirmation from the printer whether the document was successfully printed or not. Moreover, I am also interested if I can get error signal from printer, like if paper is jammed.

I have the Printer Name and Document name which I am sending for Print. Has anybody done some research in this area and can tell me how to accomplish this?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
SharpUrBrain
  • 3,180
  • 5
  • 38
  • 56
  • 1
    System.Printing didn't make the cut for .NET 2.0 and probably shouldn't have made the cut for .NET 3.0. It isn't really to blame, the underlying Windows API just isn't strong. Detune your requirements. – Hans Passant Aug 05 '11 at 12:05

2 Answers2

7

You might be able to use WMI for this. It provides several printing-related classes, including Win32_PrintJob.

This is untested, but something like this should get you started:

SelectQuery query = new SelectQuery("Win32_PrintJob");

using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(query))
using (ManagementObjectCollection printJobs = searcher.Get())
    foreach (ManagementObject printJob in printJobs)
    {
        // The format of the Win32_PrintJob.Name property is "PrinterName,JobNumber"
        string name = (string) printJob["Name"];
        string[] nameParts = name.Split(',');
        string printerName = nameParts[0];
        string jobNumber = nameParts[1];
        string document = (string) printJob["Document"];
        string jobStatus = (string) printJob["JobStatus"];

        // Process job properties...
    }
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
0

Of course, the above-mentioned answer is correct but if you are using Windows API in C# to check the Print Job Status, you have to do a little bit more, specifically to get rid of nested if-then-else clauses. Here is a sample code:

namespace Sample
{
    class UsingNativeAPIs
    {
        private const int JOB_STATUS_RETAINED = 8192;          
        private const int JOB_STATUS_COMPLETE = 4096;          
        private const int JOB_STATUS_RESTART = 2048;           
        private const int JOB_STATUS_USER_INTERVENTION = 1024;
        private const int JOB_STATUS_BLOCKED_DEVQ = 512;       
        private const int JOB_STATUS_DELETED = 256;            
        private const int JOB_STATUS_PRINTED = 128;            
        private const int JOB_STATUS_PAPEROUT = 64;                            
        private const int JOB_STATUS_OFFLINE = 32;             
        private const int JOB_STATUS_PRINTING = 16;     
        private const int JOB_STATUS_SPOOLING = 8;      
        private const int JOB_STATUS_DELETING = 4;                       
        private const int JOB_STATUS_ERROR = 2;                
        private const int JOB_STATUS_PAUSED = 1;
                                                                       
        private Dictionary<int, string> jDict = new Dictionary<int, string>()
            {
                {JOB_STATUS_RETAINED, "Retained"},
                {JOB_STATUS_COMPLETE, "Complete"},
                {JOB_STATUS_RESTART, "restarted"},
                {JOB_STATUS_USER_INTERVENTION, "UserIntervention"},
                {JOB_STATUS_BLOCKED_DEVQ, "Blocked"},
                {JOB_STATUS_DELETED, "Deleted"},
                {JOB_STATUS_PRINTED, "Printed"},
                {JOB_STATUS_PAPEROUT, "PaperOut"},
                {JOB_STATUS_OFFLINE, "Offline"},
                {JOB_STATUS_PRINTING, "Printing"},
                {JOB_STATUS_SPOOLING, "Spooling"},
                {JOB_STATUS_DELETING, "Deleting"},
                {JOB_STATUS_ERROR, "Error"},
                {JOB_STATUS_PAUSED, "Paused"},                      
            };

        private StringBuilder Get_Status(JOB_INFO_2W[] jobInfos, /*... your parameters, if any*/)
        {
            string jobStatus;
            StringBuilder sb;
            foreach (var job in jobInfos)
            {
                jobStatus = "";
                foreach (var item in jdict)
                {
                    if ((job.Status & item.Key) == item.Key)
                    {
                        jobStatus = item.Value;
                        break; // or just ignore this line if you look for all combined statuses
                    }
                }
                sb.Append("Status = " + jobStatus);
            }

            return sb;
        }
    }
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
amirfg
  • 272
  • 2
  • 6
  • 21