检查打印是否成功完成

11

我正在使用C#开发Windows窗体应用程序。

在我的应用程序中,我编写了以下代码以从本地计算机获取所有图像并打印它们。

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

现在我想要检查该打印命令是否成功,然后删除我手动创建用于存储图像的临时文件夹。

我尝试了下面的代码,但它对我没有起作用。

        PrintServer myPrintServer;                    
        PrintQueueCollection myPrintQueues = myPrintServer.GetPrintQueues();
        String printQueueNames = "My Print Queues:\n\n";
        foreach (PrintQueue pq in myPrintQueues)
        {
            printQueueNames += "\t" + pq.Name + "\n";
        }

1
但对我来说它没有起作用 -- 它为什么不起作用? - Austin Salonen
@AustinSalonen 获取PrintQueueCollection,我的打印队列 = myPrintServer.GetPrintQueues(); - User5590
我所指的“how”是指“你期望得到什么,实际得到了什么?” - Austin Salonen
@AustinSalonen 我想要检查打印是否成功完成了? - User5590
那么打印打印队列的名称怎么会是“尝试”呢?在MSDN上阅读您正在使用的类。 - Austin Salonen
4
如果您的代码没有收到任何异常,那么您可以安全地假设打印机后台程序已经接收了任务并负责将其发送到打印机。检查打印队列是不可靠的。有很大的可能性您永远看不到任务,因为它已经迅速被派发。此外,帮助也毫无意义,如果打印文档存在任何问题,用户已经从后台程序得到通知。 - Hans Passant
1个回答

6
这是有关PrintSystemJobInfo.JobStatus的MSDN描述。

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

我尝试了以下代码并看到了打印状态。
 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.");
            }

        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接