Quellcode durchsuchen

标签打印 打印对象复用模式【PrintDocument】

chenxy vor 3 Monaten
Ursprung
Commit
a365bd842d
1 geänderte Dateien mit 148 neuen und 3 gelöschten Zeilen
  1. 148 3
      DK.Service/BarcodePrintService/InvoiceLayoutPrinter.cs

+ 148 - 3
DK.Service/BarcodePrintService/InvoiceLayoutPrinter.cs

@@ -19,7 +19,7 @@ namespace Dongke.WinForm.Controls.InvoiceLayout
         // TODO 加密保存 解密读取
         private static byte[] KEY = new byte[] { 106, 240, 243, 74, 29, 163, 145, 29, 39, 65, 118, 96, 247, 237, 40, 121, 116, 10, 14, 50, 139, 117, 244, 163, 193, 190, 184, 66, 8, 185, 78, 73 };
         private static byte[] IV = new byte[] { 26, 122, 25, 28, 97, 211, 152, 32, 223, 110, 120, 242, 170, 35, 96, 93 };
-        //private static ICryptoTransform _decryptor = null;
+        private static ICryptoTransform _decryptor = null;
         #endregion 保存、读取相关
         #endregion
 
@@ -50,8 +50,8 @@ namespace Dongke.WinForm.Controls.InvoiceLayout
         #region 构造函数
         static InvoiceLayoutPrinter()
         {
-            //Rijndael age = Rijndael.Create();
-            //_decryptor = age.CreateDecryptor(KEY, IV);
+            Rijndael age = Rijndael.Create();
+            _decryptor = age.CreateDecryptor(KEY, IV);
         }
 
         private InvoiceLayoutPrinter()
@@ -111,6 +111,10 @@ namespace Dongke.WinForm.Controls.InvoiceLayout
 
         public static void Print(string printerName, short copies, byte[] layout, DataRow data, DataTable detail, int printDirectionKind = 0)
         {
+            // 复用打印对象模式
+            PrintReuse(printerName, copies, layout, data, detail, printDirectionKind);
+            return;
+
             //Logger.Debug("打印开始0");
             if (layout == null || layout.Length == 0)
             {
@@ -169,7 +173,77 @@ namespace Dongke.WinForm.Controls.InvoiceLayout
             }
         }
 
+        public static Dictionary<string, InvoiceLayoutPrinter> PPSS = new Dictionary<string, InvoiceLayoutPrinter>();
+        public static void PrintReuse(string printerName, short copies, byte[] layout, DataRow data, DataTable detail, int printDirectionKind = 0)
+        {
+            //Logger.Debug("打印开始0");
+            if (layout == null || layout.Length == 0)
+            {
+                throw new System.ArgumentNullException("layout");
+            }
+            if (data == null)
+            {
+                throw new System.ArgumentNullException("data");
+            }
+            //if (detail == null || detail.Rows.Count == 0)
+            //{
+            //    throw new System.ArgumentNullException("detail");
+            //}
+            string ex_pp = "异常位置:10";
+            try
+            {
+                //Logger.Debug("打印开始1");
+                InvoiceLayoutPrinter printer;
+                lock (PPSS)
+                {
+                    if (PPSS.ContainsKey(printerName))
+                    {
+                        ex_pp = "异常位置:11";
+                        printer = PPSS[printerName];
+                    }
+                    else
+                    {
+                        ex_pp = "异常位置:12";
+                        printer = new InvoiceLayoutPrinter();
+
+                        ex_pp = "异常位置:12-1";
+                        printer._printDocument = new PrintDocument();
+                        printer._printDocument.DocumentName = "LayoutDocument";
+                        //printer._printDocument.BeginPrint += new PrintEventHandler(printer.printDocument_BeginPrint);
+                        printer._printDocument.PrintPage += new PrintPageEventHandler(printer.PrintDocument_PrintPage);
+
+                        ex_pp = "异常位置:12-2";
+                        printer._printDocument.PrinterSettings.PrinterName = printerName;
+                        PPSS.Add(printerName, printer);
+                    }
+                }
+
+                lock (printer)
+                {
+                    ex_pp = "异常位置:13";
+                    printer._printDirectionKind = printDirectionKind;
+                    printer.LayoutItems = new List<LayoutItem>();
+                    printer.ReadLayoutReuse(layout);
+                    printer._printData = data;
+                    printer._printGridData = detail;
+                    printer._printDocument.PrinterSettings.Copies = copies;
+                    //Logger.Debug("打印开始2");
+
+                    ex_pp = "异常位置:14";
+                    printer._printDocument.Print();
+                }
+
+                ex_pp = "异常位置:15";
+                //GC.Collect();
+            }
+            catch (Exception ex)
+            {
+                //throw ex;
+                throw new Exception(ex_pp, ex);
+            }
+        }
         #region 票据打印
