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

PDFbox生成带表单域PDF在移动端默认阅读器无法打印问题

问题:PDFBox生成带表单域的PDF在移动端默认阅读器打印时丢失表单域

我使用PDFBox生成包含表单域的PDF文件,遇到以下问题:

  • 在iOS和Android默认PDF阅读器的打印预览中,表单域丢失且无法正常打印
  • 安装Adobe Acrobat后,PDF可正常显示及打印
  • 在MacBook的Chrome浏览器中,PDF可正常显示和打印

最初怀疑是字体不兼容问题,于是通过pDAcroForm.setNeedAppearances(true);移除Helvetica字体,改用嵌入字体,解决了未嵌入字体的问题,但表单域丢失的问题仍未解决。

相关代码

模板加载与初始化代码

public byte[] createPdf(long customer, UUID id, int amount) throws xxxxxxx {
    ByteArrayOutputStream outputStream;
    Resource resource = resourceLoader.getResource("classpath:xxxxxx.pdf");
    try (PDDocument pdDocument = PDDocument.load(resource.getFile());) {
        PDAcroForm pDAcroForm = pdDocument.getDocumentCatalog().getAcroForm();
        pDAcroForm.setNeedAppearances(true);

表单域编辑代码

PDTextField fieldCustomer = (PDTextField) pDAcroForm.getField("customer");
fieldCustomer.setValue(String.valueOf(customerId));
fieldCustomer.setReadOnly(true);
....
outputStream = new ByteArrayOutputStream();
pdDocument.save(outputStream);

解决方案/规避方法

经测试,移动端基础PDF阅读器不支持可填写表单域,用户需使用Adobe等专业PDF应用才能正常识别表单域。为兼容所有阅读器,我将需填充的表单域替换为普通文本行,代码示例如下:

PDPage firstPage=pdDocument.getPage(0);
PDFont pdfFont= PDType1Font.HELVETICA_BOLD;
int fontSize = 14;

PDPageContentStream contentStream = new PDPageContentStream(pdDocument, firstPage, PDPageContentStream.AppendMode.APPEND,true,true);
contentStream.setFont(pdfFont, fontSize);
contentStream.beginText();
contentStream.newLineAtOffset(210,500);
contentStream.showText(xxxxx);
contentStream.newLineAtOffset(0,-28);
contentStream.showText(xxxxx);
contentStream.newLineAtOffset(0,-28);
contentStream.showText(fxxxxxx);
contentStream.endText();
contentStream.close();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 18:12:39