PDF 生成工具 – iTextSharp 入门

xiexiantao 8年前

来自: http://my.oschina.net/RainyZou/blog/631647



简介

随着各个行业的快速融合进互联网,大家都希望把提高效率寄托于软件行业,那么PDF这种统一化标准的文档,也必然在企业中占据一定的份额。

目前能够产生PDF工具很多,同时例如有 Apose(商业), iTextSharp(商业+交流), Spire PDF(商业+免费), PDF library(免费). iTextSharp Java .Net都有建树,同时社区和资料比较丰富,等到广泛关注和应用。

 

搭建环境

       iTextSharp 目前最新版本是 5.5.8 这个遵循 LGPL, iTextSharp 4.1.6 以前都是遵循 LGPL + MPL,这个对于常用混迹于商业软件的你来说,应该是值得注意的。

       我们通过从 Nuget 中搜索 iTextSharp ,并且选择合适的版本添加到项目中。我是用的是 4.1.6 版本。

 

基本概念

       DocumentiTextSharp 的核心内容,也就是我们所说的需要的文档,他主要是通过 open, Add, Close 方法来完成对文档的操作。

       PdfPTable, PdfPCell 用来产生各式各样的表格。

       HeaderFooter 是文档的渲染,用来生成页面的头部和尾部的。

 

简单示例

       理解了上面的几个概念,我们来通过几个简短的实例来了解 iTextSharp 的一些使用情况。

       生成一个简单PDF文档,让我们了解一下iTextSharp 是如何创建一个文档,同时如何保存到文件中的,理解一下生成文档的几个常用步骤。

            var doc1 = new Document(); //1              FileStream stream = new FileStream(fileName, FileMode.Create);              PdfWriter.GetInstance(doc1, stream); //2                doc1.Open(); //3              doc1.Add(new Paragraph("My First PDF"));//4              doc1.Close();//5

     实例图如下:

    

 

       生成一个带有Header Footer PDF文档,要理解Header Footer添加的时机,它应该在PDF打开之前进行设置,从另外的角度去看,就像是对文档的一种渲染。

            var doc = new Document();              PdfWriter.GetInstance(doc, new FileStream(fileName, FileMode.Create));                HeaderFooter header = new HeaderFooter(new Phrase("this is the page"), false);              header.Alignment = HeaderFooter.ALIGN_CENTER;              doc.Header = header;                doc.Footer = new HeaderFooter(new Phrase("C&C Reservoirs"), false);              doc.Open();              doc.Add(new Paragraph("What was the day today?"));                doc.Close();

 

       下面示例是生成一个包含 table 的文档格式,也是使用比较多的一种形式。主要要如何设置表头,看看在表头设置的时候分页的处理。同时处理表格的格式等等。

            PdfPTable table = new PdfPTable(4);              table.TotalWidth = 400f;              table.LockedWidth = true;              table.HeaderRows = 1;                PdfPCell header = new PdfPCell(new Phrase("Header"));              header.BorderWidth = 0;              header.HorizontalAlignment = PdfCell.ALIGN_CENTER;              header.Colspan = 4;                             table.AddCell(header);                for (int i = 0; i < 100; i++)              {                  table.AddCell("Cell 1");                  table.AddCell("Cell 2");                  table.AddCell("Cell 3");                  table.AddCell("Cell 4");              }                PdfPTable nested = new PdfPTable(1);              nested.AddCell("Nested Row 1");              nested.AddCell("Nested Row 2");              nested.AddCell("Nested Row 3");                PdfPCell nesthousing = new PdfPCell(nested);              nesthousing.Padding = 0f;                table.AddCell(nesthousing);                PdfPCell bottom = new PdfPCell(new Phrase("bottom"));              bottom.Colspan = 3;              bottom.HorizontalAlignment = PdfCell.ALIGN_CENTER;              bottom.VerticalAlignment = PdfCell.ALIGN_CENTER;                table.AddCell(bottom);

总结

    对于iTextSharp使用还是比较方便,当然还有比较复杂的构建,我这篇文章就不再描述,如果有机会会在以后的篇幅中进行描述与讲解如何添加图片,如何添加附件,以及图片旋转等等一系列问题。