C# winform 打印类

 1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Windows.Forms;
5 using System.Drawing.Printing;
6 using System.Drawing;
7
8 namespace Etaocn.Util
9 {
10 /**//// <summary>
11 /// 创建人 贺挺
12 /// </summary>

13 public class Printer
14 {
15 private DataGridView dataview;
16 private PrintDocument printDoc;
17 //打印有效区域的宽度
18 int width;
19 int height;
20 int columns;
21 double Rate;
22 bool hasMorePage = false;
23 int currRow = 0;
24 int rowHeight = 20;
25 //打印页数
26 int PageNumber;
27 //当前打印页的行数
28 int pageSize = 20;
29 //当前打印的页码
30 int PageIndex;
31
32 private int PageWidth; //打印纸的宽度
33 private int PageHeight; //打印纸的高度
34 private int LeftMargin; //有效打印区距离打印纸的左边大小
35 private int TopMargin;//有效打印区距离打印纸的上面大小
36 private int RightMargin;//有效打印区距离打印纸的右边大小
37 private int BottomMargin;//有效打印区距离打印纸的下边大小
38
39 int rows;
40
41 /**//// <summary>
42 /// 构造函数
43 /// </summary>
44 /// <param name="dataview">要打印的DateGridView</param>
45 /// <param name="printDoc">PrintDocument用于获取打印机的设置</param>

46 public Printer(DataGridView dataview, PrintDocument printDoc)
47 {
48 this.dataview = dataview;
49 this.printDoc = printDoc;
50 PageIndex = 0;
51 //获取打印数据的具体行数
52 this.rows = dataview.RowCount;
53
54 this.columns = dataview.ColumnCount;
55 //判断打印设置是否是横向打印
56 if (!printDoc.DefaultPageSettings.Landscape)
57 {
58
59 PageWidth = printDoc.DefaultPageSettings.PaperSize.Width;
60 PageHeight = printDoc.DefaultPageSettings.PaperSize.Height;
61
62 }

63 else
64 {
65
66 PageHeight = printDoc.DefaultPageSettings.PaperSize.Width;
67 PageWidth = printDoc.DefaultPageSettings.PaperSize.Height;
68
69 }

70 LeftMargin = printDoc.DefaultPageSettings.Margins.Left;
71 TopMargin = printDoc.DefaultPageSettings.Margins.Top;
72 RightMargin = printDoc.DefaultPageSettings.Margins.Right;
73 BottomMargin = printDoc.DefaultPageSettings.Margins.Bottom;
74
75
76 height = PageHeight - TopMargin - BottomMargin - 2;
77 width = PageWidth - LeftMargin - RightMargin - 2;
78
79 double tempheight = height;
80 double temprowHeight = rowHeight;
81 while (true)
82 {
83 string temp = Convert.ToString(tempheight / Math.Round(temprowHeight, 3));
84 int i = temp.IndexOf('.');
85 double tt = 100;
86 if (i != -1)
87 {
88 tt = Math.Round(Convert.ToDouble(temp.Substring(temp.IndexOf('.'))), 3);
89 }

90 if (tt <= 0.01)
91 {
92 rowHeight = Convert.ToInt32(temprowHeight);
93 break;
94 }

95 else
96 {
97 temprowHeight = temprowHeight + 0.01;
98
99 }

100 }

101 pageSize = height / rowHeight;
102 if ((rows + 1) <= pageSize)
103 {
104 pageSize = rows + 1;
105 PageNumber = 1;
106 }

107 else
108 {
109 PageNumber = rows / (pageSize - 1);
110 if (rows % (pageSize - 1) != 0)
111 {
112 PageNumber = PageNumber + 1;
113 }

114
115 }

116
117
118
119 }

120
121
122
123 /**//// <summary>
124 /// 初始化打印
125 /// </summary>

126 private void InitPrint()
127 {
128 PageIndex = PageIndex + 1;
129 if (PageIndex == PageNumber)
130 {
131 hasMorePage = false;
132 if (PageIndex != 1)
133 {
134 pageSize = rows % (pageSize - 1) + 1;
135 }

136 }

137 else
138 {
139 hasMorePage = true;
140 }

141
142
143
144 }

145 //打印头
146 private void DrawHeader(Graphics g)
147 {
148
149 Font font = new Font("宋体", 12, FontStyle.Bold);
150 int temptop = (rowHeight / 2) + TopMargin + 1;
151 int templeft = LeftMargin + 1;
152
153 for (int i = 0; i < this.columns; i++)
154 {
155 string headString = this.dataview.Columns[i].HeaderText;
156 float fontHeight = g.MeasureString(headString, font).Height;
157 float fontwidth = g.MeasureString(headString, font).Width;
158 float temp = temptop - (fontHeight) / 3;
159 g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));
160 templeft = templeft + (int)(this.dataview.Columns[i].Width / Rate) + 1;
161 }

162
163 }