+
         private void ReadLayout(byte[] layout)
         {
             try
@@ -241,6 +315,77 @@ namespace Dongke.WinForm.Controls.InvoiceLayout
                 throw ex;
             }
         }
+        private void ReadLayoutReuse(byte[] layout)
+        {
+            try
+            {
+                using (MemoryStream stream = new MemoryStream(layout))
+                {
+                    stream.Position = 0;
+                    // 反序列化
+                    BinaryFormatter formatter = new BinaryFormatter();
+
+                    // TODO 解密读取
+                    //if (CryptoStream)
+                    if (true)
+                    {
+                        using (CryptoStream cStream = new CryptoStream(stream, _decryptor, CryptoStreamMode.Read))
+                        //Rijndael age = Rijndael.Create();
+                        //using (CryptoStream cStream = new CryptoStream(stream, age.CreateDecryptor(KEY, IV), CryptoStreamMode.Read))
+                        {
+                            this.Property = (PropertyOfLayoutBox)formatter.Deserialize(cStream);
+                        }
+                    }
+                    else
+                    {
+                        this.Property = (PropertyOfLayoutBox)formatter.Deserialize(stream);
+                    }
+                    //_lastItemRF = propertyOflayoutBox.LastItemRF;
+                    //_newItemID = propertyOflayoutBox.NewItemID;
+                    //_paperHeight = propertyOflayoutBox.PaperHeight;
+                    //_paperWidth = propertyOflayoutBox.PaperWidth;
+                    //if (_zoomType != ZoomType.Whole)
+                    //{
+                    //    ChangePaperSize();
+                    //}
+                    //else
+                    //{
+                    //    InitializeZoom();
+                    //}
+                    //paperArea.BackgroundImage = propertyOflayoutBox.BackgroundImage;
+                    //_backgroundImageName = propertyOflayoutBox.BackgroundImageName;
+                    //_isPrintBackground = propertyOflayoutBox.PrintBackground;
+                    //_isPrintAreaVisible = propertyOflayoutBox.PrintAreaVisible;
+                    ArrayList items = this.Property.Items;
+
+                    if (items != null && 0 < items.Count)
+                    {
+                        foreach (LayoutItem item in items)
+                        {
+                            //item.Init(this, false);
+                            //item.Init();
+                            this.LayoutItems.Add(item);
+                            if (item.ItemType == ItemType.Grid)
+                            {
+                                this.OneGridItem = item as GridItem;
+                                if (this.OneGridItem.HeadRowsHeight == 0)
+                                {
+                                    this.OneGridItem.HeadRowsHeight = this.OneGridItem.RowsHeight;
+                                }
+                            }
+                        }
+                    }
+                    //paperArea.Select();
+                    //RefreshItemsBound();
+                    ////paperArea.Refresh();
+                    //return true;
+                }
+            }
+            catch (Exception ex)
+            {
+                throw ex;
+            }
+        }
 
         private List<Image> DrawImages()
         {