Hello.
My challenge is to intercept all printjobs. Foreach job I want to determine, whether the duplex or the X pages per sheet function was used.
I’m trying to solve the solution using C# and the .net Framework.
I already found a couple of solutions to get more general information about the printjob (id, page count, user, color, …) by using WMI and the System.Printing namespace. But I couldn’t retrieve the Duplex or PagerPerSheet information jet.
My most promising approach was the following (see also my attached code sample):
- Attach to WMI Creation event for Printjobs to get notified
- Read jobid from event properties
- Use System.Printing namespace to get printjob from specific printqueue
- Pause printjob and try to use its properties
I already thought to have it solved, when I discovered the PrintTicket (ReachFramework) class attached to the PrintQueue, which offers the properties “Duplexing” and “PagesPerSheet”.
Unfortunately I had to discover that those Properties – against my expectations – do not represent the state of the printjob I submitted.
Am I misunderstanding the functionality of the PrintQueue.CurrentJobSettings or do I use it wrong?
I tried to figure this out for days but starting to spin in circles.
If anybody could point out my mistake or provide some other solution to intercept this information I would appreciate it very much.
Thanks in advance,
Hans-Peter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Printing;
using System.Threading;
namespace PrintJobInterceptor
{
class Program
{
static void Main(string[] args)
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
string machineName = System.Environment.MachineName;
string scopeName = String.Format(@"\\{0}\root\CIMV2", machineName);
watcher.Scope = new ManagementScope(scopeName);
string oQuery = "SELECT * FROM __InstanceCreationEvent WITHIN 0.01 WHERE TargetInstance ISA \"Win32_PrintJob\"";
watcher.Query = new EventQuery(oQuery);
watcher.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
watcher.Start();
Console.ReadLine();
}
static void watcher_EventArrived(object sender, EventArrivedEventArgs e)
{
PrintEventInfo(e);
int id = GetPrintJobId(e);
LocalPrintServer s = new LocalPrintServer();
PrintQueue q = s.GetPrintQueue("Microsoft XPS Document Writer");
PrintSystemJobInfo printJob = null;
try
{
printJob = q.GetJob(id);
printJob.Pause();
Thread.Sleep(1000);
printJob.Refresh();
printJob.HostingPrintQueue.Refresh();
Console.WriteLine("Pages Per Sheet: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.PagesPerSheet);
Console.WriteLine("Duplex: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.Duplexing);
Console.WriteLine("Copy Count: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.CopyCount);
}
catch (Exception ex)
{
}
finally
{
if (printJob != null)
printJob.Resume();
}
}
static void PrintEventInfo(EventArrivedEventArgs e)
{
foreach (PropertyData data in e.NewEvent.Properties)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("e.NewEvent.Properties: " + data.Name + " # " + data.Value);
if (data.Value is ManagementBaseObject)
{
foreach (PropertyData qualifier in ((ManagementBaseObject)data.Value).Properties)
{
Console.WriteLine("- " + qualifier.Name + " # " + qualifier.Value);
}
}
else
{
int i = 0;
}
}
}
static int GetPrintJobId(EventArrivedEventArgs e)
{
uint jobId = (uint)((ManagementBaseObject)e.NewEvent.Properties["TargetInstance"].Value).Properties["JobId"].Value;
int intId = (int)jobId;
return intId;
}
}
}