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

Apache PDFBox与Acrobat Reader多行文本域填充差异原因咨询

手动输入与代码填充PDF文本域的垂直位置差异问题

手动输入数据时,显示效果如下:
手动输入数据的显示效果

使用代码填充文本域时,显示效果如下:
代码填充文本域的显示效果

为何二者的垂直位置存在差异?感谢您提供的任何帮助。


相关说明

  • 模板为IRS的f1065sk1表单
  • 运行代码时出现如下警告:
    WARNING: Using fallback font Helvetica for HelveticaLTStd-Roman
    
  • 该表单由Adobe LiveCycle Designer创建,无法使用Acrobat Pro编辑

复现问题的最简代码

package util.pdf.box;

import util.file.BytesToFile;
import util.file.FileToBytes;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

import java.io.ByteArrayOutputStream;

public class PdfFormFiller {

    public byte[] fill(
        byte[] template,
        String key,
        String value
    ) {

        byte[] pdfBytes = new byte[]{};
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try (
            PDDocument doc = PDDocument.load(template)
        ) {
            PDDocumentCatalog pdDocCatalog = doc.getDocumentCatalog( );
            PDAcroForm acroForm = pdDocCatalog.getAcroForm();
            PDField field = acroForm.getField(key);
            field.setValue(value);
            doc.save(baos);
        } catch ( Exception e ) {
            System.out.println( e.getMessage( ) );
        }
        pdfBytes = baos.toByteArray( );
        return pdfBytes;

    }

    public static void main(String[] args) {

        String filePath = "/Users/johndoe/f1065sk1.pdf";
        byte[] pdfBytes = FileToBytes.readFile( filePath );
        PdfFormFiller pdfFormFiller = new PdfFormFiller( );
        byte[] pdfFilled = pdfFormFiller.fill(
            pdfBytes,
            "topmostSubform[0].Page1[0].LeftCol[0].f1_7[0]",
            "Name\nAddress\nCity, State Zip"
        );
        String outDirName = "/Users/johndoe";
        String outFileName = "f1065sk1.filled.pdf";
        BytesToFile.writeToDirFile( pdfFilled, outDirName, outFileName );

    }

}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 06:12:21