复制代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308

Source Codeusing System;
using System.Text;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.IO;
namespace TextPrinter
{
public class TextPrinter
{
private string m_Title;
private string m_Context;
private Font m_Font;
private Font m_TitleFont;
private SizeF m_WordSize;
private int m_CurrentPage;
private Rectangle m_PrintableArea;
private string m_LeftContext;
private int m_RowPadding;
private TitleType m_TitleType;
private FooterType m_FooterType;
//private int m_TotalPage;
//private bool m_Prepare;
private string m_FooterFormat;
private PrintDocument printDoc;
public TextPrinter()
{
printDoc = new PrintDocument();
printDoc.PrintPage += new PrintPageEventHandler(InternalPrintPage);
m_Font = SystemFonts.DefaultFont;
m_TitleFont = SystemFonts.CaptionFont;
m_TitleType = TitleType.None;
m_FooterType = FooterType.None;
FooterFormat = "第{0}页";
}
///
/// Report Title
///
public string Title
{
get { return m_Title; }
set { m_Title = value; }
}
///
/// Report Context string
///
public string Context
{
get { return m_Context; }
set { m_Context = value; }
}
///
/// Report Context Font
///
public Font Font
{
get { return m_Font; }
set { m_Font = value; }
}
///
/// Report Title Font
///
public Font TitleFont
{
get { return m_TitleFont; }
set { m_TitleFont = value; }
}
///
/// Report Title Type
///
public TitleType TitleType
{
get { return m_TitleType; }
set { m_TitleType = value; }
}
///
/// Report Footer Type
///
public FooterType FooterType
{
get { return m_FooterType; }
set { m_FooterType = value; }
}
///
/// Report Footer Format. etc "第{0}页"
///
public string FooterFormat
{
get { return m_FooterFormat; }
set { m_FooterFormat = value; }
}
//public int TotalPage
//{
//
get { return m_TotalPage; }
//}
///
/// Report row padding. 行间距
///
public int RowPadding
{
get { return m_RowPadding; }
set { m_RowPadding = value; }
}
///
/// 初始化打印
///
private void InitPrint()
{
m_CurrentPage = 1;
}
///
/// 打印机设置
///
public void PrintSetting()
{
PrintDialog pd = new PrintDialog();
pd.Document = printDoc;
pd.ShowDialog();
}
///
/// 纸张设置
///
public void PageSetting()
{
PageSetupDialog psd = new PageSetupDialog();
psd.Document = printDoc;
psd.ShowDialog();
}
///
/// 预览报表
///
public void Preview()
{
InitPrint();
PrintPreviewDialog pdlg = new PrintPreviewDialog();
pdlg.Document = printDoc;
pdlg.WindowState = FormWindowState.Maximized;
pdlg.PrintPreviewControl.Zoom = 1;
//printDoc.Print();
//m_Prepare = true;
pdlg.ShowDialog();
}
///
/// 打印报表
///
public void Print()
{
InitPrint();
printDoc.Print();
}
///
/// Read a line from buffer,
///
///
///
private string GetLine()
{
string tmp = String.Empty;
try
{
SizeF size;
for (int i = 0; i < m_LeftContext.Length; i++)
{
size = TextRenderer.MeasureText(m_LeftContext.Substring(0, i), Font);
if (size.Width > m_PrintableArea.Width)
{
tmp = m_LeftContext.Substring(0, i - 1);
m_LeftContext = m_LeftContext.Substring(i - 1);
return tmp;
}
if (m_LeftContext.Substring(0, i).IndexOf("/n", 0) >= 0)
{
tmp = m_LeftContext.Substring(0, i);
m_LeftContext = m_LeftContext.Substring(i);
return tmp;
}
}
tmp = m_LeftContext;
m_LeftContext = String.Empty;
return tmp;
}
finally
{
tmp = tmp.Replace("/n", "");
}
}
///
/// internal print method
///
///
///
private void InternalPrintPage(object sender, PrintPageEventArgs e)
{
int top = 0;
if (m_CurrentPage == 1)
{
m_LeftContext = Context;
m_LeftContext = m_LeftContext.Replace("/r", "");
m_PrintableArea = e.MarginBounds;
m_WordSize = TextRenderer.MeasureText("W", Font);
if (TitleType == TitleType.OnlyFirstPage)
top += DrawTitle(e.Graphics);
}
if (TitleType == TitleType.AllPage)
top += DrawTitle(e.Graphics);
while (top < m_PrintableArea.Height - (int)Math.Floor(m_WordSize.Height))
{
string tmp = GetLine();
e.Graphics.DrawString(tmp, Font, SystemBrushes.MenuText, e.MarginBounds.X, e.MarginBounds.Y + top);
top += (int)Math.Floor(m_WordSize.Height) + RowPadding;
}
if (FooterType != FooterType.None)
DrawFooter(e.Graphics);
e.HasMorePages = m_LeftContext != string.Empty;
m_CurrentPage++;
}
///
/// Print Report Title
///
///
///
private int DrawTitle(Graphics g)
{
SizeF size = g.MeasureString(Title, TitleFont);
DrawString(g, Title, TitleFont, SystemBrushes.WindowText, m_PrintableArea, StringAlignment.Center, StringAlignment.Near);
return (int)Math.Floor(size.Height);
}
///
/// Print Report Footer
///
///
private void DrawFooter(Graphics g)
{
string tmp = string.Empty;
if (FooterType == FooterType.OnlyPageNumber)
tmp = string.Format(FooterFormat, m_CurrentPage);
//else if (FooterType == FooterType.PageNumOfTotal)
//
tmp = string.Format("{0} of {1}", m_CurrentPage, 0);
SizeF size = g.MeasureString(tmp, SystemFonts.DefaultFont);
DrawString(g, tmp, SystemFonts.DefaultFont, SystemBrushes.WindowText,
new Rectangle(m_PrintableArea.X, m_PrintableArea.Bottom - (int)size.Height,
m_PrintableArea.Width, (int)size.Height), StringAlignment.Far, StringAlignment.Center);
}
///
/// Draw a string with alignment parameter
///
///
///
///
///
///
///
///
private void DrawString(Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment)
{
StringFormat sf = new StringFormat();
sf.Alignment = alignment;
sf.LineAlignment = lineAlignment;
g.DrawString(s, font, brush, rect, sf);
}
public static void Preview(string pStr)
{
TextPrinter textPrinter = new TextPrinter();
textPrinter.Context = pStr;
textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel);
textPrinter.Title = "打印测试页";
textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel);
textPrinter.TitleType = TitleType.AllPage;
textPrinter.FooterType = FooterType.OnlyPageNumber;
textPrinter.FooterFormat = "Page {0}";
//textPrinter.PrintSetting();
//textPrinter.PageSetting();
textPrinter.Preview();
//textPrinter.Print();
}
public static void Print(string pStr)
{
TextPrinter textPrinter = new TextPrinter();
textPrinter.Context = pStr;
textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel);
textPrinter.Title = "打印测试页";
textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel);
textPrinter.TitleType = TitleType.AllPage;
textPrinter.FooterType = FooterType.OnlyPageNumber;
textPrinter.FooterFormat = "Page {0}";
//textPrinter.PrintSetting();
//textPrinter.PageSetting();
//textPrinter.Preview();
textPrinter.Print();
}
}
public enum TitleType
{
None,
OnlyFirstPage,
AllPage
}
public enum FooterType
{
None,
OnlyPageNumber
//PageNumOfTotal
}
}
发表评论 取消回复