PrintHandler.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using FastReport;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.RegularExpressions;
  9. using System.Windows.Forms;
  10. using System.Drawing.Printing;
  11. namespace UAS_PRINT
  12. {
  13. class PrintHandler
  14. {
  15. //打印图片集合
  16. static List<Bitmap> bitmaps = new List<Bitmap>();
  17. static DataTable dt = new DataTable();
  18. public static void zplprint(string str, string PrinterName)
  19. {
  20. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  21. //获取所有的打印格式数据
  22. if (data.ContainsKey("data"))
  23. {
  24. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
  25. List<string> PrintList = new List<string>();
  26. int PrintTime = Regex.Matches(PrintCode, "XA").Count;
  27. for (int i = 0; i < PrintTime; i++)
  28. {
  29. PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
  30. PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
  31. }
  32. for (int i = 0; i < PrintList.Count; i++)
  33. {
  34. PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
  35. }
  36. data.Clear();
  37. }
  38. else if (data.ContainsKey("exceptionInfo"))
  39. {
  40. //string PrintCode = data["exceptionInfo"].ToString();
  41. //MessageBox.Show(PrintCode);
  42. }
  43. }
  44. public static void zplprint(string str)
  45. {
  46. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  47. //获取所有的打印格式数据
  48. if (data.ContainsKey("data"))
  49. {
  50. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
  51. string PrinterName = data["uid"].ToString().Replace("\"", "");
  52. if (!PrintCode.StartsWith("^XA"))
  53. {
  54. return;
  55. }
  56. Console.WriteLine(PrintCode);
  57. PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
  58. PrintDocument pd = new PrintDocument();
  59. pd.PrinterSettings.PrinterName = PrinterName;
  60. data.Clear();
  61. }
  62. else if (data.ContainsKey("exceptionInfo"))
  63. {
  64. }
  65. }
  66. public static void vendorZplPrint( string str)
  67. {
  68. Report Report = new Report();
  69. string chooseprintername;
  70. string label_type = "";
  71. string label_name = "";
  72. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  73. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  74. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  75. try
  76. {
  77. parameter = (List<Dictionary<string, object>>)data["parameter"];
  78. barcode = (List<Dictionary<string, object>>)data["barcode"];
  79. }
  80. catch
  81. {
  82. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  83. return;
  84. }
  85. try
  86. {
  87. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  88. }
  89. catch
  90. {
  91. MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示");
  92. return;
  93. }
  94. try
  95. {
  96. label_type = ((string)data["label_type"]).Replace("\"", "");
  97. }
  98. catch
  99. {
  100. MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示");
  101. return;
  102. }
  103. try
  104. {
  105. label_name = ((string)data["label_name"]).Replace("\"", "");
  106. }
  107. catch
  108. {
  109. MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
  110. return;
  111. }
  112. try
  113. {
  114. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");
  115. }
  116. catch
  117. {
  118. MessageBox.Show("未找到可使用的标签文件");
  119. return;
  120. }
  121. int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
  122. int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
  123. foreach (var item in barcode)
  124. {
  125. foreach (var itemc in item)
  126. {
  127. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  128. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  129. {
  130. string itemstr = "";
  131. if (itemc.Value != null)
  132. {
  133. itemstr = itemc.Value.ToString();
  134. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  135. {
  136. itemstr = itemstr.Remove(0, 2);
  137. }
  138. if (itemstr.StartsWith("\""))
  139. {
  140. itemstr = itemstr.Remove(0, 1);
  141. }
  142. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  143. {
  144. itemstr = itemstr.Remove(itemstr.Length - 2);
  145. }
  146. if (itemstr.EndsWith("\""))
  147. {
  148. itemstr = itemstr.Remove(itemstr.Length - 1);
  149. }
  150. if (itemstr.Contains("\\\""))
  151. {
  152. itemstr = itemstr.Replace("\\\"", "\"");
  153. }
  154. }
  155. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  156. if (!dt.Columns.Contains(itemc.Key))
  157. dt.Columns.Add(DC);
  158. }
  159. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  160. {
  161. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  162. if (!dt.Columns.Contains(itemc.Key))
  163. dt.Columns.Add(DC);
  164. }
  165. }
  166. }
  167. foreach (var item in barcode)
  168. {
  169. DataRow dr = dt.NewRow();
  170. foreach (var itemc in item)
  171. {
  172. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  173. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  174. {
  175. string itemstr = "";
  176. if (itemc.Value != null)
  177. {
  178. itemstr = itemc.Value.ToString();
  179. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  180. {
  181. itemstr = itemstr.Remove(0, 2);
  182. }
  183. if (itemstr.StartsWith("\""))
  184. {
  185. itemstr = itemstr.Remove(0, 1);
  186. }
  187. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  188. {
  189. itemstr = itemstr.Remove(itemstr.Length - 2);
  190. }
  191. if (itemstr.EndsWith("\""))
  192. {
  193. itemstr = itemstr.Remove(itemstr.Length - 1);
  194. }
  195. if (itemstr.Contains("\\\""))
  196. {
  197. itemstr = itemstr.Replace("\\\"", "\"");
  198. Console.WriteLine(itemstr);
  199. }
  200. }
  201. dr[itemc.Key] = itemstr;
  202. }
  203. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  204. {
  205. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  206. }
  207. }
  208. dt.Rows.Add(dr);
  209. }
  210. try
  211. {
  212. Report.RegisterData(dt, label_type);
  213. Report.GetDataSource(label_type).Enabled = true;
  214. Report.PrintSettings.ShowDialog = false;
  215. Report.PrintSettings.Printer = chooseprintername;
  216. Report.Print();
  217. }
  218. catch
  219. {
  220. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确");
  221. }
  222. dt.Clear();
  223. }
  224. }
  225. }