前一篇 C# 使用 iTextSharp(4.1.2) 生成 PDF 文档 写了一个用 iTextSharp 4.1.2 生成 PDF 文档的例子,由于新版 iTextSharp 5.0.2 变化太大,所以有必要再记录一下。比较两篇你可以很明显的感觉到它们之间 API 上的差异来。
| 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 | using System.Data; using System.IO; using ExcelLibrary; using iTextSharp.text.pdf; using iTextSharp.text; namespace Unmi {     class PdfTest     {         public static void Main(string[] args)         {             //这里用的 ExcelLibrary 组件来从 Excel 文件生成一个 DataTable 对象             DataTable dt = DataSetHelper.CreateDataTable("D:\\test.xls", 0);             //为将要被创建的Pdf文档指定大小和颜色             Rectangle rec = new Rectangle(PageSize.A4.Rotate());//A4纸横向             //rec.BackgroundColor = new Color(System.Drawing.Color.Plum); //设置背景色             //创建Pdf文档             Document doc = new Document(rec);             //为Document创建多个PdfWriter对象             PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("c:\\test.pdf", FileMode.Create));             //要在事件中设置页眉页脚了             writer.PageEvent = new HeaderEvent();             //例如创建 HtmlWriter 还能生成一个相应的 html 文件             //iTextSharp.text.html.HtmlWriter.GetInstance(doc, new FileStream("c:\\test.html", FileMode.Create));             //设置文档的边距,依次是 left, right, top, bottom             doc.SetMargins(17.2f, 18.8f, 18.8f, 15f);             //可以在Open()方法调用前为doc添加摘要信息             doc.AddCreationDate();             doc.AddCreator("Unmi");             doc.AddAuthor("Unmi");             doc.AddTitle("iTextSharp 5 生成 PDF 文档示例");             doc.Open(); //打开文档             //向 PDF 文档添加一个 Table,其他的内容对象有 phrase、Paragraph、Graphic             PdfPTable pdfTable = new PdfPTable(dt.Columns.Count);             /*              * 要理解 Table 中的单元格怎么显示,先设定列数,然后逐个放 Cell,当前行的 Cell              * 数量到达列数时另起新行,可用单元格的 Rowspan,Colspan 设定跨行或跨列的数量             **/             pdfTable.SpacingBefore = 3;             pdfTable.SpacingAfter = 3;             pdfTable.WidthPercentage = 98f;             //可以使单元格内容跨页显示             pdfTable.SplitLate = false;             pdfTable.SplitRows = true;             //每一列的宽度比率,这里要求你的 Excel 第一张表有 6 列             pdfTable.SetWidths(new float[] { 0.8f, 1f, 0.6f, 0.6f, 0.9f, 7f });             //输出表头             foreach (DataColumn dc in dt.Columns)             {                 Font headerFont = new Font(Font.FontFamily.HELVETICA,10, Font.BOLD, BaseColor.WHITE);                 PdfPCell headerCell = new PdfPCell(new Paragraph(dc.ColumnName, headerFont));                 headerCell.HorizontalAlignment = Element.ALIGN_CENTER;                 headerCell.BackgroundColor = BaseColor.RED;                 headerCell.BorderColor = BaseColor.WHITE;                 pdfTable.AddCell(headerCell);             }             //pdfTable.EndHeaders();// 表头是否显示在每一页             //显示数据             foreach (DataRow dr in dt.Rows)             {                 foreach (DataColumn dc in dt.Columns)                 {                     PdfPCell dataCell = new PdfPCell(new Paragraph(dr[dc].ToString()));                     dataCell.Padding = 3;                     dataCell.SetLeading(dataCell.Leading, 1.5f);//设置行间距                     dataCell.BorderColor = BaseColor.GRAY;                     pdfTable.AddCell(dataCell);                 }             }             doc.Add(pdfTable);             doc.Close();         }     }     //用来在每一页加页眉的页面事件     class HeaderEvent : PdfPageEventHelper,IPdfPageEvent     {         private Phrase header;         public HeaderEvent()         {             header = new Phrase("http://unmi.blogjava.net");         }         public void OnEndPage(PdfWriter writer, Document document)         {             PdfContentByte cb = writer.DirectContent;             ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, header,                    (document.Right - document.Left) / 2 + document.LeftMargin, document.Top + 6, 0);         }     } } | 
这里有例子就不再多加了说明了,注释也写得比较详细。
参考: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
本文链接 https://yanbin.blog/csharp-itextsharp5-0-2-pdf/, 来自 隔叶黄莺 Yanbin Blog
[版权声明]  本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。
 本文采用 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 进行许可。