如何从当前printjob中读取双面打印和每张页数设置 [英] How to read Duplex and Pages per Sheet setting from current printjob

查看:480
本文介绍了如何从当前printjob中读取双面打印和每张页数设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我的挑战是拦截所有的printjobs。 Foreach的工作我想确定,是否使用了双面打印或每页X页功能。



我正在尝试使用C#和.net解决方案框架。

我已经找到了几个解决方案,通过使用WMI和System.Printing命名空间来获取有关printjob(id,页数,用户,颜色......)的更多一般信息。但我无法检索Duplex或PagerPerSheet信息流。



我最有希望的方法是以下(另请参阅我附带的代码示例):

- 附加到Printjobs的WMI Creation事件以获得通知

- 从事件属性中读取jobid

- 使用System.Printing命名空间从特定的printqueue获取printjob />
- 暂停printjob并尝试使用其属性



当我发现附加到PrintTicket(ReachFramework)的类时,我已经认为它已经解决了PrintQueue,提供Duplexing和PagesPerSheet属性。

不幸的是,我必须发现那些属性 - 违背我的期望 - 并不代表我提交的printjob的状态。



我误解了PrintQueue.CurrentJobSettings的功能还是错误地使用了它?



我试图计算这种情况持续了好几天但是开始了如果有人能指出我的错误或提供其他解决方案来拦截这些信息,我会非常感激。



提前致谢,

Hans-Peter



使用系统; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Management;使用System.Printing
;
使用System.Threading;
命名空间PrintJobInterceptor
{
//将其粘贴到控制台应用程序中
//请添加对ReachFramework,System.Management和System.Printing的引用
class Program
{
static void Main(string [] args)
{
//初始化事件观察者并订阅事件
ManagementEventWatcher watcher = new ManagementEventWatcher();
string machineName = System.Environment.MachineName;
string scopeName = String.Format(@\\ {0} \root \ CYIM2,machineName);
watcher.Scope = new ManagementScope(scopeName);
//附加到printjobs的创建事件
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)
{
//关于printjob的一些信息可以在event args中找到
PrintEventInfo(e);
int id = GetPrintJobId(e);
LocalPrintServer s = new LocalPrintServer();
// XPS打印机硬编码在这里。请用您的打印机名称替换。
PrintQueue q = s.GetPrintQueue(Microsoft XPS Document Writer);
PrintSystemJobInfo printJob = null;
尝试
{
printJob = q.GetJob(id);
//在从队列中检索printjob之前不要输入断点。否则GetJob方法中会有一个excheption
//如果你不暂停工作,小工作就会快速完成。特别是在调试时。
printJob.Pause();
//如果您没有等待一段时间并更新信息,并非所有信息都是正确的(例如总页数,...)
Thread.Sleep(1000);
printJob.Refresh();
printJob.HostingPrintQueue.Refresh();
//当我看一下各种printteckets(特别是当前的)时,我希望它能保持正确的设置
Console.WriteLine(每张页数:+ printJob.HostingPrintQueue。 CurrentJobSettings.CurrentPrintTicket.PagesPerSheet);
//我知道xps只能进行手动双面打印 - 您可以尝试使用其他打印机
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 。恢复();
}
}
static void PrintEventInfo(EventArrivedEventArgs e)
{
foreach(e.NewEvent.Properties中的PropertyData数据)
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(e.NewEvent.Properties:+ data.Name +#+ data.Value);

if(data.Value是ManagementBaseObject)
{
foreach((ManagementBaseObject)中的PropertyData限定符data.Value).Properties)
{
Console.WriteLine( - + qualifier.Name +#+ qualifier.Value);
}
}
其他
{
int i = 0;
}
}
}
static int GetPrintJobId(EventArrivedEventArgs e)
{
uint jobId =(uint)((ManagementBaseObject)e.NewEvent.Properties [ TargetInstance]值)的.properties [ 的JobId]值。;
int intId =(int)jobId;
return intId;
}
}
}

解决方案

你有没有找到解决这个问题的方法?

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
{
    //paste this into an console application
    //please add references to ReachFramework, System.Management and System.Printing
    class Program
    {
        static void Main(string[] args)
        {
            // Initialize an event watcher and subscribe to events             
            ManagementEventWatcher watcher = new ManagementEventWatcher();
            string machineName = System.Environment.MachineName;
            string scopeName = String.Format(@"\\{0}\root\CIMV2", machineName);
            watcher.Scope = new ManagementScope(scopeName);
            // attach to creation events of printjobs
            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)
        {
            //some information about printjob can be found in event args
            PrintEventInfo(e);
            int id = GetPrintJobId(e);
            LocalPrintServer s = new LocalPrintServer();
            //XPS printer hardcoded here. Pleade replace with your printer's name.
            PrintQueue q = s.GetPrintQueue("Microsoft XPS Document Writer");
            PrintSystemJobInfo printJob = null;
            try
            {                
                printJob = q.GetJob(id);
                //do not enter a breakpoint until the printjob was retrieved from queue. otherwise there will be an excheption in the GetJob method
                //if you don't pause the job, small jobs will be gone to fast. espacially while debugging.
                printJob.Pause();
                //if you don't wait some time and update the info, not all information will be correct (e.g. total page count ,...)
                Thread.Sleep(1000);
                printJob.Refresh();
                printJob.HostingPrintQueue.Refresh();
                //when i take a look at the various printteckets (especially the current one), i would expect it to hold the correct settings
                Console.WriteLine("Pages Per Sheet: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.PagesPerSheet);
                //i know xps is only capable of manual duplexing - you can try it with some other printer
                Console.WriteLine("Duplex: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.Duplexing);
                Console.WriteLine("Copy Count: " + printJob.HostingPrintQueue.CurrentJobSettings.CurrentPrintTicket.CopyCount);  
              
                //unfortunately most of the infos are allways null, although i specified them in the print dialog.
            }
            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;
        }
    }
}

解决方案

Did you ever figure out a solution to this problem?


这篇关于如何从当前printjob中读取双面打印和每张页数设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