C# 使用 iTextSharp(5.0.2) 生成 PDF 文档
前一篇 C# 使用 iTextSharp(4.1.2) 生成 PDF 文档 写了一个用 iTextSharp 4.1.2 生成 PDF 文档的例子,由于新版 iTextSharp 5.0.2 变化太大,所以有必要再记录一下。比较两篇你可以很明显的感觉到它们之间 API 上的差异来。
1using System.Data;
2using System.IO;
3using ExcelLibrary;
4using iTextSharp.text.pdf;
5using iTextSharp.text;
6
7namespace Unmi
8{
9 class PdfTest
10 {
11 public static void Main(string[] args)
12 {
13
14 //这里用的 ExcelLibrary 组件来从 Excel 文件生成一个 DataTable 对象
15 DataTable dt = DataSetHelper.CreateDataTable("D:\\test.xls", 0);
16
17 //为将要被创建的Pdf文档指定大小和颜色
18 Rectangle rec = new Rectangle(PageSize.A4.Rotate());//A4纸横向
19
20 //rec.BackgroundColor = new Color(System.Drawing.Color.Plum); //设置背景色
21
22 //创建Pdf文档
23 Document doc = new Document(rec);
24
25 //为Document创建多个PdfWriter对象
26 PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("c:\\test.pdf", FileMode.Create));
27
28 //要在事件中设置页眉页脚了
29 writer.PageEvent = new HeaderEvent();
30
31 //例如创建 HtmlWriter 还能生成一个相应的 html 文件
32 //iTextSharp.text.html.HtmlWriter.GetInstance(doc, new FileStream("c:\\test.html", FileMode.Create));
33
34 //设置文档的边距,依次是 left, right, top, bottom
35 doc.SetMargins(17.2f, 18.8f, 18.8f, 15f);
36
37 //可以在Open()方法调用前为doc添加摘要信息
38 doc.AddCreationDate();
39 doc.AddCreator("Unmi");
40 doc.AddAuthor("Unmi");
41 doc.AddTitle("iTextSharp 5 生成 PDF 文档示例");
42
43 doc.Open(); //打开文档
44
45 //向 PDF 文档添加一个 Table,其他的内容对象有 phrase、Paragraph、Graphic
46 PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);
47
48 /*
49 * 要理解 Table 中的单元格怎么显示,先设定列数,然后逐个放 Cell,当前行的 Cell
50 * 数量到达列数时另起新行,可用单元格的 Rowspan,Colspan 设定跨行或跨列的数量
51 **/
52
53 pdfTable.SpacingBefore = 3;
54 pdfTable.SpacingAfter = 3;
55 pdfTable.WidthPercentage = 98f;
56
57 //可以使单元格内容跨页显示
58 pdfTable.SplitLate = false;
59 pdfTable.SplitRows = true;
60
61 //每一列的宽度比率,这里要求你的 Excel 第一张表有 6 列
62 pdfTable.SetWidths(new float[] { 0.8f, 1f, 0.6f, 0.6f, 0.9f, 7f });
63
64 //输出表头
65 foreach (DataColumn dc in dt.Columns)
66 {
67 Font headerFont = new Font(Font.FontFamily.HELVETICA,10, Font.BOLD, BaseColor.WHITE);
68 PdfPCell headerCell = new PdfPCell(new Paragraph(dc.ColumnName, headerFont));
69 headerCell.HorizontalAlignment = Element.ALIGN_CENTER;
70 headerCell.BackgroundColor = BaseColor.RED;
71 headerCell.BorderColor = BaseColor.WHITE;
72 pdfTable.AddCell(headerCell);
73 }
74
75 //pdfTable.EndHeaders();// 表头是否显示在每一页
76
77 //显示数据
78 foreach (DataRow dr in dt.Rows)
79 {
80 foreach (DataColumn dc in dt.Columns)
81 {
82 PdfPCell dataCell = new PdfPCell(new Paragraph(dr[dc].ToString()));
83
84 dataCell.Padding = 3;
85 dataCell.SetLeading(dataCell.Leading, 1.5f);//设置行间距
86 dataCell.BorderColor = BaseColor.GRAY;
87
88 pdfTable.AddCell(dataCell);
89 }
90 }
91
92 doc.Add(pdfTable);
93 doc.Close();
94 }
95 }
96
97 //用来在每一页加页眉的页面事件
98 class HeaderEvent : PdfPageEventHelper,IPdfPageEvent
99 {
100 private Phrase header;
101 public HeaderEvent()
102 {
103 header = new Phrase("http://unmi.blogjava.net");
104 }
105 public void OnEndPage(PdfWriter writer, Document document)
106 {
107 PdfContentByte cb = writer.DirectContent;
108 ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, header,
109 (document.Right - document.Left) / 2 + document.LeftMargin, document.Top + 6, 0);
110 }
111 }
112}这里有例子就不再多加了说明了,注释也写得比较详细。
参考:1. iText 简介
2. 使用iTextSharp创建pdf文件
3. iText 5.0.2 发布,PDF操作Java类库
4. 第18章. iText PDF 生成
- Create PDFs in ASP.NET - getting started with iTextSharp
- Working with Fonts
- Adding Text with Chunks, Phrases and Paragraphs
- Lists with iTextSharp
- Links and Bookmarks
- Introducing Tables
- Working with images
- Drawing shapes and Graphics
- Page Layout with Columns
[版权声明]
本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。