You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PdfStamper向PDF添加文本附件失败,应如何排查解决?

PDF嵌入字符串附件代码问题修复方案

你的代码存在以下3个核心问题:

  • 你创建的Document对象从未调用document.open()和document.close(),最终生成的/storage/emulated/0/Download/Aa.pdf是无效空文件,PdfReader读取无效PDF时会直接抛出异常
  • 你同时用同一个文件路径作为PdfReader的读取源和PdfStamper的输出目标,文件读写冲突会直接导致PDF内容被损坏,无法正常生成最终文件
  • String.format用法错误:String.format("data.txt",data)没有占位符,无法把你的字符串参数代入,且fileEmbedded方法的参数顺序你用错了,第三个参数是嵌入后显示的文件名,第四个参数才是文件的二进制内容,你当前的传参逻辑完全不符合方法要求

修复代码

场景1:给已存在的有效PDF文件添加附件

import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class AddAttachmentToPdf {
    public static void main(String[] args) throws Exception {
        String directoryPath = "/storage/emulated/0/Download";
        // 要嵌入的字符串内容
        String data = PaintView.full_string_return();
        // 注意:srcPdf是已经存在的有效PDF路径,outputPdf是输出的带附件的新PDF路径,不能和源路径相同
        String srcPdf = directoryPath + "/source.pdf";
        String outputPdf = directoryPath + "/output_with_attachment.pdf";
        
        PdfReader reader = new PdfReader(srcPdf);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputPdf));
        // 构造嵌入文件:第三个参数是附件显示的文件名,第四个参数是文件内容的二进制数组
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                stamper.getWriter(),
                null,
                "data.txt",
                data.getBytes(StandardCharsets.UTF_8)
        );
        stamper.addFileAttachment("自定义附件描述", fs);
        stamper.close();
        reader.close();
    }
}

场景2:新建空白PDF同时添加附件

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfFileSpecification;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class CreatePdfWithAttachment {
    public static void main(String[] args) throws Exception {
        String directoryPath = "/storage/emulated/0/Download";
        String outputPdf = directoryPath + "/new_with_attachment.pdf";
        String data = PaintView.full_string_return();
        
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(outputPdf));
        document.open();
        // 这里可以添加PDF正文内容,不需要的话留空也可以生成有效PDF
        // 嵌入附件
        PdfFileSpecification fs = PdfFileSpecification.fileEmbedded(
                writer,
                null,
                "data.txt",
                data.getBytes(StandardCharsets.UTF_8)
        );
        writer.addFileAttachment("自定义附件描述", fs);
        document.close();
        writer.close();
    }
}

额外注意事项

  • 安卓环境下需要提前申请读写存储权限,否则会抛出文件访问权限异常
  • 上述代码适配iText 5.x版本,如果你使用的是iText 7版本,API语法会有差异

内容的提问来源于stack exchange,提问作者Pedro Carvalho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 14:27:00