| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- 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 List<Bitmap> bitmaps = new List<Bitmap>();
- static DataTable dt = new DataTable();
-
- public static void zplprint(string str, string PrinterName)
- {
- Dictionary<string, object> data = PrintHelper.ToDictionary(str);
- //获取所有的打印格式数据
- if (data.ContainsKey("data"))
- {
- string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
- List<string> PrintList = new List<string>();
- 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<string, object> 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<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>>();
- 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
- {
- 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;
- }
- try
- {
- Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".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)
- {
- 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")
- {
- 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);
- }
- }
- }
- foreach (var item in barcode)
- {
- DataRow dr = dt.NewRow();
- 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")
- {
- 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("\"", "");
- }
- }
- dt.Rows.Add(dr);
- }
- try
- {
- Report.PrintSettings.ShowDialog = false;
- 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();
- }
- }
- catch(Exception ex)
- {
- MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
- }
- dt.Clear();
- }
- }
- }
|