You need to enable JavaScript to run this app. iText生成带戳新版PDF后Aspose.PDF加载抛出空指针异常
优惠活动
大模型
产品
解决方案
定价
更多

iText生成带戳新版PDF后Aspose.PDF加载抛出空指针异常

问题触发场景

源文件为1.2版本的PDF文件,仅当通过开启新版本模式的PdfStamper(对应构造方法为new PdfStamper(reader, fos, '\0', true),最后一个参数用于控制生成PDF的版本号)加盖文本戳记时,生成的带戳PDF在执行com.aspose.pdf.Document(contentFilePath)初始化加载流程时会发生崩溃,抛出如下空指针异常:

Caused by: java.lang.NullPointerException
 at com.aspose.pdf.internal.l1if.l1v.<init>(Unknown Source)
 at com.aspose.pdf.internal.l6if.lf.lf(Unknown Source)
 at com.aspose.pdf.internal.l0h.l0p.lj(Unknown Source)
 at com.aspose.pdf.PageCollection.<init>(Unknown Source)
 at com.aspose.pdf.ADocument.l1h(Unknown Source)
 at com.aspose.pdf.ADocument.lI(Unknown Source)
 at com.aspose.pdf.ADocument.<init>(Unknown Source)
 at com.aspose.pdf.Document.<init>(Unknown Source)

调试相关信息

  • 排查中发现,页面集合对象中存在被抑制的异常,关联UnmodifiableRandomAccessList类型
  • 因PDF包含机密数据,无法提供可复现的样本文件
  • 当前使用的组件版本:Aspose 20.10、iText 5.0.5

问题复现代码

PdfStamper stamper = null;
FileOutputStream fos = null;
PdfReader reader = null;
String contentFilePathOut = "C:\\DISTR\\out.pdf";
try {
    FileInputStream fis = new FileInputStream("C:\\DISTR\\source.pdf");
    reader = new PdfReader(fis);
    fos = new FileOutputStream(contentFilePathOut, true);
    stamper = new PdfStamper(reader, fos, '\0', true);
    PdfContentByte canvas = stamper.getOverContent(1);
    ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT, new Phrase("Hello people!"), 250, 750, 0);
} finally {
    try {
        if (stamper != null) {
            stamper.close();
        }
    } catch (Exception ignore) {
    }

    try {
        if (fos != null) {
            fos.close();
        }
    } catch (Exception ignore) {

    }
    try {
        if (reader != null) {
            reader.close();
        }
    } catch (Exception ignore) {
    }
}
try {
    com.aspose.pdf.Document pdf = new com.aspose.pdf.Document(contentFilePathOut);
} catch (Exception e) {
    throw new ServiceException(e);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.14 16:24:03