164 //画表格
165 private void DrawTable(Graphics g)
166 {
167
168 Rectangle border = new Rectangle(LeftMargin, TopMargin, width, (pageSize) * rowHeight);
169 g.DrawRectangle(new Pen(Brushes.Black, 2), border);
170 for (int i = 1; i < pageSize; i++)
171 {
172 if (i != 1)
173 {
174 g.DrawLine(new Pen(Brushes.Black, 1), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
175 }

176 else
177 {
178 g.DrawLine(new Pen(Brushes.Black, 2), new Point(LeftMargin + 1, (rowHeight * i) + TopMargin + 1), new Point(width + LeftMargin, (rowHeight * i) + TopMargin + 1));
179 }

180 }

181
182 //计算出列的总宽度和打印纸比率
183 Rate = Convert.ToDouble(GetDateViewWidth()) / Convert.ToDouble(width);
184 int tempLeft = LeftMargin + 1;
185 int endY = (pageSize) * rowHeight + TopMargin;
186 for (int i = 1; i < columns; i++)
187 {
188 tempLeft = tempLeft + 1 + (int)(this.dataview.Columns[i - 1].Width / Rate);
189 g.DrawLine(new Pen(Brushes.Black, 1), new Point(tempLeft, TopMargin), new Point(tempLeft, endY));
190 }

191
192 }

193 /**//// <summary>
194 /// 获取打印的列的总宽度
195 /// </summary>
196 /// <returns></returns>

197 private int GetDateViewWidth()
198 {
199 int total = 0;
200 for (int i = 0; i < this.columns; i++)
201 {
202 total = total + this.dataview.Columns[i].Width;
203 }

204 return total;
205 }

206
207 //打印行数据
208 private void DrawRows(Graphics g)
209 {
210
211 Font font = new Font("宋体", 12, FontStyle.Regular);
212 int temptop = (rowHeight / 2) + TopMargin + 1 + rowHeight;
213
214
215 for (int i = currRow; i < pageSize + currRow - 1; i++)
216 {
217 int templeft = LeftMargin + 1;
218 for (int j = 0; j < columns; j++)
219 {
220 string headString = this.dataview.Rows[i].Cells[j].Value.ToString();
221 float fontHeight = g.MeasureString(headString, font).Height;
222 float fontwidth = g.MeasureString(headString, font).Width;
223 float temp = temptop - (fontHeight) / 3;
224 while (true)
225 {
226 if (fontwidth <= (int)(this.dataview.Columns[j].Width / Rate))
227 {
228 break;
229 }

230 else
231 {
232 headString = headString.Substring(0, headString.Length - 1);
233 fontwidth = g.MeasureString(headString, font).Width;
234 }

235 }

236 g.DrawString(headString, font, Brushes.Black, new PointF(templeft, temp));
237
238 templeft = templeft + (int)(this.dataview.Columns[j].Width / Rate) + 1;
239 }

240
241 temptop = temptop + rowHeight;
242
243
244 }

245 currRow = pageSize + currRow - 1;
246
247 }

248
249 /**//// <summary>
250 /// 在PrintDocument中的PrintPage方法中调用
251 /// </summary>
252 /// <param name="g">传入PrintPage中PrintPageEventArgs中的Graphics</param>
253 /// <returns>是否还有打印页 有返回true,无则返回false</returns>

254 public bool Print(Graphics g)
255 {
256 InitPrint();
257 DrawTable(g);
258 DrawHeader(g);
259 DrawRows(g);
260
261 //打印页码
262 string pagestr = PageIndex + " / " + PageNumber;
263 Font font = new Font("宋体", 12, FontStyle.Regular);
264 g.DrawString(pagestr, font, Brushes.Black, new PointF((PageWidth / 2) - g.MeasureString(pagestr, font).Width, PageHeight - (BottomMargin / 2) - g.MeasureString(pagestr, font).Height));
265 //打印查询的功能项名称
266 string temp = dataview.Tag.ToString() + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm");
267 g.DrawString(temp, font, Brushes.Black, new PointF(PageWidth - 5 - g.MeasureString(temp, font).Width, PageHeight - 5 - g.MeasureString(temp, font).Height));
268 return hasMorePage;
269 }

270
271
272
273
274
275
276 }

277}

278
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值