PrintHandler.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
  57. PrintDocument pd = new PrintDocument();
  58. pd.PrinterSettings.PrinterName = PrinterName;
  59. data.Clear();
  60. }
  61. else if (data.ContainsKey("exceptionInfo"))
  62. {
  63. }
  64. }
  65. public static void vendorZplPrint( string str)
  66. {
  67. Report Report = new Report();
  68. string chooseprintername;
  69. string label_type = "";
  70. string label_name = "";
  71. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  72. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  73. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  74. try
  75. {
  76. parameter = (List<Dictionary<string, object>>)data["parameter"];
  77. barcode = (List<Dictionary<string, object>>)data["barcode"];
  78. }
  79. catch
  80. {
  81. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  82. return;
  83. }
  84. try
  85. {
  86. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  87. }
  88. catch
  89. {
  90. MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示");
  91. return;
  92. }
  93. try
  94. {
  95. label_type = ((string)data["label_type"]).Replace("\"", "");
  96. }
  97. catch
  98. {
  99. MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示");
  100. return;
  101. }
  102. try
  103. {
  104. label_name = ((string)data["label_name"]).Replace("\"", "");
  105. }
  106. catch
  107. {
  108. MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
  109. return;
  110. }
  111. try
  112. {
  113. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");
  114. }
  115. catch
  116. {
  117. MessageBox.Show("未找到可使用的标签文件");
  118. return;
  119. }
  120. int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
  121. int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
  122. foreach (var item in barcode)
  123. {
  124. foreach (var itemc in item)
  125. {
  126. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  127. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  128. {
  129. string itemstr = "";
  130. if (itemc.Value != null)
  131. {
  132. itemstr = itemc.Value.ToString();
  133. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  134. {
  135. itemstr = itemstr.Remove(0, 2);
  136. }
  137. if (itemstr.StartsWith("\""))
  138. {
  139. itemstr = itemstr.Remove(0, 1);
  140. }
  141. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  142. {
  143. itemstr = itemstr.Remove(itemstr.Length - 2);
  144. }
  145. if (itemstr.EndsWith("\""))
  146. {
  147. itemstr = itemstr.Remove(itemstr.Length - 1);
  148. }
  149. if (itemstr.Contains("\\\""))
  150. {
  151. itemstr = itemstr.Replace("\\\"", "\"");
  152. }
  153. }
  154. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  155. if (!dt.Columns.Contains(itemc.Key))
  156. dt.Columns.Add(DC);
  157. }
  158. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  159. {
  160. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  161. if (!dt.Columns.Contains(itemc.Key))
  162. dt.Columns.Add(DC);
  163. }
  164. }
  165. }
  166. foreach (var item in barcode)
  167. {
  168. DataRow dr = dt.NewRow();
  169. foreach (var itemc in item)
  170. {
  171. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  172. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  173. {
  174. string itemstr = "";
  175. if (itemc.Value != null)
  176. {
  177. itemstr = itemc.Value.ToString();
  178. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  179. {
  180. itemstr = itemstr.Remove(0, 2);
  181. }
  182. if (itemstr.StartsWith("\""))
  183. {
  184. itemstr = itemstr.Remove(0, 1);
  185. }
  186. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  187. {
  188. itemstr = itemstr.Remove(itemstr.Length - 2);
  189. }
  190. if (itemstr.EndsWith("\""))
  191. {
  192. itemstr = itemstr.Remove(itemstr.Length - 1);
  193. }
  194. if (itemstr.Contains("\\\""))
  195. {
  196. itemstr = itemstr.Replace("\\\"", "\"");
  197. }
  198. }
  199. dr[itemc.Key] = itemstr;
  200. }
  201. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  202. {
  203. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  204. }
  205. }
  206. dt.Rows.Add(dr);
  207. }
  208. try
  209. {
  210. Report.RegisterData(dt, label_type);
  211. Report.GetDataSource(label_type).Enabled = true;
  212. Report.PrintSettings.ShowDialog = false;
  213. Report.PrintSettings.Printer = chooseprintername;
  214. Report.Print();
  215. }
  216. catch
  217. {
  218. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确");
  219. }
  220. dt.Clear();
  221. }
  222. }
  223. }