Printer Print Queue

  1. 需求说明

    LZ在一个标签设计公司工作,公司于是为了方便客户决定开发一个标签打印系统,我们把它叫PrintCenter。当客户在平台下单以后,会产生一个PRO文件(压缩文件)。客户此时就可以登录到PrintCenter,输入订单号,就可以查询到对应的PRO文件,打开PRO文件,就会看到一行一行的产品信息。打印系统的需求就是当客户选择PRO文件中的产品之后,单击打印按钮,就会把产品打印出来,要支持以下功能

    • 暂停打印
    • 重启打印
    • 取消打印
    • 当打印失败时,在PrintCenter显示打印错误信息。当用户看到错误信息取消打印时,能准确记录已经成功打印过的产品数量
    • 支持打印队列的排队。如果两个PrintQueue对应的是同一个打印机,则同一时刻,只允许一个PrintQueue在打印,当其中一个printQueue暂停时,另一个排除的PrintQueue则进行打印。如果两个printQueue对应的是不同的打印机,则要支持同时打印。

       

  2. 经过分析以及测试之后,发现System.Printing.dll能满足项目需求,主要应用到如下类型:
    • PrinterServer:在打印服务器上管理打印队列。
    • PrintQueue:管理打印机与打印作业。
    • PrintSystemJobInfo:详细定义打印作业。
    • PrintJobStatus:指定打印队列中打印作业的状态。

       

  3. 代码实现
    1. PrintStatus:定义打印作业的状态
       1  [Flags]
      
       2     public enum PrintStatus
      
       3     {
      
       4         // Summary:
      
       5         //     The print job has no specified state.
      
       6         Pending = 1,
      
       7         //
      
       8         // Summary:
      
       9         //     The print job is paused.
      
      10         Paused = 2,
      
      11         //
      
      12         // Summary:
      
      13         //     The print job is in an error state.
      
      14         Error = 4,
      
      15         //
      
      16         // Summary:
      
      17         //     The print job is now printing.
      
      18         Printing = 8,
      
      19         //
      
      20         // Summary:
      
      21         //     The printer is offline.
      
      22         Offline = 16,
      
      23         //
      
      24         // Summary:
      
      25         //     The printer is out of the required paper size.
      
      26         PaperOut = 32,
      
      27         //
      
      28         // Summary:
      
      29         //     The print job printed.
      
      30         Printed = 64,
      
      31         //
      
      32         // Summary:
      
      33         //     The printer requires user action to fix an error condition.
      
      34         UserIntervention = 128,
      
      35     }
      View Code
    2. WindowsPrintQueue:判断不同的driver是否指向的是同一个物理打印机
       1 public class WindowsPrintQueue
      
       2     {
      
       3         /// <summary>
      
       4         /// whether the two printer is same model.
      
       5         /// </summary>
      
       6         /// <param name="printerName1"></param>
      
       7         /// <param name="printerName2"></param>
      
       8         /// <returns></returns>
      
       9         public bool IsSameModel(string printerName1,string printerName2) 
      
      10         {
      
      11             return GetPrinterModel(printerName1).Equals(GetPrinterModel(printerName2));
      
      12         }
      
      13 
      
      14         /// <summary>
      
      15         /// Return printer model
      
      16         /// </summary>
      
      17         /// <param name="printerName"></param>
      
      18         /// <returns></returns>
      
      19         public string GetPrinterModel(string printerName) 
      
      20         {
      
      21             string model = string.Empty;
      
      22             System.Printing.PrintQueue printQueue = GetPrintQueue(printerName);
      
      23             if (printQueue != null)
      
      24             {
      
      25                 //Get printer model
      
      26                 if (printQueue.Description.IndexOf(",") == printQueue.Description.LastIndexOf(","))
      
      27                 {
      
      28                     model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + 1, printQueue.Description.LastIndexOf(",") - printQueue.Description.IndexOf(",") - 1);
      
      29                 }
      
      30                 else
      
      31                 {
      
      32                     model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + 1);
      
      33                 }
      
      34             }
      
      35             return model;
      
      36         }
      
      37 
      
      38         /// <summary>
      
      39         /// Get Windows Print Queue via printer name
      
      40         /// </summary>
      
      41         /// <param name="printerName"></param>
      
      42         /// <returns></returns>
      
      43         public System.Printing.PrintQueue GetPrintQueue(string printerName)
      
      44         {
      
      45             System.Printing.PrintQueue printQueue = null;
      
      46             PrintServer server = new PrintServer(printerName);
      
      47             foreach (System.Printing.PrintQueue pq in server.GetPrintQueues())
      
      48             {
      
      49                 if (pq.FullName.Equals(printerName))
      
      50                 {
      
      51                     printQueue = pq;
      
      52                 }
      
      53             }
      
      54             return printQueue;
      
      55         }
      
      56 
      
      57         /// <summary>
      
      58         /// Get Windows Print Queue via printer name
      
      59         /// 如果两个printer指向的是同一个物理打印机,则如果copy1的printQueue已经打开,则发送到copy2的打印job也会添加到已经打开的copy1的printQueue中.
      
      60         /// </summary>
      
      61         /// <param name="printerName"></param>
      
      62         /// <returns></returns>
      
      63         public System.Printing.PrintQueue GetOpenedPrintQueueOfSameModel(string printerName) 
      
      64         {
      
      65             System.Printing.PrintQueue doorOpenedprintQueue = null;
      
      66             System.Printing.PrintQueue currentPrinterPrintQueue = null;
      
      67             PrintServer server = new PrintServer(printerName);
      
      68             foreach (System.Printing.PrintQueue pq in server.GetPrintQueues())
      
      69             {
      
      70                 if (pq.FullName.Equals(printerName))
      
      71                 {
      
      72                     currentPrinterPrintQueue = pq;
      
      73                 }
      
      74                 else
      
      75                 { 
      
      76                    if(IsSameModel(printerName,pq.FullName))
      
      77                    {
      
      78                       if(pq.IsDoorOpened)
      
      79                       {
      
      80                           doorOpenedprintQueue = pq;
      
      81                           break;
      
      82                       }
      
      83                    }
      
      84                 }
      
      85             }
      
      86 
      
      87             if (doorOpenedprintQueue != null)
      
      88             {
      
      89                 return doorOpenedprintQueue;
      
      90             }
      
      91             else
      
      92             {
      
      93                 return currentPrinterPrintQueue;
      
      94             }
      
      95         }
      
      96     }
      View Code

 

你可能感兴趣的:(Queue)