OpenPDF icon indicating copy to clipboard operation
OpenPDF copied to clipboard

Changing fonts of form fields not working correctly

Open Lonzak opened this issue 4 years ago • 0 comments

Management summary:

When setting a font for a form field openPDF adds the DA to the widget (annotation) and not to the value (form field). This is not correct according to the spec.

Details: There seems to be problem with the current implementation when changing or adding fonts for form fields. Original PDF:

108 0 obj
<</T(names)/Kids[213 0 R 63 0 R 146 0 R]/V() /DA(/Arial 8 Tf 0 g)/FT/Tx>>
endobj
213 0 obj
<</P 57 0 R/Subtype/Widget/F 4/Type/Annot/MK<</BG[1.0 1.0 1.0]>>/Parent 108 0 R/Rect[48.6151 663.511 278.523 674.84]/AP<</N 21 0 R>>>>
endobj
[...]

So now I change the font from Arial to TimesNewRoman

BaseFont font = BaseFont.createFont(BaseFont.TIMES_ROMAN, encoding, BaseFont.EMBEDDED);
stamper.getAcroFields().setFieldProperty("names", "textfont", font), null);

and the result is like this:

127 0 obj
<</TU(AlternateFieldName)/T(names)/Kids[109 0 R 82 0 R 110 0 R]/V(Else Something)/DA(/Arial 8 Tf 0 g)/FT/Tx/Ff 29360130>>
endobj
109 0 obj
<</BS<</S/I/W 1>>/P 64 0 R/Subtype/Widget/F 4/Type/Annot/DA(/TiRo 11 Tf 1 1 1 rg )/MK<</BC[1 0 0]/R 0/BG[0.10196 0.2 0.30196]>>/Parent 127 0 R/Rect[45 135 150 155]/AP<</N 17 0 R>>>>
endobj

The result is not correct I would say:

  1. The DA of the parent stays the same
  2. The DA is added to the child thus the widget

But according to the PDF spec it belongs to the formfield (thus values in openPDF words). Adding the DA to the widget is the problem. When looking at the implementation the error seems to be (AcroFields.java lines 1071/1072)

 public boolean setFieldProperty(String field, String name, Object value, int inst[]) {
  ...
  item.getWidget(k).put(PdfName.DA, s);
  markUsed(item.getWidget(k));

When changing it to:

 item.getValue(k).put(PdfName.DA, s);
 markUsed(item.getValue(k));

solved it for me. Now the parent element contains the correct DA:

127 0 obj
<</T(names)/Kids[109 0 R 82 0 R 110 0 R]/V(Else Something)/DA(/TiRo 11 Tf 1 1 1 rg )/FT/Tx/Ff 29360130>>
endobj

Summary: When updating the font of a form field the DA key is added to the wrong dictionary. Any thoughts/views/rejections on this? If not I would create a merge request for this.

Lonzak avatar Jun 02 '21 16:06 Lonzak