PrintHandler.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. PrintHelper.SendStringToPrinter(PrinterName, PrintCode);
  60. PrintDocument pd = new PrintDocument();
  61. pd.PrinterSettings.PrinterName = PrinterName;
  62. data.Clear();
  63. }
  64. else if (data.ContainsKey("exceptionInfo"))
  65. {
  66. //string PrintCode = data["exceptionInfo"].ToString();
  67. //MessageBox.Show(PrintCode);
  68. }
  69. }
  70. public static void vendorZplPrint( PrinterList print, string PrinterName,string str) {
  71. Report Report = new Report();
  72. RadioButton PRFR = (RadioButton)print.Controls["PRFR"];
  73. RadioButton PRPIC = (RadioButton)print.Controls["PRPIC"];
  74. if (PRFR.Checked)
  75. {
  76. try
  77. {
  78. Report.Load(System.AppDomain.CurrentDomain.BaseDirectory + "GD.frx");
  79. }
  80. catch
  81. {
  82. MessageBox.Show("未找到可使用的标签文件");
  83. return;
  84. }
  85. }
  86. bitmaps.Clear();
  87. Dictionary<string, object> data = PrintHelper.ToDictionary(str);
  88. List<Dictionary<string, object>> parameter = new List<Dictionary<string, object>>();
  89. List<Dictionary<string, object>> barcode = new List<Dictionary<string, object>>();
  90. parameter = (List<Dictionary<string, object>>)data["parameter"];
  91. barcode = (List<Dictionary<string, object>>)data["barcode"];
  92. int la_width = int.Parse(parameter.First()["LA_WIDTH"].ToString());
  93. int la_height = int.Parse(parameter.First()["LA_HEIGHT"].ToString());
  94. foreach (var item in barcode)
  95. {
  96. Bitmap bit = new Bitmap(la_width * 10, la_height * 4);
  97. using (Graphics g = Graphics.FromImage(bit))
  98. {
  99. //g.CompositingQuality = CompositingQuality.HighQuality;
  100. //g.SmoothingMode = SmoothingMode.HighQuality;
  101. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  102. ////定义一枝画笔
  103. //Pen pen = new Pen(Color.Black);
  104. ////用这个画笔在左上角画一个矩形
  105. //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
  106. foreach (var itemc in item)
  107. {
  108. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  109. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  110. {
  111. string itemstr = "";
  112. if (itemc.Value != null)
  113. {
  114. itemstr = itemc.Value.ToString();
  115. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  116. {
  117. itemstr = itemstr.Remove(0, 2);
  118. }
  119. if (itemstr.StartsWith("\""))
  120. {
  121. itemstr = itemstr.Remove(0, 1);
  122. }
  123. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  124. {
  125. itemstr = itemstr.Remove(itemstr.Length - 2);
  126. }
  127. if (itemstr.EndsWith("\""))
  128. {
  129. itemstr = itemstr.Remove(itemstr.Length - 1);
  130. }
  131. if (itemstr.Contains("\\\""))
  132. {
  133. itemstr = itemstr.Replace("\\\"", "\"");
  134. }
  135. }
  136. if (PRFR.Checked)
  137. {
  138. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  139. if (!dt.Columns.Contains(itemc.Key))
  140. dt.Columns.Add(DC);
  141. }
  142. if (PRPIC.Checked)
  143. {
  144. PrintHelper.Printstring(itemc.Value != null ? itemstr : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()));
  145. }
  146. }
  147. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  148. {
  149. if (PRFR.Checked)
  150. {
  151. DataColumn DC = new DataColumn(itemc.Key, Type.GetType("System.String"));
  152. if (!dt.Columns.Contains(itemc.Key))
  153. dt.Columns.Add(DC);
  154. }
  155. if (PRPIC.Checked)
  156. {
  157. PrintHelper.DrawBarcode(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", ref bit, double.Parse(Sidc["LP_LEFTRATE"].ToString()), double.Parse(Sidc["LP_TOPRATE"].ToString()), double.Parse(Sidc["LP_WIDTH"].ToString()), double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
  158. PrintHelper.Printstring(itemc.Value != null ? itemc.Value.ToString().Replace("\"", "") : " ", int.Parse(Sidc["LP_SIZE"].ToString()), g, int.Parse(Sidc["LP_LEFTRATE"].ToString()), int.Parse(Sidc["LP_TOPRATE"].ToString()) + double.Parse(Sidc["LP_HEIGHT"].ToString()) - 2);
  159. }
  160. }
  161. }
  162. }
  163. bitmaps.Add(bit);
  164. }
  165. if (PRFR.Checked)
  166. {
  167. foreach (var item in barcode)
  168. {
  169. DataRow dr = dt.NewRow();
  170. //g.CompositingQuality = CompositingQuality.HighQuality;
  171. //g.SmoothingMode = SmoothingMode.HighQuality;
  172. //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
  173. ////定义一枝画笔
  174. //Pen pen = new Pen(Color.Black);
  175. ////用这个画笔在左上角画一个矩形
  176. //g.DrawRectangle(pen, new Rectangle(10, 10, 50, 20));
  177. foreach (var itemc in item)
  178. {
  179. Dictionary<string, object> Sidc = parameter.Find(p => p["LP_NAME"].ToString().Replace(" ", "").Replace("\"", "").ToUpper() == itemc.Key.ToUpper());
  180. if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "text")
  181. {
  182. string itemstr = "";
  183. if (itemc.Value != null)
  184. {
  185. itemstr = itemc.Value.ToString();
  186. while (itemstr.StartsWith("\f") || itemstr.StartsWith("\v") || itemstr.StartsWith("\t") || itemstr.StartsWith("\r") || itemstr.StartsWith("\n"))
  187. {
  188. itemstr = itemstr.Remove(0, 2);
  189. }
  190. if (itemstr.StartsWith("\""))
  191. {
  192. itemstr = itemstr.Remove(0, 1);
  193. }
  194. while (itemstr.EndsWith("\f") || itemstr.EndsWith("\v") || itemstr.EndsWith("\t") || itemstr.EndsWith("\r") || itemstr.EndsWith("\n"))
  195. {
  196. itemstr = itemstr.Remove(itemstr.Length - 2);
  197. }
  198. if (itemstr.EndsWith("\""))
  199. {
  200. itemstr = itemstr.Remove(itemstr.Length - 1);
  201. }
  202. if (itemstr.Contains("\\\""))
  203. {
  204. itemstr = itemstr.Replace("\\\"", "\"");
  205. Console.WriteLine(itemstr);
  206. }
  207. }
  208. dr[itemc.Key] = itemstr;
  209. }
  210. else if (Sidc["LP_VALUETYPE"].ToString().Replace(" ", "").Replace("\"", "") == "barcode")
  211. {
  212. dr[itemc.Key] = itemc.Value.ToString().Replace("\"", "");
  213. }
  214. }
  215. dt.Rows.Add(dr);
  216. }
  217. }
  218. if (PRPIC.Checked)
  219. {
  220. imagecount = bitmaps.Count;
  221. ////设置打印机名称
  222. print.PrintDoc.PrintPage += PrintPage;
  223. print.PrintDoc.Print();
  224. }
  225. if (PRFR.Checked)
  226. {
  227. Report.RegisterData(dt, "VENDORBARCODE_VIEW");
  228. Report.GetDataSource("VENDORBARCODE_VIEW").Enabled = true;
  229. Report.PrintSettings.ShowDialog = false;
  230. Report.PrintSettings.Printer = PrinterName;
  231. Report.Print();
  232. dt.Clear();
  233. }
  234. }
  235. private static void PrintPage(object sender, PrintPageEventArgs e)
  236. {
  237. Image i = bitmaps[index];
  238. Graphics g = e.Graphics;
  239. g.DrawImage(i, 4, 10, i.Width, i.Height);
  240. if (index < imagecount - 1)
  241. {
  242. e.HasMorePages = true;
  243. index++;
  244. }
  245. else
  246. {
  247. index = 0;
  248. e.HasMorePages = false;
  249. }
  250. }
  251. }
  252. }