如何使用C#获取打印作业状态

时间:2011-08-05 10:31:48

标签: c# .net printing

我能够打印文档,但我不知道如何获得它的状态。我浏览了很多资源(MSDNLinks for checking Job Status),但无法找到答案。

我实际上想要从打印机确认文档是否已成功打印。此外,我也很感兴趣,如果我可以从打印机获得错误信号,就像纸张被卡住一样。

我有打印机发送的打印机名称和文档名称。有没有人在这方面做过一些研究,可以告诉我如何做到这一点?

3 个答案:

答案 0 :(得分:3)

网上有样品...谷歌“用c#发送PJL命令”(PJL代表打印机工作语言)

codeproject Reading Data Directly from the Printer是一篇很好的文章/样本,以

开头

答案 1 :(得分:3)

您可能可以使用WMI。它提供了多个printing-related classes,包括Win32_PrintJob

这是未经测试的,但是这样的事情应该让你开始:

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...
    }

答案 2 :(得分:0)

您也可以尝试此代码

//使用JobStatus属性的标志检查打印作业的可能故障状态

internal static void SpotTroubleUsingJobAttributes(PrintSystemJobInfo theJob)
{

if ((theJob.JobStatus & PrintJobStatus.Blocked) == PrintJobStatus.Blocked)
{
    Console.WriteLine("The job is blocked.");
}
if (((theJob.JobStatus & PrintJobStatus.Completed) == PrintJobStatus.Completed)
    || 
    ((theJob.JobStatus & PrintJobStatus.Printed) == PrintJobStatus.Printed))
{
    Console.WriteLine("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))
{
    Console.WriteLine("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)
{
    Console.WriteLine("The job has errored.");
}
if ((theJob.JobStatus & PrintJobStatus.Offline) == PrintJobStatus.Offline)
{
    Console.WriteLine("The printer is offline. Have user put it online with printer front panel.");
}
if ((theJob.JobStatus & PrintJobStatus.PaperOut) == PrintJobStatus.PaperOut)
{
    Console.WriteLine("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)
{
    Console.WriteLine("The job is printing now.");
}
if ((theJob.JobStatus & PrintJobStatus.Spooling) == PrintJobStatus.Spooling)
{
    Console.WriteLine("The job is spooling now.");
}
if ((theJob.JobStatus & PrintJobStatus.UserIntervention) == PrintJobStatus.UserIntervention)
{
    Console.WriteLine("The printer needs human intervention.");
}}
     //end SpotTroubleUsingJobAttributes
相关问题