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
/// 創(chuàng)建人 賀挺
12
/// </summary>
13
public class Printer
14
{
15
private DataGridView dataview;
16
private PrintDocument printDoc;
17
//打印有效區(qū)域的寬度
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
//打印頁數(shù)
26
int PageNumber;
27
//當(dāng)前打印頁的行數(shù)
28
int pageSize = 20;
29
//當(dāng)前打印的頁碼
30
int PageIndex;
31
32
private int PageWidth; //打印紙的寬度
33
private int PageHeight; //打印紙的高度
34
private int LeftMargin; //有效打印區(qū)距離打印紙的左邊大小
35
private int TopMargin;//有效打印區(qū)距離打印紙的上面大小
36
private int RightMargin;//有效打印區(qū)距離打印紙的右邊大小
37
private int BottomMargin;//有效打印區(qū)距離打印紙的下邊大小
38
39
int rows;
40
41
/// <summary>
42
/// 構(gòu)造函數(shù)
43
/// </summary>
44
/// <param name="dataview">要打印的DateGridView</param>
45
/// <param name="printDoc">PrintDocument用于獲取打印機的設(shè)置</param>
46
public Printer(DataGridView dataview, PrintDocument printDoc)
47
{
48
this.dataview = dataview;
49
this.printDoc = printDoc;
50
PageIndex = 0;
51
//獲取打印數(shù)據(jù)的具體行數(shù)
52
this.rows = dataview.RowCount;
53
54
this.columns = dataview.ColumnCount;
55
//判斷打印設(shè)置是否是橫向打印
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
//打印行數(shù)據(jù)
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方法中調(diào)用
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

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
