itextPdf生成pdf简单示例

文章环境

jdk1.8,springboot2.6.13

POM依赖

XML 复制代码
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>

示例代码

java 复制代码
package com.example.example;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author bao
 * @date 2024/3/25 16:10
 */
public class SimpleGenPdf {
        public static void main(String[] args) throws DocumentException,
                IOException {
        // 定义中文字体
        BaseFont bfChinese = BaseFont.createFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontCN = new Font(bfChinese, 12, Font.NORMAL);

        // 步骤1:创建一个大小为A4的文档
        Document document = new Document(PageSize.A4);
        try {
            // 步骤 2:
            // 我们为document创建一个监听,并把PDF流写到文件中
            //获取资源文件路径
            String resourcePath = "./src/main/resources/";
            PdfWriter.getInstance(document, new FileOutputStream(resourcePath + "/pdf/simplePdf.pdf"));
            // 步骤 3:打开文档
            document.open();

            // 段落
            Paragraph paragraph = new Paragraph("证明\r\r", new Font(bfChinese, 13, Font.NORMAL));
            //设置文字居中
            paragraph.setAlignment(Element.ALIGN_CENTER);
            //设置左缩进
            paragraph.setIndentationLeft(12);
            //设置右缩进
            paragraph.setIndentationRight(12);
            //设置首行缩进
            paragraph.setFirstLineIndent(24);
            //行间距
            paragraph.setLeading(15f);
            //设置段落上空白
            paragraph.setSpacingBefore(5f);
            //设置段落下空白
            paragraph.setSpacingAfter(5f);
            document.add(paragraph);

            //第一个表格
            document.add(new Paragraph("默认情况下的大小---居中 80%", fontCN));
            // 创建一个有3列的表格
            PdfPTable userTable = new PdfPTable(3);
            // 定义一个表格列头
            PdfPCell tableColumnHead = new PdfPCell(new Paragraph("header with colspan 3"));
            // 定义一个表格单元的跨度
            tableColumnHead.setColspan(3);
            // 把单元加到表格中
            userTable.addCell(tableColumnHead);
            //把下面这9项顺次的加入到表格中,当一行充满时候自动折行到下一行
            userTable.addCell("1.1");
            userTable.addCell("2.1");
            userTable.addCell("3.1");
            userTable.addCell("1.2");
            userTable.addCell("2.2");
            userTable.addCell("3.2");
            userTable.addCell("1.3");
            userTable.addCell("2.3");
            userTable.addCell("3.3");
            // 增加到文档中
            document.add(userTable);

            //第二个表格
            document.add(new Paragraph("居中 100%", fontCN));
            PdfPTable centerTable = userTable;
            // 设置表格大小为可用空白区域的100%
            centerTable.setWidthPercentage(100);
            // 增加到文档中2
            document.add(centerTable);

            //第三个表格
            document.add(new Paragraph("居右 50%", fontCN));
            PdfPTable rightTable = userTable;
            // 设置表格大小为可用空白区域的50%
            rightTable.setWidthPercentage(50);
            // 设置水平对齐方式为 居右
            rightTable.setHorizontalAlignment(Element.ALIGN_RIGHT);
            // 增加到文档中3
            document.add(rightTable);

            document.add(new Paragraph("居左 50%", fontCN));
            PdfPTable leftTable = userTable;
            // 设置水平对齐方式为 居左
            leftTable.setHorizontalAlignment(Element.ALIGN_LEFT);
            document.add(leftTable);
        } catch (Exception de) {
            de.printStackTrace();
        }
        // 步骤 5:关闭文档
        document.close();
    }
}

生成结果

git完整项目代码

bnmjstu / itextpdf-simple-example · GitCode

相关推荐
s:1031 小时前
【框架】参考 Spring Security 安全框架设计出,轻量化高可扩展的身份认证与授权架构
java·开发语言
南山十一少4 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724005 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo6 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库
李长渊哦6 小时前
常用的 JVM 参数:配置与优化指南
java·jvm
计算机小白一个6 小时前
蓝桥杯 Java B 组之设计 LRU 缓存
java·算法·蓝桥杯
南宫生9 小时前
力扣每日一题【算法学习day.132】
java·学习·算法·leetcode
计算机毕设定制辅导-无忧学长9 小时前
Maven 基础环境搭建与配置(一)
java·maven
风与沙的较量丶10 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_7482517210 小时前
SpringBoot3 升级介绍
java