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