PrintHandler.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. using UAS_PRINT.Properties;
  12. namespace UAS_PRINT
  13. {
  14. class PrintHandler
  15. {
  16. //打印图片集合
  17. static List<Bitmap> bitmaps = new List<Bitmap>();
  18. static DataTable dt = new DataTable();
  19. public static void zplprint(string str, string PrinterName)
  20. {
  21. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  22. //获取所有的打印格式数据
  23. if (data.ContainsKey("data"))
  24. {
  25. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
  26. List<string> PrintList = new List<string>();
  27. int PrintTime = Regex.Matches(PrintCode, "XA").Count;
  28. for (int i = 0; i < PrintTime; i++)
  29. {
  30. PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
  31. PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
  32. }
  33. for (int i = 0; i < PrintList.Count; i++)
  34. {
  35. PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
  36. }
  37. data.Clear();
  38. }
  39. else if (data.ContainsKey("exceptionInfo"))
  40. {
  41. //string PrintCode = data["exceptionInfo"].ToString();
  42. //MessageBox.Show(PrintCode);
  43. }
  44. }
  45. public static void zplprint(string str)
  46. {
  47. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  48. //获取所有的打印格式数据
  49. if (data.ContainsKey("data"))
  50. {
  51. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
  52. string PrinterName = data["uid"].ToString().Replace("\"", "");
  53. if (!PrintCode.StartsWith("^XA"))
  54. {
  55. return;
  56. }
  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. if (Settings.Default.ifuseftp)
  113. {
  114. ftpOperater frp = new ftpOperater(true);
  115. try
  116. {
  117. frp.Download(label_name + ".frx", DateTime.Now);
  118. }
  119. catch (Exception ex)
  120. {
  121. MessageBox.Show("标签文件更新失败" + ex.Message);
  122. }
  123. }
  124. try
  125. {
  126. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");
  127. }
  128. catch(Exception ex)
  129. {
  130. MessageBox.Show("未找到可使用的标签文件"+ex.Message);
  131. return;
  132. }
  133. foreach (var item in barcode)
  134. {
  135. foreach (var itemc in item)
  136. {
  137. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  138. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  139. {
  140. string itemstr = "";
  141. if (itemc.Value != null)
  142. {
  143. itemstr = itemc.Value.ToString();
  144. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  145. {
  146. itemstr = itemstr.Remove(0, 2);
  147. }
  148. if (itemstr.StartsWith("\""))
  149. {
  150. itemstr = itemstr.Remove(0, 1);
  151. }
  152. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  153. {
  154. itemstr = itemstr.Remove(itemstr.Length - 2);
  155. }
  156. if (itemstr.EndsWith("\""))
  157. {
  158. itemstr = itemstr.Remove(itemstr.Length - 1);
  159. }
  160. if (itemstr.Contains("\\\""))
  161. {
  162. itemstr = itemstr.Replace("\\\"", "\"");
  163. }
  164. }
  165. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  166. if (!dt.Columns.Contains(itemc.Key))
  167. dt.Columns.Add(DC);
  168. }
  169. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  170. {
  171. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  172. if (!dt.Columns.Contains(itemc.Key))
  173. dt.Columns.Add(DC);
  174. }
  175. }
  176. }
  177. foreach (var item in barcode)
  178. {
  179. int number = Settings.Default.Printnum;
  180. for (int i = 0; i < number; i++)
  181. {
  182. DataRow dr = dt.NewRow();
  183. foreach (var itemc in item)
  184. {
  185. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  186. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  187. {
  188. string itemstr = "";
  189. if (itemc.Value != null)
  190. {
  191. itemstr = itemc.Value.ToString();
  192. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  193. {
  194. itemstr = itemstr.Remove(0, 2);
  195. }
  196. if (itemstr.StartsWith("\""))
  197. {
  198. itemstr = itemstr.Remove(0, 1);
  199. }
  200. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  201. {
  202. itemstr = itemstr.Remove(itemstr.Length - 2);
  203. }
  204. if (itemstr.EndsWith("\""))
  205. {
  206. itemstr = itemstr.Remove(itemstr.Length - 1);
  207. }
  208. if (itemstr.Contains("\\\""))
  209. {
  210. itemstr = itemstr.Replace("\\\"", "\"");
  211. }
  212. }
  213. dr[itemc.Key] = itemstr;
  214. }
  215. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  216. {
  217. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  218. }
  219. }
  220. dt.Rows.Add(dr);
  221. }
  222. }
  223. try
  224. {
  225. string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  226. if (logout_confirm == "Yes")
  227. {
  228. Report.PrintSettings.ShowDialog = false;
  229. byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(chooseprintername);
  230. chooseprintername = Encoding.UTF8.GetString(buffer);
  231. Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\");
  232. foreach (DataRow dr in dt.Rows)
  233. {
  234. DataTable dta = dt.Clone();
  235. dta.Rows.Add(dr.ItemArray);
  236. Report.RegisterData(dta, label_type);
  237. Report.GetDataSource(label_type).Enabled = true;
  238. Report.Print();
  239. }
  240. //Report.RegisterData(dt, label_type);
  241. //Report.GetDataSource(label_type).Enabled = true;
  242. //Report.Print();
  243. }
  244. }
  245. catch(Exception ex)
  246. {
  247. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
  248. }
  249. dt.Clear();
  250. }
  251. }
  252. }