使用itext-core生成PDF

1、添加引用依赖包

XML 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-core</artifactId>
            <version>8.0.3</version>
            <type>pom</type>
        </dependency>

2、上代码

java 复制代码
package com.student.demo.pdf;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;
import java.io.IOException;

/**
 * @Author: ylj
 * @Date: 2024/3/20
 */
public class PdfCreator1 {
    //windows 系统字体路径
    private static final String FONT_PATH = "C:\\Windows\\Fonts\\simsun.ttc,0";

    public static void main(String[] args) throws IOException {
        //文件生成地址
        String pdfFilePath = "D:\\pdf\\accRecFinVou1.pdf";
        File file = new File(pdfFilePath);
        //写pdf文件
        PdfWriter writer = new PdfWriter(file);
        //pdf文档对象
        PdfDocument pdf = new PdfDocument(writer);
        //文档内容
        Document document = new Document(pdf, PageSize.A4);

        // 加载宋体字体
        //PdfFont songFont = PdfFontFactory.createFont("path/to/simsong.ttc,1", PdfEncodings.IDENTITY_H, true);
        //PdfFont songFont = PdfFontFactory.createFont("simsong.ttc,1");

        //HeiseiMin-W3 HeiseiKakuGo-W5 KozMinPro-Regular STSong-Light STSongStd-Light MHei-Medium MSung-Light MSungStd-Light HYGoThic-Medium HYSMyeongJo-Medium HYSMyeongJoStd-Medium
        //PdfFont songFont = PdfFontFactory.createFont("STSong-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
        
        // 文件头
        Paragraph title = new Paragraph("应收账款融资凭证")
                .setFont(songFont)
                .setFontSize(18)
                .setTextAlignment(TextAlignment.CENTER)
                //.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 100)
                .setBold();
        document.add(title);

        // 融资日期
        Paragraph date = new Paragraph("融资日期:       年   月   日")
                .setFont(songFont)
                .setFontSize(10.5f) // 五号字体,近似值
                .setTextAlignment(TextAlignment.CENTER);
                //.setFixedPosition(pdf.getFirstPage().getPageSize().getWidth() / 2, pdf.getFirstPage().getPageSize().getHeight() - 130);
        document.add(date);

        // 创建表格 11列
        float[] columnWidths = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; // 定义每列的宽度比例
        Table table = new Table(columnWidths);
        table.setWidth(530);
        table.setHorizontalAlignment(HorizontalAlignment.CENTER);

        // 第1行new Cell(所占行数, 所占列数)
        table.addCell(new Cell(1, 1).add(new Paragraph("第1行").setHeight(40).setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("XXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("保理商名称").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 8).add(new Paragraph("XXXXXXXXXXXXXXXXXXXX公司").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        // 第2行
        table.addCell(new Cell(2, 1).add(new Paragraph("第2行").setHeight(40)
                .setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(2, 2).add(new Paragraph("    仟   佰   拾   万   仟   佰   拾   元整").setFont(songFont).setFontSize(10.5f))
                .setHorizontalAlignment(HorizontalAlignment.CENTER).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("万").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("千").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("百").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("十").setFont(songFont).setFontSize(10.5f)));
        table.addCell(new Cell(1, 1).add(new Paragraph("元").setFont(songFont).setFontSize(10.5f)));
        //第3行
        for (int i = 1; i<=8; i++){
            table.addCell(new Cell(1, 1));
        }
        // 第4行
        table.addCell(new Cell(1, 1).add(new Paragraph("第4行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));
        table.addCell(new Cell(3, 1).add(new Paragraph("表格").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(3, 8).setFont(songFont));

        // 第5行
        table.addCell(new Cell(1, 1).add(new Paragraph("第5行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第6行
        table.addCell(new Cell(1, 1).add(new Paragraph("第6行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第7行
        table.addCell(new Cell(1, 1).add(new Paragraph("第7行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));
        table.addCell(new Cell(3, 1).add(new Paragraph("编号").setFont(songFont)).setVerticalAlignment(VerticalAlignment.MIDDLE));
        table.addCell(new Cell(3, 8).setFont(songFont));

        // 第8行
        table.addCell(new Cell(1, 1).add(new Paragraph("第8行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第9行
        table.addCell(new Cell(1, 1).add(new Paragraph("第9行").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("       /年").setTextAlignment(TextAlignment.CENTER).setFont(songFont)));

        // 第10行
        table.addCell(new Cell(1, 1).add(new Paragraph("第10行").setHeight(80).setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 1).setTextAlignment(TextAlignment.CENTER));
        table.addCell(new Cell(1, 1).add(new Paragraph("签章").setFont(songFont).setFontSize(10.5f)).setHorizontalAlignment(HorizontalAlignment.CENTER));
        table.addCell(new Cell(1, 8).setTextAlignment(TextAlignment.CENTER));

         //添加表格到文档
        document.add(table);

        // 关闭文档
        document.close();

        System.out.println("PDF生成结束======>");
    }
}

3、效果图

最后注意几个点:

1、字体问题:itext不支持中文,在显示过程中会碰到中文不显示问题,上面给的代码是支持中文宋体的。

复制代码
//使用的是系统自带支持的中文宋体
PdfFont songFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", PdfFontFactory.EmbeddingStrategy.PREFER_NOT_EMBEDDED);
复制代码
//使用系统路径下的字体(windows和linux系统对应目录下都有,自行查询目录位置)
PdfFont songFont = PdfFontFactory.createFont(FONT_PATH);

2、版本问题,不同版本之间存在有些方法调用的名称和参数发生了变化,这个需要注意。

相关推荐
昔人'19 分钟前
`list-style-type: decimal-leading-zero;`在有序列表`<ol></ol>` 中将零添加到一位数前面
前端·javascript·html
缺点内向3 小时前
Java:创建、读取或更新 Excel 文档
java·excel
带刺的坐椅3 小时前
Solon v3.4.7, v3.5.6, v3.6.1 发布(国产优秀应用开发框架)
java·spring·solon
四谎真好看5 小时前
Java 黑马程序员学习笔记(进阶篇18)
java·笔记·学习·学习笔记
桦说编程5 小时前
深入解析CompletableFuture源码实现(2)———双源输入
java·后端·源码
java_t_t5 小时前
ZIP工具类
java·zip
lang201509285 小时前
Spring Boot优雅关闭全解析
java·spring boot·后端
岁月宁静6 小时前
深度定制:在 Vue 3.5 应用中集成流式 AI 写作助手的实践
前端·vue.js·人工智能
pengzhuofan6 小时前
第10章 Maven
java·maven
心易行者6 小时前
10天!前端用coze,后端用Trae IDE+Claude Code从0开始构建到平台上线
前端