Java 如果在 PDF 表单中多次出现,PDFBox 不会保持字段的字体外观

Java PDFBox does not maintain the font appearence of a field if it appears severraly in a PDF Form

我需要从我的 java 网络应用程序动态填写 pdf 表单,我发现 PDFBox 非常有用,除了当我有多个同名字段时遇到的问题或挑战。

我在一页 pdf 表单的不同位置有 5 个同名字段(可以说是 'wcode')。该字段具有不同的字体。通常,当您手动填写一个字段时,其他字段会自动选择相同的值,当我使用 PDFbox 填写它时会发生同样的情况,除了 PDFBox 将我所有的 5 个字段更改为与出现在 pdf 表单中的第一个字段具有相同的字体。

这是用于填写字段的代码。

PDDocument _pdfDocument = PDDocument.load(new File(originalPdf))
PDAcroForm acroForm = _pdfDocument.getDocumentCatalog().getAcroForm();    
PDTextField myCodeField = (PDTextField) acroForm.getField("wcode");
       if (myCodeField != null) {
          myCodeField .setValue(my.getCode());
       }
//Refresh layout && Flatten the document                
       acroForm.refreshAppearances();
       acroForm.flatten();
 _pdfDocument.save(outputFile);

我加了

acroForm.refreshAppearances();

经过一些研究,但没有任何改变。

因此,如果出现在 pdf 表单上的第一个 'wcode' 字段是 6pt,其余的所有其他 'wcode' 字段即使我在外观属性中将它们设置为 pdf 也将是 6pt 12pt.

我正在使用 PDFBox 2.0.5

这个问题在大约一个月前发布的PDFBox 2.0.6版本中已经得到解决。 Check comment on the jira 3837 here