1
using System;
2
using System.Text;
3
using System.Drawing;
4
using System.Drawing.Printing;
5
using System.Windows.Forms;
6
using System.IO;
7![]()
8
namespace TextPrinter
9
{
10
public class TextPrinter
11
{
12
private string m_Title;
13
private string m_Context;
14
private Font m_Font;
15
private Font m_TitleFont;
16
private SizeF m_WordSize;
17
private int m_CurrentPage;
18
private Rectangle m_PrintableArea;
19
private string m_LeftContext;
20
private int m_RowPadding;
21
private TitleType m_TitleType;
22
private FooterType m_FooterType;
23
//private int m_TotalPage;
24
//private bool m_Prepare;
25
private string m_FooterFormat;
26
private PrintDocument printDoc;
27![]()
28
public TextPrinter()
29
{
30
printDoc = new PrintDocument();
31
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);
32
m_Font = SystemFonts.DefaultFont;
33
m_TitleFont = SystemFonts.CaptionFont;
34
m_TitleType = TitleType.None;
35
m_FooterType = FooterType.None;
36
FooterFormat = "第{0}页";
37
}
38![]()
39
/// <summary>
40
/// Report Title
41
/// </summary>
42
public string Title
43
{
44
get { return m_Title; }
45
set { m_Title = value; }
46
}
47![]()
48
/// <summary>
49
/// Report Context string
50
/// </summary>
51
public string Context
52
{
53
get { return m_Context; }
54
set { m_Context = value; }
55
}
56![]()
57
/// <summary>
58
/// Report Context Font
59
/// </summary>
60
public Font Font
61
{
62
get { return m_Font; }
63
set { m_Font = value; }
64
}
65![]()
66
/// <summary>
67
/// Report Title Font
68
/// </summary>
69
public Font TitleFont
70
{
71
get { return m_TitleFont; }
72
set { m_TitleFont = value; }
73
}
74![]()
75
/// <summary>
76
/// Report Title Type
77
/// </summary>
78
public TitleType TitleType
79
{
80
get { return m_TitleType; }
81
set { m_TitleType = value; }
82
}
83![]()
84
/// <summary>
85
/// Report Footer Type
86
/// </summary>
87
public FooterType FooterType
88
{
89
get { return m_FooterType; }
90
set { m_FooterType = value; }
91
}
92![]()
93
/// <summary>
94
/// Report Footer Format. etc "第{0}页"
95
/// </summary>
96
public string FooterFormat
97
{
98
get { return m_FooterFormat; }
99
set { m_FooterFormat = value; }
100
}
101![]()
102
//public int TotalPage
103
//{
104
// get { return m_TotalPage; }
105
//}
106![]()
107
/// <summary>
108
/// Report row padding. 行间距
109
/// </summary>
110
public int RowPadding
111
{
112
get { return m_RowPadding; }
113
set { m_RowPadding = value; }
114
}
115![]()
116
/// <summary>
117
/// 初始化打印
118
/// </summary>
119
private void InitPrint()
120
{
121
m_CurrentPage = 1;
122
}
123![]()
124
/// <summary>
125
/// 打印机设置
126
/// </summary>
127
public void PrintSetting()
128
{
129
PrintDialog pd = new PrintDialog();
130
pd.Document = printDoc;
131
pd.ShowDialog();
132
}
133![]()
134
/// <summary>
135
/// 纸张设置
136
/// </summary>
137
public void PageSetting()
138
{
139
PageSetupDialog psd = new PageSetupDialog();
140
psd.Document = printDoc;
141
psd.ShowDialog();
142
}
143![]()
144
/// <summary>
145
/// 预览报表
146
/// </summary>
147
public void Preview()
148
{
149
InitPrint();
150
PrintPreviewDialog pdlg = new PrintPreviewDialog();
151
pdlg.Document = printDoc;
152
pdlg.WindowState = FormWindowState.Maximized;
153
pdlg.PrintPreviewControl.Zoom = 1;
154
//printDoc.Print();
155
//m_Prepare = true;
156
pdlg.ShowDialog();
157
}
158
159
/// <summary>
160
/// 打印报表
161
/// </summary>
162
public void Print()
163
{
164
InitPrint();
165
printDoc.Print();
166
}
167![]()
168
/// <summary>
169
/// 从内容Context中读出一行
170
/// </summary>
171
/// <param name="g"></param>
172
/// <returns></returns>
173
private string GetLine(Graphics g)
174
{
175
string tmp = String.Empty;
176
try
177
{
178
SizeF size;
179
for (int i = 0; i < m_LeftContext.Length; i++)
180
{
181
size = g.MeasureString(m_LeftContext.Substring(0, i), Font);
182
if (size.Width > m_PrintableArea.Width)
183
{
184
tmp = m_LeftContext.Substring(0, i - 1);
185
m_LeftContext = m_LeftContext.Substring(i - 1);
186
return tmp;
187
}
188
if (m_LeftContext.Substring(0, i).IndexOf("\n", 0) >= 0)
189
{
190
tmp = m_LeftContext.Substring(0, i);
191
m_LeftContext = m_LeftContext.Substring(i);
192
return tmp;
193
}
194
}
195
tmp = m_LeftContext;
196
m_LeftContext = String.Empty;
197
return tmp;
198
}
199
finally
200
{
201
tmp = tmp.Replace("\n", "");
202
}
203
}
204![]()
205
/// <summary>
206
/// 打印主过程
207
/// </summary>
208
/// <param name="sender"></param>
209
/// <param name="e"></param>
210
private void pd_PrintPage(object sender, PrintPageEventArgs e)
211
{
212
int top = 0;
213
if (m_CurrentPage == 1)
214
{
215
m_LeftContext = Context;
216
m_LeftContext = m_LeftContext.Replace("\r", "");
217
m_PrintableArea = e.MarginBounds;
218
m_WordSize = e.Graphics.MeasureString("W", Font);
219
if (TitleType == TitleType.OnlyFirstPage)
220
top += DrawTitle(e.Graphics);
221
}
222
if (TitleType == TitleType.AllPage)
223
top += DrawTitle(e.Graphics);
224
while (top < m_PrintableArea.Height - (int)Math.Floor(m_WordSize.Height))
225
{
226
string tmp = GetLine(e.Graphics);
227
e.Graphics.DrawString(tmp, Font, SystemBrushes.MenuText, e.MarginBounds.X, e.MarginBounds.Y + top);
228
top += (int)Math.Floor(m_WordSize.Height) + RowPadding;
229
}
230
if (FooterType != FooterType.None)
231
DrawFooter(e.Graphics);
232
e.HasMorePages = m_LeftContext != string.Empty;
233
m_CurrentPage++;
234
}
235![]()
236
/// <summary>
237
/// Print Report Title
238
/// </summary>
239
/// <param name="g"></param>
240
/// <returns></returns>
241
private int DrawTitle(Graphics g)
242
{
243
SizeF size = g.MeasureString(Title, TitleFont);
244
DrawString(g, Title, TitleFont, SystemBrushes.WindowText, m_PrintableArea, StringAlignment.Center, StringAlignment.Near);
245![]()
246
return (int)Math.Floor(size.Height);
247
}
248![]()
249
/// <summary>
250
/// Print Report Footer
251
/// </summary>
252
/// <param name="g"></param>
253
private void DrawFooter(Graphics g)
254
{
255
string tmp = string.Empty;
256
if (FooterType == FooterType.OnlyPageNum)
257
tmp = string.Format(FooterFormat, m_CurrentPage);
258
//else if (FooterType == FooterType.PageNumOfTotal)
259
// tmp = string.Format("{0} of {1}", m_CurrentPage, 0);
260
SizeF size = g.MeasureString(tmp, SystemFonts.DefaultFont);
261
DrawString(g, tmp, SystemFonts.DefaultFont, SystemBrushes.WindowText,
262
new Rectangle(m_PrintableArea.X, m_PrintableArea.Bottom - (int)size.Height,
263
m_PrintableArea.Width, (int)size.Height), StringAlignment.Far, StringAlignment.Center);
264
}
265![]()
266
/// <summary>
267
/// Draw a string with alignment parameter
268
/// </summary>
269
/// <param name="g"></param>
270
/// <param name="s"></param>
271
/// <param name="font"></param>
272
/// <param name="brush"></param>
273
/// <param name="rect"></param>
274
/// <param name="alignment"></param>
275
/// <param name="lineAlignment"></param>
276
private void DrawString(Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment)
277
{
278
StringFormat sf = new StringFormat();
279
sf.Alignment = alignment;
280
sf.LineAlignment = lineAlignment;
281![]()
282
g.DrawString(s, font, brush, rect, sf);
283
}
284![]()
285
public static void TestPreview(string pStr)
286
{
287
TextPrinter textPrinter = new TextPrinter();
288
textPrinter.Context = pStr;
289
textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel);
290
textPrinter.Title = "打印测试页";
291
textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel);
292
textPrinter.TitleType = TitleType.AllPage;
293
textPrinter.FooterType = FooterType.OnlyPageNum;
294
textPrinter.FooterFormat = "Page {0}";
295
//textPrinter.PrintSetting();
296
//textPrinter.PageSetting();
297
textPrinter.Preview();
298
//textPrinter.Print();
299
}
300
}
301![]()
302
public enum TitleType
303
{
304
None,
305
OnlyFirstPage,
306
AllPage
307
}
308![]()
309
public enum FooterType
310
{
311
None,
312
OnlyPageNum
313
//PageNumOfTotal
314
}
315
}
316![]()
using System;2
using System.Text;3
using System.Drawing;4
using System.Drawing.Printing;5
using System.Windows.Forms;6
using System.IO;7

