PrintHandler.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 SetFormCenter(Form form)
  49. {
  50. form.StartPosition = FormStartPosition.CenterParent;
  51. }
  52. public static void zplprint(string str)
  53. {
  54. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  55. //获取所有的打印格式数据
  56. if (data.ContainsKey("data"))
  57. {
  58. string PrintCode = data["data"].ToString().Replace(" ", "").Replace("\"", "").Replace("\\n", "");
  59. string PrinterName = data["uid"].ToString().Replace("\"", "");
  60. if (!PrintCode.StartsWith("^XA"))
  61. {
  62. return;
  63. }
  64. PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
  65. PrintDocument pd = new PrintDocument();
  66. pd.PrinterSettings.PrinterName = PrinterName;
  67. data.Clear();
  68. }
  69. else if (data.ContainsKey("exceptionInfo"))
  70. {
  71. }
  72. }
  73. public static void vendorZplPrint( string str)
  74. {
  75. Report Report = new Report();
  76. string chooseprintername;
  77. string label_type = "";
  78. string label_name = "";
  79. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  80. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  81. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  82. try
  83. {
  84. parameter = (List<Dictionary<string, object>>)data["parameter"];
  85. barcode = (List<Dictionary<string, object>>)data["barcode"];
  86. }
  87. catch
  88. {
  89. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  90. return;
  91. }
  92. try
  93. {
  94. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  95. }
  96. catch
  97. {
  98. MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示");
  99. return;
  100. }
  101. try
  102. {
  103. label_type = ((string)data["label_type"]).Replace("\"", "");
  104. }
  105. catch
  106. {
  107. MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示");
  108. return;
  109. }
  110. try
  111. {
  112. label_name = ((string)data["label_name"]).Replace("\"", "");
  113. }
  114. catch
  115. {
  116. MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
  117. return;
  118. }
  119. if (Settings.Default.ifuseftp)
  120. {
  121. ftpOperater frp = new ftpOperater(true);
  122. try
  123. {
  124. frp.Download(label_name + ".frx", DateTime.Now);
  125. }
  126. catch (Exception ex)
  127. {
  128. MessageBox.Show("标签文件更新失败" + ex.Message);
  129. }
  130. }
  131. try
  132. {
  133. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + label_name+".frx");
  134. }
  135. catch(Exception ex)
  136. {
  137. MessageBox.Show("未找到可使用的标签文件"+ex.Message);
  138. return;
  139. }
  140. foreach (var item in barcode)
  141. {
  142. foreach (var itemc in item)
  143. {
  144. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  145. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  146. {
  147. string itemstr = "";
  148. if (itemc.Value != null)
  149. {
  150. itemstr = itemc.Value.ToString();
  151. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  152. {
  153. itemstr = itemstr.Remove(0, 2);
  154. }
  155. if (itemstr.StartsWith("\""))
  156. {
  157. itemstr = itemstr.Remove(0, 1);
  158. }
  159. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  160. {
  161. itemstr = itemstr.Remove(itemstr.Length - 2);
  162. }
  163. if (itemstr.EndsWith("\""))
  164. {
  165. itemstr = itemstr.Remove(itemstr.Length - 1);
  166. }
  167. if (itemstr.Contains("\\\""))
  168. {
  169. itemstr = itemstr.Replace("\\\"", "\"");
  170. }
  171. }
  172. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  173. if (!dt.Columns.Contains(itemc.Key))
  174. dt.Columns.Add(DC);
  175. }
  176. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  177. {
  178. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  179. if (!dt.Columns.Contains(itemc.Key))
  180. dt.Columns.Add(DC);
  181. }
  182. }
  183. }
  184. code = 0;
  185. first = "";
  186. last = "";
  187. foreach (var item in barcode)
  188. {
  189. int number = Settings.Default.Printnum;
  190. for (int i = 0; i < number; i++)
  191. {
  192. DataRow dr = dt.NewRow();
  193. foreach (var itemc in item)
  194. {
  195. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  196. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  197. {
  198. string itemstr = "";
  199. if (itemc.Value != null)
  200. {
  201. itemstr = itemc.Value.ToString();
  202. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  203. {
  204. itemstr = itemstr.Remove(0, 2);
  205. }
  206. if (itemstr.StartsWith("\""))
  207. {
  208. itemstr = itemstr.Remove(0, 1);
  209. }
  210. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  211. {
  212. itemstr = itemstr.Remove(itemstr.Length - 2);
  213. }
  214. if (itemstr.EndsWith("\""))
  215. {
  216. itemstr = itemstr.Remove(itemstr.Length - 1);
  217. }
  218. if (itemstr.Contains("\\\""))
  219. {
  220. itemstr = itemstr.Replace("\\\"", "\"");
  221. }
  222. }
  223. dr[itemc.Key] = itemstr;
  224. }
  225. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  226. {
  227. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  228. if (code == 0)
  229. first = itemc.Value.ToString().Replace("\"", "");
  230. if(code == barcode.Count -1)
  231. last = itemc.Value.ToString().Replace("\"", "");
  232. }
  233. }
  234. dt.Rows.Add(dr);
  235. }
  236. code++;
  237. }
  238. try
  239. {
  240. string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  241. //string logout_confirm = MessageBox.Show("打印" + dt.Rows.Count + "张条码,"+first+"到"+last+",是否继续", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly).ToString();
  242. if (logout_confirm == "Yes")
  243. {
  244. Report.PrintSettings.ShowDialog = false;
  245. byte[] buffer = Encoding.GetEncoding("GB2312").GetBytes(chooseprintername);
  246. chooseprintername = Encoding.UTF8.GetString(buffer);
  247. Report.PrintSettings.Printer = chooseprintername.Replace("\\\\", "\\");
  248. //foreach (DataRow dr in dt.Rows)
  249. //{
  250. // DataTable dta = dt.Clone();
  251. // dta.Rows.Add(dr.ItemArray);
  252. // Report.RegisterData(dta, label_type);
  253. // Report.GetDataSource(label_type).Enabled = true;
  254. // Report.Print();
  255. //}
  256. Report.RegisterData(dt, label_type);
  257. Report.GetDataSource(label_type).Enabled = true;
  258. Report.Print();
  259. }
  260. }
  261. catch(Exception ex)
  262. {
  263. MessageBox.Show("打印出现错误,请检查标签文件选择是否正确"+ex.Message);
  264. }
  265. dt.Clear();
  266. }
  267. // static LabelFormatDocument format;
  268. static BarTender.Format doc2;
  269. //public static void SinglePrint(string iCaller, BarTender.Application lbl, string LabelName, string LaID, string PrinterName, string SnCode, int PrintNum, string MakeCode, string ProdCode, string LabelType, string IfRePrint, out string ErrorMessage)
  270. public static void SinglePrint(string str, BarTender.Application lbl)
  271. {
  272. string chooseprintername;
  273. string label_type = "";
  274. string label_name = "";
  275. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  276. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  277. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  278. try
  279. {
  280. parameter = (List<Dictionary<string, object>>)data["parameter"];
  281. barcode = (List<Dictionary<string, object>>)data["barcode"];
  282. }
  283. catch
  284. {
  285. MessageBox.Show("打印数据解析异常,请联系管理员进行处理");
  286. return;
  287. }
  288. try
  289. {
  290. chooseprintername = ((string)data["chooseprintername"]).Replace("\"", "");
  291. }
  292. catch
  293. {
  294. MessageBox.Show("未传输打印机名称至打印程序,请联系管理员进行处理", "提示");
  295. return;
  296. }
  297. try
  298. {
  299. label_type = ((string)data["label_type"]).Replace("\"", "");
  300. }
  301. catch
  302. {
  303. MessageBox.Show("未获取到标签类型,请联系管理员进行处理", "提示");
  304. return;
  305. }
  306. try
  307. {
  308. label_name = ((string)data["label_name"]).Replace("\"", "");
  309. }
  310. catch
  311. {
  312. MessageBox.Show("未获取到标签名称,请联系管理员进行处理", "提示");
  313. return;
  314. }
  315. doc2 = lbl.Formats.Open(System.AppDomain.CurrentDomain.BaseDirectory + label_name + ".btw");
  316. if (doc2 == null)
  317. {
  318. MessageBox.Show("标签文件打开失败");
  319. return ;
  320. }
  321. foreach (var item in barcode)
  322. {
  323. foreach (var itemc in item)
  324. {
  325. for (int k = 0; k < doc2.NamedSubStrings.Count; k++)
  326. {
  327. if (doc2.NamedSubStrings.Item(k + 1).Name.ToUpper() == itemc.Key.ToUpper())
  328. {
  329. string itemstr = "";
  330. if (itemc.Value != null)
  331. {
  332. itemstr = itemc.Value.ToString();
  333. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  334. {
  335. itemstr = itemstr.Remove(0, 2);
  336. }
  337. if (itemstr.StartsWith("\""))
  338. {
  339. itemstr = itemstr.Remove(0, 1);
  340. }
  341. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  342. {
  343. itemstr = itemstr.Remove(itemstr.Length - 2);
  344. }
  345. if (itemstr.EndsWith("\""))
  346. {
  347. itemstr = itemstr.Remove(itemstr.Length - 1);
  348. }
  349. if (itemstr.Contains("\\\""))
  350. {
  351. itemstr = itemstr.Replace("\\\"", "\"");
  352. }
  353. }
  354. doc2.SetNamedSubStringValue(doc2.NamedSubStrings.Item(k + 1).Name, itemstr);
  355. }
  356. }
  357. }
  358. // 同样标签的份数
  359. doc2.PrintSetup.IdenticalCopiesOfLabel = Settings.Default.Printnum;
  360. // 序列标签数
  361. doc2.PrintSetup.NumberSerializedLabels = 1;
  362. doc2.PrintSetup.Printer = chooseprintername;
  363. doc2.PrintOut(false, false);
  364. }
  365. doc2.Close(BarTender.BtSaveOptions.btDoNotSaveChanges);
  366. //打印完毕
  367. lbl.Quit(BarTender.BtSaveOptions.btDoNotSaveChanges);
  368. return ;
  369. }
  370. }
  371. }