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

如何用PDFBox 2.0.26在PDF第二页及多页写入文本?

PDFBox 2.0.26 多页PDF文本写入问题解决

问题场景

现有一个两页的PDF文件,已成功在第一页写入文本,但第二页写入的文本无法显示,需解决两个需求:

  1. 如何在第二页写入文本;
  2. 如何同时在第一页和第二页写入文本。

原使用代码:

PDDocument pdfdoc = PDDocument.load(getClass().getResourceAsStream("/templates/test.pdf"));
PDPageContentStream contentStream = new PDPageContentStream(pdfdoc, pdfdoc.getPage(0), PDPageContentStream.AppendMode.APPEND, true, true);
contentStream.beginText();
contentStream.newLineAtOffset(0, 0);
contentStream.setFont(PDType0Font.load(pdfdoc, new File(getClass().getResource("/fonts/...").getPath())), 12);
contentStream.newLineAtOffset(125, 600);
contentStream.showText(letter_number.getValue());
contentStream.endText();
contentStream.close();
pdfdoc.close();

解决方案

原代码仅操作了索引为0的第一页,PDFBox中页面索引从0开始,要操作第二页需针对索引为1的页面创建独立的PDPageContentStream。

1. 单独在第二页写入文本

针对第二页创建内容流,步骤与第一页一致:

try (PDDocument pdfdoc = PDDocument.load(getClass().getResourceAsStream("/templates/test.pdf"))) {
    // 预加载字体,避免重复加载浪费资源
    PDType0Font font = PDType0Font.load(pdfdoc, getClass().getResourceAsStream("/fonts/xxx.ttf"));
    
    // 获取第二页(索引为1)
    PDPage secondPage = pdfdoc.getPage(1);
    try (PDPageContentStream contentStream = new PDPageContentStream(pdfdoc, secondPage, PDPageContentStream.AppendMode.APPEND, true, true)) {
        contentStream.beginText();
        contentStream.setFont(font, 12);
        // 设置文本在页面的位置(原点为页面左下角,按需调整坐标)
        contentStream.newLineAtOffset(125, 600);
        contentStream.showText("第二页要写入的内容");
        contentStream.endText();
    }
    
    // 保存修改后的PDF
    pdfdoc.save("output.pdf");
} catch (IOException e) {
    e.printStackTrace();
}

2. 同时在第一页和第二页写入文本

分别为两页创建内容流,复用已加载的字体提升效率:

try (PDDocument pdfdoc = PDDocument.load(getClass().getResourceAsStream("/templates/test.pdf"))) {
    PDType0Font font = PDType0Font.load(pdfdoc, getClass().getResourceAsStream("/fonts/xxx.ttf"));
    
    // 操作第一页
    PDPage firstPage = pdfdoc.getPage(0);
    try (PDPageContentStream firstStream = new PDPageContentStream(pdfdoc, firstPage, PDPageContentStream.AppendMode.APPEND, true, true)) {
        firstStream.beginText();
        firstStream.setFont(font, 12);
        firstStream.newLineAtOffset(125, 600);
        firstStream.showText(letter_number.getValue());
        firstStream.endText();
    }
    
    // 操作第二页
    PDPage secondPage = pdfdoc.getPage(1);
    try (PDPageContentStream secondStream = new PDPageContentStream(pdfdoc, secondPage, PDPageContentStream.AppendMode.APPEND, true, true)) {
        secondStream.beginText();
        secondStream.setFont(font, 12);
        secondStream.newLineAtOffset(125, 600);
        secondStream.showText("第二页的文本内容");
        secondStream.endText();
    }
    
    pdfdoc.save("output.pdf");
} catch (IOException e) {
    e.printStackTrace();
}

注意事项

  • 使用try-with-resources语法自动关闭PDDocument和PDPageContentStream,避免资源泄漏;
  • AppendMode.APPEND表示在原页面内容后追加文本,不会覆盖原有内容;
  • 字体加载优先用getResourceAsStream获取输入流,避免文件路径解析错误;
  • 文本坐标newLineAtOffset(x, y)的原点在页面左下角,需根据实际布局调整位置。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 07:18:20