iText7导出pdf的使用

本次参考的文章:

https://www.cnblogs.com/qlqwjy/p/8213989.html

https://blog.csdn.net/darkjazz11/article/details/79647252

iText7版本的官方API文档:https://api.itextpdf.com/iText7/7.0.3/

一、使用Maven管理所需的jar包

    <properties>
        <itext.version>RELEASE</itext.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>kernel</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>io</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>layout</artifactId>
            <version>7.0.3</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>font-asian</artifactId>
            <version>7.0.3</version>
        </dependency>
    </dependencies>
</project>

二、编写函数导出pdf

//1、创建流对象
        PdfWriter pdfWriter=new PdfWriter(new File("pdf样品.pdf"));

        //2、创建文档对象
        PdfDocument pdfDocument=new PdfDocument(pdfWriter);

        //3、创建内容文档对象
        Document document=new Document(pdfDocument, PageSize.A4);

        PdfFont font= PdfFontFactory.createFont("STSongStd-Light","UniGB-UCS2-H",true);

        Table table=new Table(new float[]{1,3,1,1,1});
        table.setWidthPercent(100);
        //创建表头
        Cell head=new Cell(1,5); //一行四列
        Paragraph paragraph = new Paragraph("2018年第一季度键教科考核结果");
        paragraph.setTextAlignment(TextAlignment.CENTER).setFont(font).setBold();
        head.add(paragraph).setTextAlignment(TextAlignment.CENTER).setBorder(Border.NO_BORDER);
        //加入表格
        table.addHeaderCell(head);

        //表格第一行
        Cell cell1_1=new Cell();
        cell1_1.add(new Paragraph("督导项目").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell1_2=new Cell();
        cell1_2.add(new Paragraph("存在问题/奖惩/亮点").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell1_3=new Cell();
        cell1_3.add(new Paragraph("扣分").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell1_4=new Cell();
        cell1_4.add(new Paragraph("奖励(元)").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell1_5=new Cell();
        cell1_5.add(new Paragraph("处罚(元)").setTextAlignment(TextAlignment.CENTER).setFont(font));
        table.addCell(cell1_1);
        table.addCell(cell1_2);
        table.addCell(cell1_3);
        table.addCell(cell1_4);
        table.addCell(cell1_5);

        //表格第二行
        Cell cell2_1=new Cell();
        cell2_1.add(new Paragraph("安全生产管理").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell2_2=new Cell();
        cell2_2.add(new Paragraph("成立科室消防管理小组").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell2_3=new Cell();
        cell2_3.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell2_4=new Cell();
        cell2_4.add(new Paragraph("200").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell2_5=new Cell();
        cell2_5.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
        table.addCell(cell2_1);
        table.addCell(cell2_2);
        table.addCell(cell2_3);
        table.addCell(cell2_4);
        table.addCell(cell2_5);

        //第三行
        Cell cell3_1=new Cell(1,2);
        cell3_1.add(new Paragraph("合计(扣分、奖励、处罚)").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell3_2=new Cell();
        cell3_2.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell3_3=new Cell();
        cell3_3.add(new Paragraph("200").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell3_4=new Cell();
        cell3_4.add(new Paragraph("-").setTextAlignment(TextAlignment.CENTER).setFont(font));
        table.addCell(cell3_1);
        table.addCell(cell3_2);
        table.addCell(cell3_3);
        table.addCell(cell3_4);

        //第四行
        Cell cell4_1=new Cell(1,2);
        cell4_1.add(new Paragraph("合计(总分)").setTextAlignment(TextAlignment.CENTER).setFont(font));
        Cell cell4_2=new Cell(1,3);
        cell4_2.add(new Paragraph("100").setTextAlignment(TextAlignment.CENTER).setFont(font));
        table.addCell(cell4_1);
        table.addCell(cell4_2);

        document.add(table);
        document.close();

三、结果如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值