PrintHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. static int code = 0;
  20. static string first = "";
  21. static string last = "";
  22. public static void zplprint(string str, string PrinterName)
  23. {
  24. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  25. //获取所有的打印格式数据
  26. if (data.ContainsKey("data"))
  27. {
  28. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
  29. List<string> PrintList = new List<string>();
  30. int PrintTime = Regex.Matches(PrintCode, "XA").Count;
  31. for (int i = 0; i < PrintTime; i++)
  32. {
  33. PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
  34. PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
  35. }
  36. for (int i = 0; i < PrintList.Count; i++)
  37. {
  38. PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
  39. }
  40. data.Clear();
  41. }
  42. else if (data.ContainsKey("exceptionInfo"))
  43. {
  44. //string PrintCode = data["exceptionInfo"].ToString();
  45. //MessageBox.Show(PrintCode);
  46. }
  47. }
  48. public static void zplprint(string str)
  49. {
  50. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  51. //获取所有的打印格式数据
  52. if (data.ContainsKey("data"))
  53. {
  54. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
  55. string PrinterName = data["uid"].ToString().Replace("\"", "");
  56. if (!PrintCode.StartsWith("^XA"))
  57. {
  58. return;
  59. }
  60. PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
  61. PrintDocument pd = new PrintDocument();
  62. pd.PrinterSettings.PrinterName = PrinterName;
  63. data.Clear();
  64. }
  65. else if (data.ContainsKey("exceptionInfo"))
  66. {
  67. }
  68. }
  69. public static void vendorZplPrint( string str)
  70. {
  71. Report Report = new Report();
  72. string chooseprintername;
  73. string label_type = "";
  74. string label_name = "";
  75. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  76. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  77. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  78. try
  79. {
  80. parameter = (List<Dictionary<string, object>>)data["parameter"];
  81. barcode = (List<Dictionary<string, object>>)data["barcode"];
  82. }
  83. catch
  84. {
  85. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  86. return;
  87. }
  88. try
  89. {
  90. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  91. }
  92. catch
  93. {
  94. MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示");
  95. return;
  96. }
  97. try
  98. {
  99. label_type = ((string)data["label_type"]).Replace("\"", "");
  100. }
  101. catch
  102. {
  103. MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示");
  104. return;
  105. }
  106. try
  107. {
  108. label_name = ((string)data["label_name"]).Replace("\"", "");
  109. }
  110. catch
  111. {
  112. MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
  113. return;
  114. }
  115. if (Settings.Default.ifuseftp)
  116. {
  117. ftpOperater frp = new ftpOperater(true);
  118. try
  119. {
  120. frp.Download(label_name + ".frx", DateTime.Now);
  121. }
  122. catch (Exception ex)
  123. {
  124. MessageBox.Show("标签文件更新失败" + ex.Message);
  125. }
  126. }
  127. try
  128. {
  129. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");
  130. }
  131. catch(Exception ex)
  132. {
  133. MessageBox.Show("未找到可使用的标签文件"+ex.Message);
  134. return;
  135. }
  136. foreach (var item in barcode)
  137. {
  138. foreach (var itemc in item)
  139. {
  140. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  141. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  142. {
  143. string itemstr = "";
  144. if (itemc.Value != null)
  145. {
  146. itemstr = itemc.Value.ToString();
  147. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  148. {
  149. itemstr = itemstr.Remove(0, 2);
  150. }
  151. if (itemstr.StartsWith("\""))
  152. {
  153. itemstr = itemstr.Remove(0, 1);
  154. }
  155. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  156. {
  157. itemstr = itemstr.Remove(itemstr.Length - 2);
  158. }
  159. if (itemstr.EndsWith("\""))
  160. {
  161. itemstr = itemstr.Remove(itemstr.Length - 1);
  162. }
  163. if (itemstr.Contains("\\\""))
  164. {
  165. itemstr = itemstr.Replace("\\\"", "\"");
  166. }
  167. }
  168. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  169. if (!dt.Columns.Contains(itemc.Key))
  170. dt.Columns.Add(DC);
  171. }
  172. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  173. {
  174. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  175. if (!dt.Columns.Contains(itemc.Key))
  176. dt.Columns.Add(DC);
  177. }
  178. }
  179. }
  180. code = 0;
  181. first = "";
  182. last = "";
  183. foreach (var item in barcode)
  184. {
  185. int number = Settings.Default.Printnum;
  186. for (int i = 0; i < number; i++)
  187. {
  188. DataRow dr = dt.NewRow();
  189. foreach (var itemc in item)
  190. {
  191. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  192. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  193. {
  194. string itemstr = "";
  195. if (itemc.Value != null)
  196. {
  197. itemstr = itemc.Value.ToString();
  198. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  199. {
  200. itemstr = itemstr.Remove(0, 2);
  201. }
  202. if (itemstr.StartsWith("\""))
  203. {
  204. itemstr = itemstr.Remove(0, 1);
  205. }
  206. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  207. {
  208. itemstr = itemstr.Remove(itemstr.Length - 2);
  209. }
  210. if (itemstr.EndsWith("\""))
  211. {
  212. itemstr = itemstr.Remove(itemstr.Length - 1);
  213. }
  214. if (itemstr.Contains("\\\""))
  215. {
  216. itemstr = itemstr.Replace("\\\"", "\"");
  217. }
  218. }
  219. dr[itemc.Key] = itemstr;
  220. }
  221. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  222. {
  223. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  224. if (code == 0)
  225. first = itemc.Value.ToString().Replace("\"", "");
  226. if(code == barcode.Count -1)
  227. last = itemc.Value.ToString().Replace("\"", "");
  228. }
  229. }
  230. dt.Rows.Add(dr);
  231. }
  232. code++;
  233. }
  234. try
  235. {
  236. string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  237. //string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,"+first+"到"+last+",是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  238. if (logout_confirm == "Yes")
  239. {
  240. Report.PrintSettings.ShowDialog = false;
  241. byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(chooseprintername);
  242. chooseprintername = Encoding.UTF8.GetString(buffer);
  243. Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\");
  244. //foreach (DataRow dr in dt.Rows)
  245. //{
  246. // DataTable dta = dt.Clone();
  247. // dta.Rows.Add(dr.ItemArray);
  248. // Report.RegisterData(dta, label_type);
  249. // Report.GetDataSource(label_type).Enabled = true;
  250. // Report.Print();
  251. //}
  252. Report.RegisterData(dt, label_type);
  253. Report.GetDataSource(label_type).Enabled = true;
  254. Report.Print();
  255. }
  256. }
  257. catch(Exception ex)
  258. {
  259. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
  260. }
  261. dt.Clear();
  262. }
  263. }
  264. }