您始终可以使用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