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; using UAS_PRINT.Properties; namespace UAS_PRINT { class PrintHandler { //打印图片集合 static List bitmaps = new List(); static DataTable dt = new DataTable(); static int code = 0; static string first = ""; static string last = ""; 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 SetFormCenter(Form form) { form.StartPosition = FormStartPosition.CenterParent; } 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")) { } } public static void vendorZplPrint( string str) { Report Report = new Report(); string chooseprintername; string label_type = ""; string label_name = ""; Dictionary data = PrintHelper.ToDictionary(str); List> parameter = new List>(); List> barcode = new List>(); try { parameter = (List>)data["parameter"]; barcode = (List>)data["barcode"]; } catch { MessageBox.Show("打印数据解析异常,请联系管理员进行处理"); return; } try { chooseprintername = ((string)data["chooseprintername"]).Replace("\"", ""); } catch { MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示"); return; } try { label_type = ((string)data["label_type"]).Replace("\"", ""); } catch { MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示"); return; } try { label_name = ((string)data["label_name"]).Replace("\"", ""); } catch { MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示"); return; } if (Settings.Default.ifuseftp) { ftpOperater frp = new ftpOperater(true); try { frp.Download(label_name + ".frx", DateTime.Now); } catch (Exception ex) { MessageBox.Show("标签文件更新失败" + ex.Message); } } try { Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx"); } catch(Exception ex) { MessageBox.Show("未找到可使用的标签文件"+ex.Message); return; } foreach (var item in barcode) { 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("\\\"", "\""); } } 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") { DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String")); if (!dt.Columns.Contains(itemc.Key)) dt.Columns.Add(DC); } } } code = 0; first = ""; last = ""; foreach (var item in barcode) { int number = Settings.Default.Printnum; for (int i = 0; i < number; i++) { DataRow dr = dt.NewRow(); 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("\\\"", "\""); } } dr[itemc.Key] = itemstr; } else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode") { dr[itemc.Key] = itemc.Value.ToString().Replace("\"", ""); if (code == 0) first = itemc.Value.ToString().Replace("\"", ""); if(code == barcode.Count -1) last = itemc.Value.ToString().Replace("\"", ""); } } dt.Rows.Add(dr); } code++; } try { string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString(); //string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,"+first+"到"+last+",是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString(); if (logout_confirm == "Yes") { Report.PrintSettings.ShowDialog = false; byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(chooseprintername); chooseprintername = Encoding.UTF8.GetString(buffer); Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\"); //foreach (DataRow dr in dt.Rows) //{ // DataTable dta = dt.Clone(); // dta.Rows.Add(dr.ItemArray); // Report.RegisterData(dta, label_type); // Report.GetDataSource(label_type).Enabled = true; // Report.Print(); //} Report.RegisterData(dt, label_type); Report.GetDataSource(label_type).Enabled = true; Report.Print(); } } catch(Exception ex) { MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message); } dt.Clear(); } // static LabelFormatDocument format; static BarTender.Format doc2; //public static void SinglePrint(string iCaller, BarTender.Application lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage) public static void SinglePrint(string str, BarTender.Application lbl) { string chooseprintername; string label_type = ""; string label_name = ""; Dictionary data = PrintHelper.ToDictionary(str); List> parameter = new List>(); List> barcode = new List>(); try { parameter = (List>)data["parameter"]; barcode = (List>)data["barcode"]; } catch { MessageBox.Show("打印数据解析异常,请联系管理员进行处理"); return; } try { chooseprintername = ((string)data["chooseprintername"]).Replace("\"", ""); } catch { MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示"); return; } try { label_type = ((string)data["label_type"]).Replace("\"", ""); } catch { MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示"); return; } try { label_name = ((string)data["label_name"]).Replace("\"", ""); } catch { MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示"); return; } doc2 = lbl.Formats.Open(System.AppDomain.CurrentDomain.BaseDirectory + label_name + ".btw"); if (doc2 == null) { MessageBox.Show("标签文件打开失败"); return ; } foreach (var item in barcode) { foreach (var itemc in item) { for (int k = 0; k < doc2.NamedSubStrings.Count; k++) { if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == itemc.Key.ToUpper()) { 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("\\\"", "\""); } } doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, itemstr); } } } // 同样标签的份数 doc2.PrintSetup.IdenticalCopiesOfLabel = Settings.Default.Printnum; // 序列标签数 doc2.PrintSetup.NumberSerializedLabels = 1; doc2.PrintSetup.Printer = chooseprintername; doc2.PrintOut(false, false); } doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges); //打印完毕 lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges); return ; } } }