TicketPrinterHelper.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using NextTreatMesDemo.Models;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Drawing.Printing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Media;
  12. using Brushes = System.Drawing.Brushes;
  13. using FontStyle = System.Drawing.FontStyle;
  14. namespace NextTreatMesDemo.Utils
  15. {
  16. public class TicketPrinterHelper
  17. {
  18. //定义一个字符串流,用来接收所要打印的数据
  19. private static StringReader sr;
  20. //打印列表
  21. static PrintModel[] PrintModels;
  22. //二维码
  23. static string QrCode;
  24. //str要打印的数据
  25. public static bool Print(string str)
  26. {
  27. bool result = true;
  28. try
  29. {
  30. sr = new StringReader(str);
  31. PrintDocument pd = new PrintDocument();
  32. pd.PrintController = new StandardPrintController();
  33. //PaperSize pageSize = new PaperSize("Custom", getYc(193), 600);
  34. pd.DefaultPageSettings.Margins.Top = 2;
  35. pd.DefaultPageSettings.Margins.Left = 10;
  36. //pd.DefaultPageSettings.PaperSize = pageSize;
  37. pd.PrinterSettings.PrinterName = pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
  38. pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
  39. pd.Print();
  40. }
  41. catch (Exception)
  42. {
  43. result = false;
  44. }
  45. finally
  46. {
  47. if (sr != null)
  48. sr.Close();
  49. }
  50. return result;
  51. }
  52. public static bool Print(PrintModel[] pms, string ptrName)
  53. {
  54. bool result = true;
  55. try
  56. {
  57. PrintModels = pms;
  58. PrintDocument pd = new PrintDocument();
  59. pd.PrintController = new StandardPrintController();
  60. //PaperSize pageSize = new PaperSize("Custom", getYc(193), 600);
  61. pd.DefaultPageSettings.Margins.Top = 2;
  62. pd.DefaultPageSettings.Margins.Left = 10;
  63. //pd.DefaultPageSettings.PaperSize = pageSize;
  64. pd.PrinterSettings.PrinterName = ptrName;// pd.DefaultPageSettings.PrinterSettings.PrinterName;//默认打印机
  65. pd.PrintPage += new PrintPageEventHandler(pd_PrintPage1);
  66. pd.Print();
  67. }
  68. catch (Exception)
  69. {
  70. result = false;
  71. }
  72. finally
  73. {
  74. if (sr != null)
  75. sr.Close();
  76. }
  77. return result;
  78. }
  79. private static int getYc(double cm)
  80. {
  81. return (int)(cm / 25.4) * 100;
  82. }
  83. private static void pd_PrintPage(object sender, PrintPageEventArgs ev)
  84. {
  85. System.Drawing.Text.InstalledFontCollection fonts = new System.Drawing.Text.InstalledFontCollection();
  86. Font printFont = new Font("Arial", 9);//打印字体
  87. float linesPerPage = 0;
  88. float yPos = 0;
  89. int count = 0;
  90. float leftMargin = ev.MarginBounds.Left;
  91. float topMargin = ev.MarginBounds.Top;
  92. String line = "";
  93. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
  94. while (count < linesPerPage && ((line = sr.ReadLine()) != null))
  95. {
  96. //leftMargin = (350 - ev.Graphics.MeasureString(line, printFont).ToSize().Width)/2;
  97. yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
  98. ev.Graphics.DrawString(line, printFont, Brushes.Black,
  99. leftMargin, yPos);
  100. count++;
  101. }
  102. //ev.Graphics.DrawImage(QRCodeHelper.CreateQRCode("http://www.baidu.com"), new Point(70, Convert.ToInt32(yPos) + 20));
  103. //If more lines exist, print another page.
  104. if (line != null)
  105. ev.HasMorePages = true;
  106. else
  107. ev.HasMorePages = false;
  108. }
  109. private static void pd_PrintPage1(object sender, PrintPageEventArgs ev)
  110. {
  111. float yPos = 0;
  112. float leftMargin = ev.MarginBounds.Left;
  113. float topMargin = ev.MarginBounds.Top;
  114. int count = 0;
  115. Font printFont;
  116. foreach (PrintModel pm in PrintModels)
  117. {
  118. if (pm.IsBold)
  119. {
  120. printFont = new Font(pm.FontFamily, pm.FontSize, FontStyle.Bold);//打印字体
  121. }
  122. else
  123. {
  124. printFont = new Font(pm.FontFamily, pm.FontSize);//打印字体
  125. }
  126. float linesPerPage = 0;
  127. String line = "";
  128. linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);
  129. while (count < linesPerPage && ((line = pm.Text.ReadLine()) != null))
  130. {
  131. //leftMargin = (350 - ev.Graphics.MeasureString(line, printFont).ToSize().Width)/2;
  132. yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
  133. ev.Graphics.DrawString(line, printFont, Brushes.Black,
  134. leftMargin, yPos);
  135. count++;
  136. }
  137. //If more lines exist, print another page.
  138. if (line != null)
  139. ev.HasMorePages = true;
  140. else
  141. ev.HasMorePages = false;
  142. topMargin = topMargin + 20;
  143. yPos = yPos + 20;
  144. }
  145. //if (!string.IsNullOrEmpty(QrCode))
  146. //{
  147. // ev.Graphics.DrawImage(QRCodeHelper.CreateQRCode(QrCode), new Point(70, Convert.ToInt32(yPos)));
  148. //}
  149. }
  150. }
  151. public enum Printer
  152. {
  153. REGO = 0,
  154. DASCOM = 1
  155. }
  156. }