8
namespace TextPrinter9
{10
public class TextPrinter11
{12
private string m_Title;13
private string m_Context;14
private Font m_Font;15
private Font m_TitleFont;16
private SizeF m_WordSize;17
private int m_CurrentPage;18
private Rectangle m_PrintableArea;19
private string m_LeftContext;20
private int m_RowPadding;21
private TitleType m_TitleType;22
private FooterType m_FooterType;23
//private int m_TotalPage;24
//private bool m_Prepare;25
private string m_FooterFormat;26
private PrintDocument printDoc;27

28
public TextPrinter()29
{30
printDoc = new PrintDocument();31
printDoc.PrintPage += new PrintPageEventHandler(pd_PrintPage);32
m_Font = SystemFonts.DefaultFont;33
m_TitleFont = SystemFonts.CaptionFont;34
m_TitleType = TitleType.None;35
m_FooterType = FooterType.None;36
FooterFormat = "第{0}页";37
}38

39
/// <summary>40
/// Report Title41
/// </summary>42
public string Title43
{44
get { return m_Title; }45
set { m_Title = value; }46
}47

48
/// <summary>49
/// Report Context string50
/// </summary>51
public string Context52
{53
get { return m_Context; }54
set { m_Context = value; }55
}56

57
/// <summary>58
/// Report Context Font59
/// </summary>60
public Font Font61
{62
get { return m_Font; }63
set { m_Font = value; }64
}65

66
/// <summary>67
/// Report Title Font68
/// </summary>69
public Font TitleFont70
{71
get { return m_TitleFont; }72
set { m_TitleFont = value; }73
}74

75
/// <summary>76
/// Report Title Type77
/// </summary>78
public TitleType TitleType79
{80
get { return m_TitleType; }81
set { m_TitleType = value; }82
}83

84
/// <summary>85
/// Report Footer Type86
/// </summary>87
public FooterType FooterType88
{89
get { return m_FooterType; }90
set { m_FooterType = value; }91
}92

93
/// <summary>94
/// Report Footer Format. etc "第{0}页"95
/// </summary>96
public string FooterFormat97
{98
get { return m_FooterFormat; }99
set { m_FooterFormat = value; }100
}101

102
//public int TotalPage103
//{104
// get { return m_TotalPage; }105
//}106

107
/// <summary>108
/// Report row padding. 行间距109
/// </summary>110
public int RowPadding111
{112
get { return m_RowPadding; }113
set { m_RowPadding = value; }114
}115

116
/// <summary>117
/// 初始化打印118
/// </summary>119
private void InitPrint()120
{121
m_CurrentPage = 1;122
}123

124
/// <summary>125
/// 打印机设置126
/// </summary>127
public void PrintSetting()128
{129
PrintDialog pd = new PrintDialog();130
pd.Document = printDoc;131
pd.ShowDialog();132
}133

134
/// <summary>135
/// 纸张设置136
/// </summary>137
public void PageSetting()138
{139
PageSetupDialog psd = new PageSetupDialog();140
psd.Document = printDoc;141
psd.ShowDialog();142
}143

144
/// <summary>145
/// 预览报表146
/// </summary>147
public void Preview()148
{149
InitPrint();150
PrintPreviewDialog pdlg = new PrintPreviewDialog();151
pdlg.Document = printDoc;152
pdlg.WindowState = FormWindowState.Maximized;153
pdlg.PrintPreviewControl.Zoom = 1;154
//printDoc.Print();155
//m_Prepare = true;156
pdlg.ShowDialog();157
}158
159
/// <summary>160
/// 打印报表161
/// </summary>162
public void Print()163
{164
InitPrint();165
printDoc.Print();166
}167

168
/// <summary>169
/// 从内容Context中读出一行170
/// </summary>171
/// <param name="g"></param>172
/// <returns></returns>173
private string GetLine(Graphics g)174
{175
string tmp = String.Empty;176
try177
{178
SizeF size;179
for (int i = 0; i < m_LeftContext.Length; i++)180
{181
size = g.MeasureString(m_LeftContext.Substring(0, i), Font);182
if (size.Width > m_PrintableArea.Width)183
{184
tmp = m_LeftContext.Substring(0, i - 1);185
m_LeftContext = m_LeftContext.Substring(i - 1);186
return tmp;187
}188
if (m_LeftContext.Substring(0, i).IndexOf("\n", 0) >= 0)189
{190
tmp = m_LeftContext.Substring(0, i);191
m_LeftContext = m_LeftContext.Substring(i);192
return tmp;193
}194
}195
tmp = m_LeftContext;196
m_LeftContext = String.Empty;197
return tmp;198
}199
finally200
{201
tmp = tmp.Replace("\n", "");202
}203
}204

205
/// <summary>206
/// 打印主过程207
/// </summary>208
/// <param name="sender"></param>209
/// <param name="e"></param>210
private void pd_PrintPage(object sender, PrintPageEventArgs e)211
{212
int top = 0;213
if (m_CurrentPage == 1)214
{215
m_LeftContext = Context;216
m_LeftContext = m_LeftContext.Replace("\r", "");217
m_PrintableArea = e.MarginBounds;218
m_WordSize = e.Graphics.MeasureString("W", Font);219
if (TitleType == TitleType.OnlyFirstPage)220
top += DrawTitle(e.Graphics);221
}222
if (TitleType == TitleType.AllPage)223
top += DrawTitle(e.Graphics);224
while (top < m_PrintableArea.Height - (int)Math.Floor(m_WordSize.Height))225
{226
string tmp = GetLine(e.Graphics);227
e.Graphics.DrawString(tmp, Font, SystemBrushes.MenuText, e.MarginBounds.X, e.MarginBounds.Y + top);228
top += (int)Math.Floor(m_WordSize.Height) + RowPadding;229
}230
if (FooterType != FooterType.None)231
DrawFooter(e.Graphics);232
e.HasMorePages = m_LeftContext != string.Empty;233
m_CurrentPage++;234
}235

236
/// <summary>237
/// Print Report Title238
/// </summary>239
/// <param name="g"></param>240
/// <returns></returns>241
private int DrawTitle(Graphics g)242
{243
SizeF size = g.MeasureString(Title, TitleFont);244
DrawString(g, Title, TitleFont, SystemBrushes.WindowText, m_PrintableArea, StringAlignment.Center, StringAlignment.Near);245

246
return (int)Math.Floor(size.Height);247
}248

249
/// <summary>250
/// Print Report Footer251
/// </summary>252
/// <param name="g"></param>253
private void DrawFooter(Graphics g)254
{255
string tmp = string.Empty;256
if (FooterType == FooterType.OnlyPageNum)257
tmp = string.Format(FooterFormat, m_CurrentPage);258
//else if (FooterType == FooterType.PageNumOfTotal)259
// tmp = string.Format("{0} of {1}", m_CurrentPage, 0);260
SizeF size = g.MeasureString(tmp, SystemFonts.DefaultFont);261
DrawString(g, tmp, SystemFonts.DefaultFont, SystemBrushes.WindowText,262
new Rectangle(m_PrintableArea.X, m_PrintableArea.Bottom - (int)size.Height,263
m_PrintableArea.Width, (int)size.Height), StringAlignment.Far, StringAlignment.Center);264
}265

266
/// <summary>267
/// Draw a string with alignment parameter268
/// </summary>269
/// <param name="g"></param>270
/// <param name="s"></param>271
/// <param name="font"></param>272
/// <param name="brush"></param>273
/// <param name="rect"></param>274
/// <param name="alignment"></param>275
/// <param name="lineAlignment"></param>276
private void DrawString(Graphics g, string s, Font font, Brush brush, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment)277
{278
StringFormat sf = new StringFormat();279
sf.Alignment = alignment;280
sf.LineAlignment = lineAlignment;281

282
g.DrawString(s, font, brush, rect, sf);283
}284

285
public static void TestPreview(string pStr)286
{287
TextPrinter textPrinter = new TextPrinter();288
textPrinter.Context = pStr;289
textPrinter.Font = new Font("宋体", 16, FontStyle.Regular, GraphicsUnit.Pixel);290
textPrinter.Title = "打印测试页";291
textPrinter.TitleFont = new Font("宋体", 24, FontStyle.Bold, GraphicsUnit.Pixel);292
textPrinter.TitleType = TitleType.AllPage;293
textPrinter.FooterType = FooterType.OnlyPageNum;294
textPrinter.FooterFormat = "Page {0}";295
//textPrinter.PrintSetting();296
//textPrinter.PageSetting();297
textPrinter.Preview();298
//textPrinter.Print();299
}300
}301

302
public enum TitleType303
{304
None,305
OnlyFirstPage,306
AllPage307
}308

309
public enum FooterType310
{311
None,312
OnlyPageNum313
//PageNumOfTotal314
}315
}316



浙公网安备 33010602011771号