PrintHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name + ".frx");
  137. foreach (var item in barcode)
  138. {
  139. foreach (var itemc in item)
  140. {
  141. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  142. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  143. {
  144. string itemstr = "";
  145. if (itemc.Value != null)
  146. {
  147. itemstr = itemc.Value.ToString();
  148. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  149. {
  150. itemstr = itemstr.Remove(0, 2);
  151. }
  152. if (itemstr.StartsWith("\""))
  153. {
  154. itemstr = itemstr.Remove(0, 1);
  155. }
  156. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  157. {
  158. itemstr = itemstr.Remove(itemstr.Length - 2);
  159. }
  160. if (itemstr.EndsWith("\""))
  161. {
  162. itemstr = itemstr.Remove(itemstr.Length - 1);
  163. }
  164. if (itemstr.Contains("\\\""))
  165. {
  166. itemstr = itemstr.Replace("\\\"", "\"");
  167. }
  168. }
  169. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  170. if (!dt.Columns.Contains(itemc.Key))
  171. dt.Columns.Add(DC);
  172. }
  173. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  174. {
  175. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  176. if (!dt.Columns.Contains(itemc.Key))
  177. dt.Columns.Add(DC);
  178. }
  179. }
  180. }
  181. code = 0;
  182. first = "";
  183. last = "";
  184. foreach (var item in barcode)
  185. {
  186. int number = Settings.Default.Printnum;
  187. for (int i = 0; i < number; i++)
  188. {
  189. DataRow dr = dt.NewRow();
  190. foreach (var itemc in item)
  191. {
  192. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  193. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  194. {
  195. string itemstr = "";
  196. if (itemc.Value != null)
  197. {
  198. itemstr = itemc.Value.ToString();
  199. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  200. {
  201. itemstr = itemstr.Remove(0, 2);
  202. }
  203. if (itemstr.StartsWith("\""))
  204. {
  205. itemstr = itemstr.Remove(0, 1);
  206. }
  207. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  208. {
  209. itemstr = itemstr.Remove(itemstr.Length - 2);
  210. }
  211. if (itemstr.EndsWith("\""))
  212. {
  213. itemstr = itemstr.Remove(itemstr.Length - 1);
  214. }
  215. if (itemstr.Contains("\\\""))
  216. {
  217. itemstr = itemstr.Replace("\\\"", "\"");
  218. }
  219. }
  220. dr[itemc.Key] = itemstr;
  221. }
  222. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  223. {
  224. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  225. if (code == 0)
  226. first = itemc.Value.ToString().Replace("\"", "");
  227. if(code == barcode.Count -1)
  228. last = itemc.Value.ToString().Replace("\"", "");
  229. }
  230. }
  231. dt.Rows.Add(dr);
  232. }
  233. code++;
  234. }
  235. try
  236. {
  237. string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  238. ////string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,"+first+"到"+last+",是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  239. if (logout_confirm == "Yes")
  240. {
  241. Report.PrintSettings.ShowDialog = false;
  242. byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(chooseprintername);
  243. chooseprintername = Encoding.UTF8.GetString(buffer);
  244. Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\");
  245. //foreach (DataRow dr in dt.Rows)
  246. //{
  247. // DataTable dta = dt.Clone();
  248. // dta.Rows.Add(dr.ItemArray);
  249. // Report.RegisterData(dta, label_type);
  250. // Report.GetDataSource(label_type).Enabled = true;
  251. // Report.Print();
  252. //}
  253. Report.RegisterData(dt, label_type);
  254. Report.GetDataSource(label_type).Enabled = true;
  255. Report.Print();
  256. }
  257. }
  258. catch (Exception ex)
  259. {
  260. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
  261. }
  262. dt.Clear();
  263. }
  264. }
  265. }