Browse Source

UAS打印插件调整

Hcsy 7 years ago
parent
commit
8fa08ecc2f
3 changed files with 92 additions and 179 deletions
  1. 9 50
      UAS_PRINT/Form2.cs
  2. 1 1
      UAS_PRINT/ModeBusTCPServer.cs
  3. 82 128
      UAS_PRINT/PrintHandler.cs

+ 9 - 50
UAS_PRINT/Form2.cs

@@ -54,55 +54,6 @@ namespace UAS_PRINT
                     //获取Post过来的参数和数据
                     List<HttpListenerPostValue> lst = httppost.GetHttpListenerPostValue();
 
-                    string userName = "";
-                    string password = "";
-                    string suffix = "";
-                    string adType = "";
-
-                    //使用方法
-                    //foreach (var key in lst)
-                    //{
-                    //    if (key.type == 0)
-                    //    {
-                    //        string value = Encoding.UTF8.GetString(key.datas).Replace("\r\n", "");
-                    //        if (key.name == "username")
-                    //        {
-                    //            userName = value;
-                    //            Console.WriteLine(value);
-                    //        }
-                    //        if (key.name == "password")
-                    //        {
-                    //            password = value;
-                    //            Console.WriteLine(value);
-                    //        }
-                    //        if (key.name == "suffix")
-                    //        {
-                    //            suffix = value;
-                    //            Console.WriteLine(value);
-                    //        }
-                    //        if (key.name == "adtype")
-                    //        {
-                    //            adType = value;
-                    //            Console.WriteLine(value);
-                    //        }
-                    //    }
-                    //    if (key.type == 1)
-                    //    {
-                    //        string fileName = request.Request.QueryString["FileName"];
-                    //        if (!string.IsNullOrEmpty(fileName))
-                    //        {
-                    //            string filePath = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.ToString("yyMMdd_HHmmss_ffff") + Path.GetExtension(fileName).ToLower();
-                    //            if (key.name == "File")
-                    //            {
-                    //                FileStream fs = new FileStream(filePath, FileMode.Create);
-                    //                fs.Write(key.datas, 0, key.datas.Length);
-                    //                fs.Close();
-                    //                fs.Dispose();
-                    //            }
-                    //        }
-                    //    }
-                    //}
-                   
                         //Response
                         request.Response.StatusCode = 200;
                         request.Response.Headers.Add("Access-Control-Allow-Origin", "*");
@@ -124,7 +75,15 @@ namespace UAS_PRINT
                         }
                         memoryStream.Position = 0;
                         string req = System.Text.Encoding.UTF8.GetString(buffera);
-                        PrintHandler.zplprint(req);
+                        if (req.Contains("\"" + "chooseprintername" + "\""))
+                        {
+                            req = req.Remove(req.IndexOf("device"), req.IndexOf("data") - 2);
+                            PrintHandler.vendorZplPrint(req);
+                        }
+                        else
+                        {
+                            PrintHandler.zplprint(req);
+                        }
                     }
                     else if (requestContext.Request.Url.PathAndQuery == "/default?type=printer")
                     {

+ 1 - 1
UAS_PRINT/ModeBusTCPServer.cs

@@ -206,7 +206,7 @@ namespace UAS_PRINT
             else
             {
                 //print.browser.FocusedFrame.ExecuteJavaScriptAsync("document.getElementById('confirmPicturePrint').click();");
-                PrintHandler.vendorZplPrint(print, PrinterName, print.Printcontent);
+                //PrintHandler.vendorZplPrint(print, PrinterName, print.Printcontent);
             }
             print.Close();
         }

+ 82 - 128
UAS_PRINT/PrintHandler.cs

@@ -23,7 +23,7 @@ namespace UAS_PRINT
 
         static DataTable dt = new DataTable("VENDORBARCODE_VIEW");
 
