0

我从打印机队列(.NET)中的监视器作业中下载了这段代码, 现在我想知道打印作业的总页数。我看到了评论并添加了以下代码:

if ( data[ i ].Field == (ushort)PRINTERJOBNOTIFICATIONTYPES.JOB_NOTIFY_FIELD_TOTAL_PAGES &&
    data[ i ].Type == (ushort)PRINTERNOTIFICATIONTYPES.PRINTER_NOTIFY_TYPE )
{
    uint pagesCount = data[ i ].NotifyData.adwData[ 0 ];
}

但我总是得到错误的页面。我尝试使用“PrinterQueueWatch”,总页数始终为 0。

4

1 回答 1

0

您始终可以使用Win32_PrintJob类来获取打印作业,并且可以从该打印作业中获取总页数。请记住,在打印作业状态从假脱机转移到打印(我在自己的代码中经历过)之后,总页数才可用。

在我的例子中,我使用 WMI 来监视打印作业,并在EventArrived观察者的事件处理程序上,我添加了一些代码行来获取打印作业的总页数。

尝试这个:

public void PrintJobChange(object sender, EventArrivedEventArgs e)
{
    ManagementBaseObject objProps = (ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value;

    ...

    string jobStatus = (string)objProps["JobStatus"];
    uint totalPages = ((uint)objProps["TotalPages"]);

    Console.WriteLine("Status: " + jobStatus + ", Total Pages: " + totalPages);
}

然后PrintJobChange订阅EventArrived处理程序:

public void MonitorPrintJobs()
{
    string queryClassName = "__InstanceOperationEvent";
    string queryCond = "TargetInstance ISA 'Win32_PrintJob'";
    TimeSpan queryTimeSpan = new TimeSpan(1);

    try
    {
        WqlEventQuery eventQuery = new WqlEventQuery(queryClassName, queryTimeSpan, queryCond);
        ManagementEventWatcher watcher = new ManagementEventWatcher(eventQuery);

        if (PrintJobChange != null)
        {
            watcher.EventArrived += PrintJobChange;
        }

        watcher.Start();
    }
    catch (Exception ex)
    {
        string msg = string.Format("Error monitoring print jobs. Exception {0} Trace {1}.",
        ex.Message, ex.StackTrace);
        MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
    }
}

仅向我的虚拟打印机打印 1 页的控制台输出:

状态:假脱机,总页数:0
状态:打印,总页数:1
于 2017-01-18T08:35:51.490 回答