Pregunta

I have a very simple use-case for a filling up an acroform. I have a non-multi line text field. I would like to resize the font size to fit in the width of the text field.

The PDF spec mentions that a font size of 0 implies auto fit to width. However PDFBox - 1419 & PDFBOX-1402 mention that this isn’t supported in pdfbox.

Hence I have some small logic to calculate the font-sizes based on the widths etc. However I’m facing problems setting the font size.

I’m seeing the behavior mentioned in PDFBox - 1419.

Starts out with incorrect font size. If I click into the field, it displays correctly. Click outside the field, it reverts back to the wrong display.

Code :

pdfFormField.getDictionary.setString(COSName.DA, "/Helv 10 Tf 0 g”)
pdfFormField.setValue("Hello")

Any pointers or help would be much appreciated.

A simple example of such a PDF is here

¿Fue útil?

Solución

Pdfbox form field classes read the default appearance into a member variable early in their life-cycle and don't follow-up to changes in the form field dictionary they are based on. Thus, when creating the appearance stream during pdfFormField.setValue("Hello"), the former DA value is used.

After setting the default appearance, therefore, you have to instantiate the form field object anew. Then set the field value using this new object.

For sample code look at this answer to How to set the text of a PDTextbox to a color?; here the existing DA value of a text field is changed to contain a color setting operation before the field value is set.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top