iTextSharp

http://sourceforge.net/projects/itextsharp/files/itextsharp/

导出样式如下

生成文档样式

下边是生成所示文档的代码

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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
public void Export_Pdf(string amp_name, string barcode, string year_week, List<DataVoltage> dv_list)
{
for (Int32 i = 0; i < uIData.SampleNum; i++)
{
DataVoltage dv = new DataVoltage();
dv.V1 = 65.23;
dv.V2 = 64.23;
dv.V3 = 65.36;
dv.V4 = 66.67;
dv.Time = "12:45:58";
dv.result = "OK";
dv_list.Add(dv);
}

//设置纸张大小,自定义大小
//iTextSharp.text.Rectangle pageSize = new iTextSharp.text.Rectangle(216f, 716f);
//pageSize.BackgroundColor = new iTextSharp.text.BaseColor(0xFF, 0xFF, 0xDE);
//new Document(pageSize, 36f, 72f, 108f, 180f))
//边距单位:mm
using (Document document = new Document(iTextSharp.text.PageSize.A4))
{
PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("test2.pdf", FileMode.Create));
document.Open();

// 添加文档信息
document.AddTitle("ATLAS振动台电压记录表");
document.AddSubject("振动过程中电压检测");
document.AddKeywords("ATLAS 振动 电压 记录");
document.AddCreator("SONAVOX");
document.AddAuthor("87000013");


// 新宋体字,后面的1是索引,索引从0开始,索引的可选项: 0, 1 ;不可省略,因宋体字有两种,宋体,新宋
string fontFile = @"C:\Windows\Fonts\SIMSUN.TTC,1";
// 字体
BaseFont bFont = BaseFont.CreateFont(fontFile, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
iTextSharp.text.Font font = new iTextSharp.text.Font(bFont, 18.0f, Font.BOLD);

PdfPTable table = new PdfPTable(7);
// 使用chuck 可有效的输出文字
// 也可使用 document.Add(new iTextSharp.text.Paragraph("Hello World"));
Chunk chuck1 = new iTextSharp.text.Chunk("ATLAS振动台电压记录表\r\n" + amp_name);
// 文字字体
chuck1.Font = font;
PdfPCell cell = new PdfPCell(new Phrase(chuck1));
// 表头单元格占的列数,7列
cell.Colspan = 7;
// 单元格占的行数,3行
cell.Rowspan = 1;
// 边框
cell.BorderWidth = 0.2f;
// 边框颜色
cell.BorderColor = BaseColor.BLACK;
//背景姿色
//cell.BackgroundColor = new BaseColor(252, 213, 180);
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
// 添加单元格
table.AddCell(cell);
string[] header = new string[] { //注意是6列,最后2列合并
"开始时间:", "12:12:23", "结束时间:", "12:13:50", "仪器型号", "GW.Inc,GDM-8246,FW2.02",
"桥接输出:", "YES", "生产周期:"," 23 年,49 周","产品条码:", "AA1006080234900008",
"上限电压:", "50V", "下限电压:", "30V", "记录周期:", "1秒/点",
};

header[1] = startTime; //开始时间
header[3] = DateTime.Now.ToLongTimeString(); //结束时间
header[5] = uIData.Device.Length > 3 ? uIData.Device.Substring(3) : "N/A"; //仪器型号
header[7] = uIData.IsBridgeOut ? "YES" : "NO"; //是否桥接输出
header[9] = year_week; //生产周期
header[11] = barcode; //条码
header[13] = uIData.LimitUp + "V"; //上限
header[15] = uIData.LimitDown + "V"; //下限
header[17] = uIData.SampleFreq + "点/秒"; //记录周期
iTextSharp.text.Font font2 = new iTextSharp.text.Font(bFont, 10.0f);
//添加3行表头
for (int j = 0; j < 3; j++) //列数确定后,多的单元格是添加不进去的
{
for (int i = 0; i < 6; i++) //列数确定后,多的单元格是添加不进去的
{
// 使用chuck 可有效的输出文字
// 也可使用 document.Add(new iTextSharp.text.Paragraph("Hello World"));
Chunk chuck = new iTextSharp.text.Chunk(header[i + j * 6]);
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

if (i < 5)
{

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 单元格占的列数,1列
cell.Colspan = 1;
// 单元格占的行数,1行
cell.Rowspan = 1;
// 边框
cell.BorderWidth = 0.2f;
// 边框颜色
cell.BorderColor = BaseColor.BLACK;
//背景色
cell.BackgroundColor = new BaseColor(252, 213, 180);
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
// 添加单元格
table.AddCell(cell);
}
else
{
cell = new PdfPCell(new Phrase(chuck));
// 单元格占的列数,2列 会自动添加另外1列
cell.Colspan = 2;
// 单元格占的行数,1行
cell.Rowspan = 1;
// 边框
cell.BorderWidth = 0.2f;
//背景色
cell.BackgroundColor = new BaseColor(252, 213, 180);
// 边框颜色
cell.BorderColor = BaseColor.BLACK;
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
// 添加单元格
table.AddCell(cell);

}




}
}
string[] header1 = new string[] { //注意是6列,最后2列合并
"序号","CH1(V)", "CH2(V)"," CH3(V)"," CH4(V)","测试时间","测试结果"
};
for (int i = 0; i < 7; i++) //列数确定后,多的单元格是添加不进去的
{
// 使用chuck 可有效的输出文字
// 也可使用 document.Add(new iTextSharp.text.Paragraph("Hello World"));
Chunk chuck = new iTextSharp.text.Chunk(header1[i]);
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 单元格占的列数,1列
cell.Colspan = 1;
// 单元格占的行数,1行
cell.Rowspan = 1;
// 边框
cell.BorderWidth = 0.2f;
// 边框颜色
cell.BorderColor = BaseColor.BLACK;
//背景色
cell.BackgroundColor = new BaseColor(146, 205, 220);
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
// 添加单元格
table.AddCell(cell);
}
DataVoltage dv;
for (Int32 i = 0; i < uIData.SampleNum; i++)
{
dv = dv_list[i];
//以下是内容

// 使用chuck 可有效的输出文字
// 也可使用 document.Add(new iTextSharp.text.Paragraph("Hello World"));
Chunk chuck = new iTextSharp.text.Chunk((i + 1).ToString());
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 单元格占的列数,1列
cell.Colspan = 1;
// 单元格占的行数,1行
cell.Rowspan = 1;
// 边框
cell.BorderWidth = 0.2f;
// 边框颜色
cell.BorderColor = BaseColor.BLACK;
//背景色
//cell.BackgroundColor = new BaseColor(146, 205, 220);
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
// 添加单元格
table.AddCell(cell);

chuck = new iTextSharp.text.Chunk(dv.V1.ToString());
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
chuck = new iTextSharp.text.Chunk(dv.V2.ToString());
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);


if (!uIData.IsBridgeOut)
{

chuck = new iTextSharp.text.Chunk(dv.V3.ToString());
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
chuck = new iTextSharp.text.Chunk(dv.V4.ToString());
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
}
else
{
chuck = new iTextSharp.text.Chunk("N/A");
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
chuck = new iTextSharp.text.Chunk("N/A");
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
}
chuck = new iTextSharp.text.Chunk(dv.Time);
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);
chuck = new iTextSharp.text.Chunk(dv.result);
// 文字字体
chuck.Font = font2;
//var paragraph = new iTextSharp.text.Paragraph(chuck);

// 对齐方式,剧中对齐
cell = new PdfPCell(new Phrase(chuck));
// 水平对齐方式,剧中
cell.HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER;
// 垂直对齐方式: 剧中
cell.VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE;
table.AddCell(cell);

}

