Check If Print has been done successfully

11,505

Here is a msdn description for PrintSystemJobInfo.JobStatus

https://msdn.microsoft.com/en-us/library/system.printing.printsystemjobinfo.jobstatus(v=vs.110).aspx

I tried the following code and saw print status.

 private void brnPrint_Click(object sender, EventArgs e)
        {
            var files = Directory.GetFiles(@"D:\Folder", "*.jpg");

            foreach (var i in files)
            {
                var objPrintDoc = new PrintDocument();
                objPrintDoc.PrintPage += (obj, eve) =>
                    {
                        Image img = Image.FromFile(i);
                        Point loc = new Point(100, 100);
                        eve.Graphics.DrawImage(img, loc);
                    };

                objPrintDoc.Print();
                PrintServer myPrintServer = new PrintServer(@"\\ComputerName");
                PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();                   
                try
                {
                    foreach (PrintQueue pq in myPrintQueues)
                    {
                        pq.Refresh();
                        PrintJobInfoCollection pCollection = pq.GetPrintJobInfoCollection();
                        foreach (PrintSystemJobInfo job in pCollection)
                        {
                            listBox1.Items.Add(pq.Name);
                            SpotTroubleUsingJobAttributes(job);
                        }

                    }
                }
                catch (Exception)
                {
                     //throw;
                }
            }
        }

        public void SpotTroubleUsingJobAttributes(PrintSystemJobInfo theJob)
        {
            if ((theJob.JobStatus & PrintJobStatus.Blocked) == PrintJobStatus.Blocked)
            {
                listBox1.Items.Add("The job is blocked.");
            }
            if (((theJob.JobStatus & PrintJobStatus.Completed) == PrintJobStatus.Completed)
                ||
                ((theJob.JobStatus & PrintJobStatus.Printed) == PrintJobStatus.Printed))
            {
                listBox1.Items.Add(
                    "The job has finished. Have user recheck all output bins and be sure the correct printer is being checked.");
            }
            if (((theJob.JobStatus & PrintJobStatus.Deleted) == PrintJobStatus.Deleted)
                ||
                ((theJob.JobStatus & PrintJobStatus.Deleting) == PrintJobStatus.Deleting))
            {
                listBox1.Items.Add(
                    "The user or someone with administration rights to the queue has deleted the job. It must be resubmitted.");
            }
            if ((theJob.JobStatus & PrintJobStatus.Error) == PrintJobStatus.Error)
            {
                listBox1.Items.Add("The job has errored.");
            }
            if ((theJob.JobStatus & PrintJobStatus.Offline) == PrintJobStatus.Offline)
            {
                listBox1.Items.Add("The printer is offline. Have user put it online with printer front panel.");
            }
            if ((theJob.JobStatus & PrintJobStatus.PaperOut) == PrintJobStatus.PaperOut)
            {
                listBox1.Items.Add("The printer is out of paper of the size required by the job. Have user add paper.");
            }

            //if (((theJob.JobStatus & PrintJobStatus.Paused) == PrintJobStatus.Paused)
            //    ||
            //    ((theJob.HostingPrintQueue.QueueStatus & PrintQueueStatus.Paused) == PrintQueueStatus.Paused))
            //{
            //    HandlePausedJob(theJob);
            //    //HandlePausedJob is defined in the complete example.
            //}

            if ((theJob.JobStatus & PrintJobStatus.Printing) == PrintJobStatus.Printing)
            {
                listBox1.Items.Add("The job is printing now.");
            }
            if ((theJob.JobStatus & PrintJobStatus.Spooling) == PrintJobStatus.Spooling)
            {
                listBox1.Items.Add("The job is spooling now.");
            }
            if ((theJob.JobStatus & PrintJobStatus.UserIntervention) == PrintJobStatus.UserIntervention)
            {
                listBox1.Items.Add("The printer needs human intervention.");
            }

        }
Share:
11,505

Related videos on Youtube

User5590
Author by

User5590

Updated on September 14, 2022

Comments

  • User5590
    User5590 over 1 year

    I am developing windowform application in c#.

    In my app, i have written below code to get all image from local machine and print it.

      files = Directory.GetFiles(@"C:\temp", "*.jpeg");
    
            foreach (var i in files)
            {
                var objPrintDoc = new PrintDocument();
                objPrintDoc.PrintPage += (obj, eve) =>
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(i);
                    Point loc = new Point(100, 100);
                    eve.Graphics.DrawImage(img, loc);
                };
                objPrintDoc.Print();
            }
    

    Now i want to check if that print has been done successfully or not and then i want to delete temp folder which i have created manually to store images.

    I have tried below code, but it didn't work for me.

            PrintServer myPrintServer;                    
            PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
            String printQueueNames = "My Print Queues:\n\n";
            foreach (PrintQueue pq in myPrintQueues)
            {
                printQueueNames += "\t" + pq.Name + "\n";
            }
    
    • and_the_rand
      and_the_rand over 10 years
      but it didn't work for me -- how does it not work?
    • user1703401
      user1703401 over 10 years
      If you didn't get any exceptions from your code then you can safely assume that the printer spooler got the job and is responsible to get it to the printer. Checking the print queue is not reliable. There are good odds that you'll never see the job because it was dispatched quickly. Nor is there any point in helping, the user already gets a notification from the spooler if there was any problem getting the document printed.