PDF 生成工具 – iTextSharp 入门


简介c#

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

目前可以产生PDF工具不少,同时例若有 Apose(商业), iTextSharp(商业+交流), Spire PDF(商业+免费), PDF library(免费). iTextSharp Java .Net都有建树,同时社区和资料比较丰富,等到普遍关注和应用。this

 

搭建环境spa

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

       咱们经过从 Nuget 中搜索 iTextSharp ,而且选择合适的版本添加到项目中。我是用的是 4.1.6 版本。图片

 

基本概念文档

       DocumentiTextSharp 的核心内容,也就是咱们所说的须要的文档,他主要是经过 open, Add, Close 方法来完成对文档的操做。get

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

       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使用仍是比较方便,固然还有比较复杂的构建,我这篇文章就再也不描述,若是有机会会在之后的篇幅中进行描述与讲解如何添加图片,如何添加附件,以及图片旋转等等一系列问题。

相关文章
相关标签/搜索