PrintHandler.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 int imagecount = 0;
  17. static int index = 0;
  18. //打印图片集合
  19. static List<Bitmap> bitmaps = new List<Bitmap>();
  20. static DataTable dt = new DataTable("VENDORBARCODE_VIEW");
  21. public static void zplprint(string str, string PrinterName)
  22. {
  23. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  24. //获取所有的打印格式数据
  25. if (data.ContainsKey("data"))
  26. {
  27. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "");
  28. List<string> PrintList = new List<string>();
  29. int PrintTime = Regex.Matches(PrintCode, "XA").Count;
  30. for (int i = 0; i < PrintTime; i++)
  31. {
  32. PrintList.Add(PrintCode.Substring(0, PrintCode.IndexOf("XZ") + 2));
  33. PrintCode = PrintCode.Substring(PrintCode.IndexOf("XZ") + 2);
  34. }
  35. for (int i = 0; i < PrintList.Count; i++)
  36. {
  37. PrintHelper.SendStringToPrinter(PrinterName, PrintList[i]);
  38. }
  39. data.Clear();
  40. }
  41. else if (data.ContainsKey("exceptionInfo"))
  42. {
  43. //string PrintCode = data["exceptionInfo"].ToString();
  44. //MessageBox.Show(PrintCode);
  45. }
  46. }
  47. public static void zplprint(string str)
  48. {
  49. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  50. //获取所有的打印格式数据
  51. if (data.ContainsKey("data"))
  52. {
  53. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
  54. string PrinterName = data["uid"].ToString().Replace("\"", "");
  55. if (!PrintCode.StartsWith("^XA"))
  56. {
  57. return;
  58. }
  59. Console.WriteLine(PrintCode);
  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. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  74. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  75. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  76. try
  77. {
  78. parameter = (List<Dictionary<string, object>>)data["parameter"];
  79. barcode = (List<Dictionary<string, object>>)data["barcode"];
  80. }
  81. catch
  82. {
  83. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  84. return;
  85. }
  86. try
  87. {
  88. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  89. }
  90. catch
  91. {
  92. chooseprintername = "no";
  93. }
  94. try
  95. {
  96. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + "库存条码打印.frx");
  97. }
  98. catch
  99. {
  100. MessageBox.Show("未找到可使用的标签文件");
  101. return;
  102. }
  103. int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
  104. int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
  105. foreach (var item in barcode)
  106. {
  107. foreach (var itemc in item)
  108. {
  109. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  110. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  111. {
  112. string itemstr = "";
  113. if (itemc.Value != null)
  114. {
  115. itemstr = itemc.Value.ToString();
  116. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  117. {
  118. itemstr = itemstr.Remove(0, 2);
  119. }
  120. if (itemstr.StartsWith("\""))
  121. {
  122. itemstr = itemstr.Remove(0, 1);
  123. }
  124. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  125. {
  126. itemstr = itemstr.Remove(itemstr.Length - 2);
  127. }
  128. if (itemstr.EndsWith("\""))
  129. {
  130. itemstr = itemstr.Remove(itemstr.Length - 1);
  131. }
  132. if (itemstr.Contains("\\\""))
  133. {
  134. itemstr = itemstr.Replace("\\\"", "\"");
  135. }
  136. }
  137. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  138. if (!dt.Columns.Contains(itemc.Key))
  139. dt.Columns.Add(DC);
  140. }
  141. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  142. {
  143. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  144. if (!dt.Columns.Contains(itemc.Key))
  145. dt.Columns.Add(DC);
  146. }
  147. }
  148. }
  149. foreach (var item in barcode)
  150. {
  151. DataRow dr = dt.NewRow();
  152. foreach (var itemc in item)
  153. {
  154. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  155. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  156. {
  157. string itemstr = "";
  158. if (itemc.Value != null)
  159. {
  160. itemstr = itemc.Value.ToString();
  161. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  162. {
  163. itemstr = itemstr.Remove(0, 2);
  164. }
  165. if (itemstr.StartsWith("\""))
  166. {
  167. itemstr = itemstr.Remove(0, 1);
  168. }
  169. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  170. {
  171. itemstr = itemstr.Remove(itemstr.Length - 2);
  172. }
  173. if (itemstr.EndsWith("\""))
  174. {
  175. itemstr = itemstr.Remove(itemstr.Length - 1);
  176. }
  177. if (itemstr.Contains("\\\""))
  178. {
  179. itemstr = itemstr.Replace("\\\"", "\"");
  180. Console.WriteLine(itemstr);
  181. }
  182. }
  183. dr[itemc.Key] = itemstr;
  184. }
  185. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  186. {
  187. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  188. }
  189. }
  190. dt.Rows.Add(dr);
  191. }
  192. try
  193. {
  194. Report.RegisterData(dt, "BARCODE_PRINT_VIEW");
  195. Report.GetDataSource("BARCODE_PRINT_VIEW").Enabled = true;
  196. Report.PrintSettings.ShowDialog = false;
  197. Report.PrintSettings.Printer = chooseprintername;
  198. Report.Print();
  199. }
  200. catch
  201. {
  202. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确");
  203. }
  204. dt.Clear();
  205. }
  206. }
  207. }