从打印作业中获取总页数

时间:2016-07-28 19:54:26

标签: c# printing printqueuewatch

我从Monitor jobs in a printer queue (.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。

1 个答案:

答案 0 :(得分:0)

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

在我的情况下,我使用WMI来监视打印作业,并且在观察者的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); } } 处理程序:

window.MUSIC_DATA =

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

Status: Spooling, Total Pages: 0
Status: Printing, Total Pages: 1