using FastReport; using System; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Windows.Forms; using System.Drawing.Printing; namespace UAS_PRINT { class PrintHandler { //图片打印中间变量 static int imagecount = 0; static int index = 0; //打印图片集合 static List bitmaps = new List(); static DataTable dt = new DataTable("VENDORBARCODE_VIEW"); public static void zplprint(string str,string PrinterName) { Dictionary data = PrintHelper.ToDictionary(str); //获取所有的打印格式数据 if (data.ContainsKey("data")) { string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", ""); List PrintList = new List(); int PrintTime = Regex.Matches(PrintCode, "XA").Count; for (int i = 0; i < PrintTime; i++) { PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2)); PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2); } for (int i = 0; i < PrintList.Count; i++) { PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]); } data.Clear(); } else if (data.ContainsKey("exceptionInfo")) { //string PrintCode = data["exceptionInfo"].ToString(); //MessageBox.Show(PrintCode); } } public static void zplprint(string str) { Dictionary data = PrintHelper.ToDictionary(str); //获取所有的打印格式数据 if (data.ContainsKey("data")) { string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n",""); string PrinterName = data["uid"].ToString().Replace("\"", ""); if (!PrintCode.StartsWith("^XA")) { return; } PrintHelper.SendStringToPrinter(PrinterName, PrintCode); PrintDocument pd = new PrintDocument(); pd.PrinterSettings.PrinterName = PrinterName; data.Clear(); } else if (data.ContainsKey("exceptionInfo")) { //string PrintCode = data["exceptionInfo"].ToString(); //MessageBox.Show(PrintCode); } } public static void vendorZplPrint( PrinterList print, string PrinterName,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(); Dictionary data = PrintHelper.ToDictionary(str); List> parameter = new List>(); List> barcode = new List>(); parameter = (List>)data["parameter"]; barcode = (List>)data["barcode"]; 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 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) { 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("\\\"", "\""); } } 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())); } } 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); } } } } bitmaps.Add(bit); } if (PRFR.Checked) { foreach (var item in barcode) { 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 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) { 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); } } dr[itemc.Key] = itemstr; } else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode") { dr[itemc.Key] = itemc.Value.ToString().Replace("\"", ""); } } dt.Rows.Add(dr); } } if (PRPIC.Checked) { 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.PrintSettings.ShowDialog = false; Report.PrintSettings.Printer = PrinterName; 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 { index = 0; e.HasMorePages = false; } } } }