• iTestSharp的简单应用


    前言

        最近公司某项目要针对一些信息基础表绘画成表格的形式然后生成pdf文件,在网上寻找到iTestSharp发现此类库很强大,虽然园子里已经有很多大牛写了关于此插件的使用方法,但是自己也想写一写,把自己在项目中用到的记录下来

    1、准备工作:

    首先下载http://sourceforge.net/projects/itextsharp/?source=typ_redirect  dll文件,解压完之后用到的为itextsharp.dll。

    2、环境搭建:

    为了方便,在此就新建控制台应用程序,然后引入itextsharp.dll文件。

    3、itextsharp类库使用介绍:

     Document 文档,操作内存中的pdf文件
     PdfPCell 单元格
     PdfPTable 表格

      

    生成表格

    接下来演示如何生成以下表格:

     

     首先实例化Document对象,默认文档大小为A4,我们还可以使用Rectangle类来设置文档的大小。

     //实例化
     Document document = new Document(iTextSharp.text.PageSize.A4);
     //设置文档大小
     Rectangle rect = new Rectangle(600, 450);
     document.SetPageSize(rect);

    如果将内存中的Document对象保存到本地硬盘中,应使用PdfWriter类

     PdfWriter.GetInstance(document, new System.IO.FileStream("F:\test.pdf", System.IO.FileMode.Create));

     绘画表格:

            PdfPTable table = new PdfPTable(4);
    
                PdfPCell header = new PdfPCell(new Phrase("用户信息"));
                header.Colspan = 3;
                header.HorizontalAlignment = 1;
                table.AddCell(header);
    
                table.AddCell("姓名");
                table.AddCell("年龄");
                table.AddCell("性别");
                table.AddCell("生日");
    
                table.AddCell("李雷");
                table.AddCell("23");
                table.AddCell("");
                table.AddCell("1980-01-01");
    
                table.AddCell("韩梅梅");
                table.AddCell("22");
                table.AddCell("");
                table.AddCell("1982-04-03");
    
                table.AddCell("隔壁老王");
                table.AddCell("25");
                table.AddCell("");
                table.AddCell("1977-03-12");
    
                document.Open();
                document.Add(table);
                document.Close();

    在Main中Person.GetTable();调用后生成出来效果:

    纳尼?李雷,韩梅梅,隔壁老王去哪了?有木有发现所有的汉字都无法显示。

    再看一下Phrase这个类:

    它是可以设置字体的,我们来设置一个中文字体:

     //设置中文字体
     BaseFont bf = BaseFont.CreateFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
     Font font = new Font(bf);

    修改后的代码:

           PdfPTable table = new PdfPTable(4);
                PdfPCell header = new PdfPCell(new Phrase("用户信息", font));
                header.Colspan = 4;
                header.HorizontalAlignment = 1;
                table.AddCell(header);
    
                table.AddCell(new Phrase("姓名", font));
                table.AddCell(new Phrase("年龄", font));
                table.AddCell(new Phrase("性别", font));
                table.AddCell(new Phrase("生日", font));
    
                table.AddCell(new Phrase("李雷", font));
                table.AddCell("23");
                table.AddCell(new Phrase("", font));
                table.AddCell("1980-01-01");
    
                table.AddCell(new Phrase("韩梅梅", font));
                table.AddCell("22");
                table.AddCell(new Phrase("", font));
                table.AddCell("1982-04-03");
    
                table.AddCell(new Phrase("隔壁老王", font));
                table.AddCell("25");
                table.AddCell(new Phrase("", font));
                table.AddCell("1977-03-12");
    
                document.Open();
                document.Add(table);
                document.Close();

    生成的表格:

    pdf文档合并

        public static void Mergre()
            {
                string[] pdflist = new string[2];
                pdflist[0] = "F:\test1.pdf";
                pdflist[1] = "F:\test2.pdf";
                PdfReader reader;
                Document document = new Document();
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("F:\New.pdf", FileMode.Create));
                document.Open();
                PdfContentByte contentByte = writer.DirectContent;
                PdfImportedPage importedPage;
                for (int i = 0; i < pdflist.Length; i++)
                {
                    reader = new PdfReader(pdflist[i]);
                    int iPageNum = reader.NumberOfPages;
                    for (int j = 1; j <= iPageNum; j++)
                    {
                        //这个必须要创建新页,否则下面的内容会与第一页重合
                        document.NewPage();
                        //读取旧的pdf,然后添加到新的pdf 中
                        importedPage = writer.GetImportedPage(reader, j);
                        //添加到新的pdf中可设置位置
                        contentByte.AddTemplate(importedPage, 0, 0);
                    }
    
                }
                document.Close();
            }

           上面只是简单的应用,在实际项目中表格的样式,大小等问题还需一点一点去实现,API中有很多方法自己看看便可熟练运用,在学习过程中我也看了博友们的文章,感谢大家的分享,如果应用iTestSharp实现更复杂的功能建议去看这位博友的文章:

    http://www.cnblogs.com/CareySon/category/332146.html

  • 相关阅读:
    Log4Net 配置详解
    .Net Core 获取应用物理路径的常见问题
    Js/Jquery获取iframe中的元素
    Ztree树使用详解
    【解决】nginx + socket.io ,能连接但不响应事件
    基础文档官方链接
    位运算
    Java基础之集合框架--Collections.reverse()方法
    Android动画攻略—帧动画、补间动画、属性动画
    [转]京东mPaaS移动日志建设与应用
  • 原文地址:https://www.cnblogs.com/crayoncode/p/5120376.html
Copyright © 2020-2023  润新知