-        public static void zplprint(string str,string PrinterName)
+        public static void zplprint(string str, string PrinterName)
         {
             Dictionary<string, object> data = PrintHelper.ToDictionary(str);
             //获取所有的打印格式数据
@@ -57,12 +57,13 @@ namespace UAS_PRINT
             //获取所有的打印格式数据
             if (data.ContainsKey("data"))
             {
-                string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n","");
+                string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
                 string PrinterName = data["uid"].ToString().Replace("\"", "");
                 if (!PrintCode.StartsWith("^XA"))
                 {
                     return;
                 }
+                Console.WriteLine(PrintCode);
                 PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
                 PrintDocument pd = new PrintDocument();
                 pd.PrinterSettings.PrinterName = PrinterName;
@@ -70,51 +71,50 @@ namespace UAS_PRINT
             }
             else if (data.ContainsKey("exceptionInfo"))
             {
-                //string PrintCode = data["exceptionInfo"].ToString();
-                //MessageBox.Show(PrintCode);
             }
         }
 
 
-        public static void vendorZplPrint( PrinterList print, string PrinterName,string str) {
+        public static void vendorZplPrint( string str)
+        {
             Report Report = new Report();
-            RadioButton PRFR = (RadioButton)print.Controls["PRFR"];
-            RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
-            if (PRFR.Checked)
-            {
-                try
-                {
-                    Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + "GD.frx");
-                }
-                catch
-                {
-                    MessageBox.Show("未找到可使用的标签文件");
-                    return;
-                }
-            }
-            bitmaps.Clear();
+            string chooseprintername;
             Dictionary<string, object> data = PrintHelper.ToDictionary(str);
             List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
             List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
-            parameter = (List<Dictionary<string, object>>)data["parameter"];
-            barcode = (List<Dictionary<string, object>>)data["barcode"];
+            try
+            {
+                parameter = (List<Dictionary<string, object>>)data["parameter"];
+                barcode = (List<Dictionary<string, object>>)data["barcode"];
+            }
+            catch
+            {
+                MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
+                return;
+            }
+            try
+            {
+                chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
+            }
+            catch
+            {
+                chooseprintername = "no";
+            }
+            try
+            {
+                Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + "库存条码打印.frx");
+            }
+            catch
+            {
+                MessageBox.Show("未找到可使用的标签文件");
+                return;
+            }
+
             int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
             int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
 
             foreach (var item in barcode)
             {
-                Bitmap bit = new Bitmap(la_width * 10, la_height * 4);
-
-
-                using (Graphics g = Graphics.FromImage(bit))
-                {
-                    //g.CompositingQuality = CompositingQuality.HighQuality;
-                    //g.SmoothingMode = SmoothingMode.HighQuality;
-                    //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
-                    ////定义一枝画笔
-                    //Pen pen = new Pen(Color.Black);
-                    ////用这个画笔在左上角画一个矩形
-                    //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
                     foreach (var itemc in item)
                     {
                         Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
@@ -145,122 +145,76 @@ namespace UAS_PRINT
                                     itemstr = itemstr.Replace("\\\"", "\"");
                                 }
                             }
-                            if (PRFR.Checked)
-                            {
-                                DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
-                                if (!dt.Columns.Contains(itemc.Key))
-                                    dt.Columns.Add(DC);
-                            }
-                            if (PRPIC.Checked)
-                            {
-                                PrintHelper.Printstring(itemc.Value != null ? itemstr : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()));
-                            }
+                            DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
+                            if (!dt.Columns.Contains(itemc.Key))
+                                dt.Columns.Add(DC);
                         }
                         else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
                         {
-                            if (PRFR.Checked)
-                            {
-                                DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
-                                if (!dt.Columns.Contains(itemc.Key))
-                                    dt.Columns.Add(DC);
-                            }
-                            if (PRPIC.Checked)
-                            {
-                                PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
-                                PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
-                            }
+                            DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
+                            if (!dt.Columns.Contains(itemc.Key))
+                                dt.Columns.Add(DC);
                         }
                     }
-                }
-                bitmaps.Add(bit);
+                
             }
