使用Itext编辑PDF(可处理后保存本地,也可只处理文件流后返回文件流实现文件不落地)

本文介绍如何使用Itext库处理PDF文件流,既可直接返回处理后的文件流,也可选择保存到本地。示例代码展示从文件流处理到返回文件流的过程,并提示在Spring Boot中使用时的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用Itext编辑PDF(使用文件流及临时文件实现最终文件不落地)

话不多说上代码:

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.*;
import java.util.List;


public class PdfSignService {
   
   

    // Get base text
    public static BaseFont getBaseFont() throws DocumentException, IOException {
   
   
        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
        return base;
    }

    /**
     *
     * @param bytes Unprocessed file stream
     * @param list Details to add
     * @return
     * @throws IOException
     */
    public static byte[] signSinglePsw(byte[] bytes, List<PdfItextModel> list) throws IOException {
   
   
        System.out.println("PDF change start");
        //Create a temporary file to modify and eventually export the file stream
        File file = File.createTempFile("chang","pdf");
        try 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值