打印的一般示例:
可以实现自动换行,自动翻页,用命令格式控制居中、居右显示,字体加粗,下划线
命令格式 :
// 命令格式
// \x1B|3C - double high, 加粗,加大
// \x1B|uC - underline, 下划线
// \x1B|cA - Center, 居中显示
// \x1B|rA - Right, 居右显示
代码
1 ArrayList printerHelper = new ArrayList(); // 用来存储所有想要打印的行
2 int iPrintIndex = 0; // 打印计数器
3 string strPendingPrintText = ""; // 当前打印的行的剩余文字(因为一行文字可能会超出纸的宽度,需要折行)
4 string strPrintingText = ""; // 正要打印的文字
5
6 private void btn_PrintReceipt_Click(object sender, EventArgs e)
7 {
8 InitializePrinter(); // 配置打印机
9
10 if (!chkMultiPages.Checked)
11 TestInOnePage(); // 测试在一页中打印
12 else
13 TestMuntiPages(); // 测试自动翻页
14
15 if (!printDocument2.PrinterSettings.IsValid) // 检测打印机
16 {
17 System.Windows.Forms.MessageBox.Show("Invalid Printer !", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
18 return;
19 }
20 printDocument2.Print(); // 打印
21 }
22
23 /// <summary>
24 /// 初始化打印机
25 /// </summary>
26 protected void InitializePrinter()
27 {
28 printDocument2.PrinterSettings.PrinterName = listBox1.SelectedItem.ToString(); //AppState.Terminal.TmPrinter;
29
30 printDocument2.PrintController = new System.Drawing.Printing.StandardPrintController();
31 //printDocument2.DefaultPageSettings.PaperSize = new PaperSize("EPSON TM-U220", 210, 297); //可以手动配置纸的类型,也可以在控制面板中调整
32 printDocument2.DefaultPageSettings.Margins.Top = 10; // 设置 Margin
33 printDocument2.DefaultPageSettings.Margins.Left = 10;
34 printDocument2.DefaultPageSettings.Margins.Right = 10;
35 printDocument2.DefaultPageSettings.Margins.Bottom = 10;
36
37 // 重置所有参数
38 printerHelper = new ArrayList();
39 iPrintIndex = 0;
40 strPendingPrintText = "";
41 strPrintingText = "";
42 }
43
44 /// <summary>
45 /// 添加一个打印行
46 /// </summary>
47 /// <param name="s"></param>
48 /// <param name="b"></param>
49 private void Print(string s, bool b)
50 {
51 // 命令格式
52 // \x1B|3C - double high, 加粗,加大
53 // \x1B|uC - underline, 下划线
54 // \x1B|cA - Center, 居中显示
55 // \x1B|rA - Right, 居右显示
56
57 PrintAlignment alignment = PrintAlignment.Left;
58 SolidBrush brush = new SolidBrush(Color.Black);
59 Font FontOfText = new Font("Arial", 9);
60
61 if (s.IndexOf("\x1B|3C") >= 0) //double high
62 {
63 s = s.Replace("\x1B|3C", "");
64 FontOfText = new Font("Arial", 12, FontStyle.Bold);
65 }
66
67 if (s.IndexOf("\x1B|uC") >= 0) //underline
68 {
69 s = s.Replace("\x1B|uC", "");
70 FontOfText = new Font("Arial", FontOfText.Size, FontStyle.Underline);
71 }
72
73 if (s.IndexOf("\x1B|cA") >= 0) //Center
74 {
75 s = s.Replace("\x1B|cA", "");
76 alignment = PrintAlignment.Center;
77 }
78
79 if (s.IndexOf("\x1B|rA") >= 0) //Right
80 {
81 s = s.Replace("\x1B|rA", "");
82 alignment = PrintAlignment.Right;
83 }
84
85 printerHelper.Add(new PrinterHelper(s, brush, FontOfText, alignment));
86 }
87
88 /// <summary>
89 /// 打印事件,开始打印
90 /// </summary>
91 /// <param name="sender"></param>
92 /// <param name="e"></param>
93 private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
94 {
95 e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
96 e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
97 e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
98 e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
99
100 #region Test
101 if (GU_Display.Checked)
102 e.Graphics.PageUnit = GraphicsUnit.Display; // 可以设置Measure的单位, 默认为 Display
103
104 if (GU_Document.Checked)
105 e.Graphics.PageUnit = GraphicsUnit.Document;
106
107 if (GU_Inch.Checked)
108 e.Graphics.PageUnit = GraphicsUnit.Inch;
109
110 if (GU_Millimeter.Checked)
111 e.Graphics.PageUnit = GraphicsUnit.Millimeter;
112
113 if (GU_Pixel.Checked)
114 e.Graphics.PageUnit = GraphicsUnit.Pixel;
115
116 if (GU_Point.Checked)
117 e.Graphics.PageUnit = GraphicsUnit.Point;
118 #endregion Test
119
120 float P_X = e.MarginBounds.Left;
121 float P_Y = e.MarginBounds.Top;
122 PrinterHelper p;
123
124 for (; iPrintIndex < printerHelper.Count; iPrintIndex++)
125 {
126 p = (PrinterHelper)printerHelper[iPrintIndex];
127 if (strPendingPrintText.Length <= 0)
128 strPendingPrintText = p.Text;
129
130 if (strPrintingText.Length <= 0) // 如果 strPringText 有内容,说明这是翻页后的第一行
131 strPrintingText = GetOverLengthString(e.Graphics, strPendingPrintText, p.Font, e.MarginBounds.Width); // 得到可以在一行内打印的文字
132
133 while (strPrintingText.Length > 0)
134 {
135 e.HasMorePages = false;
136 if (P_Y + e.Graphics.MeasureString(strPrintingText, p.Font).Height > e.MarginBounds.Bottom) // 应该翻页
137 {
138 e.HasMorePages = true;
139 return;
140 }
141
142 switch (p.Alignment)
143 {
144 case PrintAlignment.Left:
145 P_X = e.MarginBounds.Left;
146 break;
147 case PrintAlignment.Center:
148 P_X = (e.MarginBounds.Width - e.Graphics.MeasureString(strPrintingText, p.Font).Width) / 2;
149 break;
150 case PrintAlignment.Right:
151 P_X = (e.MarginBounds.Right - e.Graphics.MeasureString(strPrintingText, p.Font).Width);
152 break;
153 }
154
155 e.Graphics.DrawString(strPrintingText, p.Font, p.Brush, P_X, P_Y);
156
157 P_Y += e.Graphics.MeasureString(strPrintingText, p.Font).Height; // Next Line Position Y
158 strPendingPrintText = strPendingPrintText.Substring(strPrintingText.Length); // Get left text
159 strPrintingText = ""; //Reset Printing text.
160
161 if (strPendingPrintText.Length <= 0) // 当前打印的行已经完成
162 break;
163 strPrintingText = GetOverLengthString(e.Graphics, strPendingPrintText, p.Font, e.MarginBounds.Width); // get text that can print in 1 line from left text
164 }
165 }
166
167 }
168
169 // This method get string that can print in 1 line
170 private string GetOverLengthString(Graphics g, string s, Font font, float MaxLen)
171 {
172 if (s.Length <= 0 || MaxLen <= 0 || g.MeasureString(s, font).Width < MaxLen)
173 return s;
174
175 while (true)
176 {
177 s = s.Substring(0, s.Length - 1);
178 if (g.MeasureString(s, font).Width < MaxLen)
179 return s;
180 }
181 }
182
183 private void TestInOnePage()
184 {
185 Print("\x1B|3C" + "\x1B|cA" + "(Center/Bold)Company Name" + "\n", true);
186 Print("\x1B|cA" + "\x1B|3C(Center)Company Remarks" + "\n", true);
187 Print("\x1B|cA" + "(Center)Company Information" + "\n", true);
188 Print("\x1B|cA" + "\x1B|uC" + "(Center/Under Line)Marketing Program Description" + "\n\n", false);
189 Print("\x1B|uCu A B eC ", true); // Will not Print
190 Print("\x1B|uC-----------------------------------------------", true); // Will not Print
191 Print("\n", true); // Will Print Blank Line
192 Print("(Left)ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n", true);
193 Print("\x1B|rA(Right)ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n", true);
194 Print("\n", true); // Will Print Blank Line
195 Print("________________________________________", true); // Will not Print
196 Print("", true); // Will not Print
197 Print("\x1B|cA" + "(Center)ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n\n\n\n\n\n\n\n\n\n", true);
198 Print("\x1B|uC" + "________________________________________\n", true);
199 Print("(Left)aaaaaaaaa" + "\n", true);
200 Print("\n", true);
201 Print("\n", true);
202 Print("\n", true);
203 Print("\n", true);
204 Print("(Left)ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ" + "\n", true);
205 Print("\x1B|rA(Right)ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQR" + "\n", true);
206 }
207
208 private void TestMuntiPages()
209 {
210 Print("\x1B|3C" + "\x1B|cA" + "Multiple Pages" + "\n\n", true);
211
212 for (int i = 0; i < 148; i++)
213 Print(string.Format(" {0} ABCDEFGHIJKLMNOPQRSTUVWXYZ", i), true);
214
215 }
216
217 /// <summary>
218 /// 打印帮助类
219 /// </summary>
220 public class PrinterHelper
221 {
222 public string Text;
223 public SolidBrush Brush;
224 public Font Font;
225 public PrintAlignment Alignment;
226
227 public PrinterHelper(string text, SolidBrush brush, Font font, PrintAlignment alignment)
228 {
229 this.Text = text;
230 this.Brush = brush;
231 this.Font = font;
232 this.Alignment = alignment;
233 }
234 }
235 public enum PrintAlignment
236 {
237 Left,
238 Center,
239 Right
240 }


浙公网安备 33010602011771号