-            if (PRFR.Checked)
+            foreach (var item in barcode)
             {
-                foreach (var item in barcode)
+                DataRow dr = dt.NewRow();
+                foreach (var itemc in item)
                 {
-                    DataRow dr = dt.NewRow();
-                    //g.CompositingQuality = CompositingQuality.HighQuality;
-                    //g.SmoothingMode = SmoothingMode.HighQuality;
-                    //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
-                    ////定义一枝画笔
-                    //Pen pen = new Pen(Color.Black);
-                    ////用这个画笔在左上角画一个矩形
-                    //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
-                    foreach (var itemc in item)
+                    Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
+                    if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
                     {
-                        Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
-                        if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
+                        string itemstr = "";
+                        if (itemc.Value != null)
                         {
-                            string itemstr = "";
-                            if (itemc.Value != null)
+                            itemstr = itemc.Value.ToString();
+                            while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
                             {
-                                itemstr = itemc.Value.ToString();
-                                while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
-                                {
-                                    itemstr = itemstr.Remove(0, 2);
-                                }
-                                if (itemstr.StartsWith("\""))
-                                {
-                                    itemstr = itemstr.Remove(0, 1);
-                                }
-                                while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
-                                {
-                                    itemstr = itemstr.Remove(itemstr.Length - 2);
-                                }
-                                if (itemstr.EndsWith("\""))
-                                {
-                                    itemstr = itemstr.Remove(itemstr.Length - 1);
-                                }
-                                if (itemstr.Contains("\\\""))
-                                {
-                                    itemstr = itemstr.Replace("\\\"", "\"");
-                                    Console.WriteLine(itemstr);
-                                }
+                                itemstr = itemstr.Remove(0, 2);
+                            }
+                            if (itemstr.StartsWith("\""))
+                            {
+                                itemstr = itemstr.Remove(0, 1);
+                            }
+                            while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
+                            {
+                                itemstr = itemstr.Remove(itemstr.Length - 2);
+                            }
+                            if (itemstr.EndsWith("\""))
+                            {
+                                itemstr = itemstr.Remove(itemstr.Length - 1);
+                            }
+                            if (itemstr.Contains("\\\""))
+                            {
+                                itemstr = itemstr.Replace("\\\"", "\"");
+                                Console.WriteLine(itemstr);
                             }
-                            dr[itemc.Key] = itemstr;
-                        }
-                        else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
-                        {
-                            dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
                         }
+                        dr[itemc.Key] = itemstr;
+                    }
+                    else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
+                    {
+                        dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
                     }
-                    dt.Rows.Add(dr);
                 }
+                dt.Rows.Add(dr);
             }
-            if (PRPIC.Checked)
+            try
             {
-                imagecount = bitmaps.Count;
-                ////设置打印机名称
-                print.PrintDoc.PrintPage += PrintPage;
-                print.PrintDoc.Print();
-            }
-            if (PRFR.Checked)
-            {
-                Report.RegisterData(dt, "VENDORBARCODE_VIEW");
-                Report.GetDataSource("VENDORBARCODE_VIEW").Enabled = true;
+               
+                Report.RegisterData(dt, "BARCODE_PRINT_VIEW");
+                Report.GetDataSource("BARCODE_PRINT_VIEW").Enabled = true;
                 Report.PrintSettings.ShowDialog = false;
-                Report.PrintSettings.Printer = PrinterName;
+                Report.PrintSettings.Printer = chooseprintername;
                 Report.Print();
-                dt.Clear();
-            }
-
-        }
-
-        private static void PrintPage(object sender, PrintPageEventArgs e)
-        {
-            Image i = bitmaps[index];
-            Graphics g = e.Graphics;
-            g.DrawImage(i, 4, 10, i.Width, i.Height);
-            if (index < imagecount - 1)
-            {
-                e.HasMorePages = true;
-                index++;
             }
-            else
+            catch
             {
-                index = 0;
-                e.HasMorePages = false;
+                MessageBox.Show("打印出现错误,请检查标签文件选择是否正确");
             }
+            dt.Clear();
         }
     }
 }