PrintHandler.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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(Exception ex)
  116. {
  117. MessageBox.Show("未找到可使用的标签文件"+ex.Message);
  118. return;
  119. }
  120. foreach (var item in barcode)
  121. {
  122. foreach (var itemc in item)
  123. {
  124. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  125. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  126. {
  127. string itemstr = "";
  128. if (itemc.Value != null)
  129. {
  130. itemstr = itemc.Value.ToString();
  131. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  132. {
  133. itemstr = itemstr.Remove(0, 2);
  134. }
  135. if (itemstr.StartsWith("\""))
  136. {
  137. itemstr = itemstr.Remove(0, 1);
  138. }
  139. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  140. {
  141. itemstr = itemstr.Remove(itemstr.Length - 2);
  142. }
  143. if (itemstr.EndsWith("\""))
  144. {
  145. itemstr = itemstr.Remove(itemstr.Length - 1);
  146. }
  147. if (itemstr.Contains("\\\""))
  148. {
  149. itemstr = itemstr.Replace("\\\"", "\"");
  150. }
  151. }
  152. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  153. if (!dt.Columns.Contains(itemc.Key))
  154. dt.Columns.Add(DC);
  155. }
  156. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  157. {
  158. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  159. if (!dt.Columns.Contains(itemc.Key))
  160. dt.Columns.Add(DC);
  161. }
  162. }
  163. }
  164. foreach (var item in barcode)
  165. {
  166. DataRow dr = dt.NewRow();
  167. foreach (var itemc in item)
  168. {
  169. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  170. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  171. {
  172. string itemstr = "";
  173. if (itemc.Value != null)
  174. {
  175. itemstr = itemc.Value.ToString();
  176. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  177. {
  178. itemstr = itemstr.Remove(0, 2);
  179. }
  180. if (itemstr.StartsWith("\""))
  181. {
  182. itemstr = itemstr.Remove(0, 1);
  183. }
  184. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  185. {
  186. itemstr = itemstr.Remove(itemstr.Length - 2);
  187. }
  188. if (itemstr.EndsWith("\""))
  189. {
  190. itemstr = itemstr.Remove(itemstr.Length - 1);
  191. }
  192. if (itemstr.Contains("\\\""))
  193. {
  194. itemstr = itemstr.Replace("\\\"", "\"");
  195. }
  196. }
  197. dr[itemc.Key] = itemstr;
  198. }
  199. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  200. {
  201. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  202. }
  203. }
  204. dt.Rows.Add(dr);
  205. }
  206. try
  207. {
  208. string logout_confirm = MessageBox.Show("打印"+dt.Rows.Count+"张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  209. if (logout_confirm == "Yes")
  210. {
  211. Report.PrintSettings.ShowDialog = false;
  212. Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\");
  213. //foreach (DataRow dr in dt.Rows)
  214. //{
  215. // DataTable dta = dt.Clone();
  216. // dta.Rows.Add(dr.ItemArray);
  217. // Report.RegisterData(dta, label_type);
  218. // Report.GetDataSource(label_type).Enabled = true;
  219. // Report.Print();
  220. //}
  221. Report.RegisterData(dt, label_type);
  222. Report.GetDataSource(label_type).Enabled = true;
  223. Report.Print();
  224. }
  225. }
  226. catch(Exception ex)
  227. {
  228. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
  229. }
  230. dt.Clear();
  231. }
  232. }
  233. }