用PrintDocument写了个打印程序。不知道该如何获取打印机打印过程中是否出错(如卡纸)。是否正常完成打印任务,还望各位大神给点意见。用PrintQueue,PrintSystemJobInfo不知道在哪获取。在EndPrint事件中获取不到。
PrintQueue总是在打印任务栏里面一闪而过。追踪不了,不是异步的,也不知道该等多久PrintQueue的Status发生了变化。我们总是使用工业打印机的,一般软件层面解决不了还有硬件的层面。比如打印机实际做了打印动作会对外输出一个电信号,这样就知道了。
先贴上点不相干的代码,以供参考:
/// <summary> /// 取消打印任务 /// </summary> /// <param name="printJobID">任务ID</param> /// <returns></returns> protected bool CancelPrintJob(int printJobID) { bool isActionPerformed = false; string searchQuery; String jobName; char[] splitArr; int prntJobID; ManagementObjectSearcher searchPrintJobs; ManagementObjectCollection prntJobCollection; try { searchQuery = "SELECT * FROM Win32_PrintJob"; searchPrintJobs = new ManagementObjectSearcher(searchQuery); prntJobCollection = searchPrintJobs.Get(); foreach (ManagementObject prntJob in prntJobCollection) { jobName = prntJob.Properties["Name"].Value.ToString(); splitArr = new char[1]; splitArr[0] = Convert.ToChar(","); prntJobID = Convert.ToInt32(jobName.Split(splitArr)[1]); if (prntJobID == printJobID) { prntJob.Delete(); isActionPerformed = true; break; } } return isActionPerformed; } catch (Exception sysException) { Log.Error(sysException); return false; } } /// <summary> /// 获取本地打印机的打印任务集合 /// </summary> /// <param name="PrinterName"></param> /// <returns></returns> protected Dictionary<string, int> GetPrintQueue(string PrinterName) { Dictionary<string, int> tempDict = new Dictionary<string, int>(); LocalPrintServer pr = new LocalPrintServer(); pr.Refresh(); EnumeratedPrintQueueTypes[] enumerationFlags = { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections, }; foreach (PrintQueue pq in pr.GetPrintQueues(enumerationFlags)) { if (pq.Name == PrinterName && pq.NumberOfJobs > 0) { var jobs = pq.GetPrintJobInfoCollection(); foreach (var job in jobs) { tempDict.Add(job.Name +"_"+ job.JobIdentifier.ToString(), job.JobIdentifier); } } } return tempDict; }
这些都试过了!未能解决。想问一下,如果通过USB或者串口是否能获取到,查了一些资料,但是写不出代码,大神有什么例子可推荐么
再有PrintQueue和PrintSystemJobInfo类里都有一些打印状态,这些状态是指打印队列里的状态么。这些状态也没获取到
printdocument搞不定,也许可以借助win32 api