////临时文件路径
//string imagePath = AppDomain.CurrentDomain.BaseDirectory + @"1.png";
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(imagePath);
//img.ScalePercent(50);
//// 图片位置
//img.SetAbsolutePosition((iTextSharp.text.PageSize.A4.Width - img.ScaledWidth) / 2, (iTextSharp.text.PageSize.A4.Height - img.ScaledHeight) / 2);
//writer.DirectContent.AddImage(img);
document.Add(table);
writer.PageEvent = new IsHandF();
writer.Flush();
document.Close();
}
// 参考网上的代码 添加页脚页眉和水印
public class IsHandF : PdfPageEventHelper, IPdfPageEvent
{
/// <summary>
/// 创建页面完成时发生
/// </summary>
public override void OnEndPage(PdfWriter writer, Document document)
{
base.OnEndPage(writer, document);

//页眉页脚使用字体
BaseFont bsFont = BaseFont.CreateFont(@"C:\Windows\Fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
iTextSharp.text.Font fontheader = new iTextSharp.text.Font(bsFont, 10, iTextSharp.text.Font.NORMAL);
iTextSharp.text.Font fontfooter = new iTextSharp.text.Font(bsFont, 10, iTextSharp.text.Font.NORMAL);

//获取文件流
PdfContentByte cbs = writer.DirectContent;
cbs.SetCharacterSpacing(1.0f); //设置文字显示时的字间距
//Phrase header = new Phrase("页眉", fontheader);
Phrase footer_r = new Phrase(writer.PageNumber.ToString() + " of " + 1, fontfooter); //writer.PageNumber.ToString()为页码。
Phrase footer_l = new Phrase(DateTime.Now.ToLongDateString(), fontfooter); //writer.PageNumber.ToString()为页码。
//页眉显示的位置 //页眉显示的位置
//ColumnText.ShowTextAligned(cbs, iTextSharp.text.Element.ALIGN_CENTER, header,
// document.Right / 2, document.Top, 0);
//页脚显示的位置
ColumnText.ShowTextAligned(cbs, iTextSharp.text.Element.ALIGN_CENTER, footer_l,
document.Left + 20, document.Bottom, 0);
ColumnText.ShowTextAligned(cbs, iTextSharp.text.Element.ALIGN_CENTER, footer_r,
document.Right - 20, document.Bottom, 0);

//添加背景色及水印,在内容下方添加
PdfContentByte cba = writer.DirectContentUnder;
////背景色
//Bitmap bmp = new Bitmap(1263, 893);
//Graphics g = Graphics.FromImage(bmp);
//Color c = Color.FromArgb(0x33ff33);
//SolidBrush b = new SolidBrush(c);//这里修改颜色
//g.FillRectangle(b, 0, 0, 1263, 893);
//System.Drawing.Image ig = bmp;
//iTextSharp.text.Image img = iTextSharp.text.Image.GetInstance(ig, new BaseColor(0xFF, 0xFF, 0xFF));
//img.SetAbsolutePosition(0, 0);
//cba.AddImage(img);

//水印
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"2.png");
image.RotationDegrees = 30;//旋转角度

PdfGState gs = new PdfGState();
gs.FillOpacity = 0.1f;//透明度
cba.SetGState(gs);

int x = -1000;
for (int j = 0; j < 15; j++)
{
x = x + 180;
int a = x;
int y = -170;
for (int i = 0; i < 10; i++)
{
a = a + 180;
y = y + 180;
image.SetAbsolutePosition(a, y);
cba.AddImage(image);
}
}
}
}