0

I have an AcroForm with some fields, and I need to fetch font information for one of fields. I know that I must use

(PdfFormField)field.getDefaultAppearance()

but this results in string like

/Helv 12 Tf 0.25 0.25 0.25 rg` 

Is there a simple way to parse that string into PdfFont object?

It is obvoious that I can parse that by hand from given string, but I suspect that IText have some utils tha will do that for me isnt' it?

Besides /Helv is only "reference" or something like that, obviously pointinc to Helvetica but what if I would have some sort of custom fonts?

Thanks!

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • Concerning **Helv** etc: usually font names in the associated font resources dictionary are mapped to font definitions. The font names in **D**efault**A**ppearances sometimes are not, though, cf. the section *Using /Helv, /HeBo, ...* in [this answer](http://stackoverflow.com/a/30248927/1729265). – mkl May 11 '17 at 13:53

1 Answers1

1

If you want the font used in the formfield, you can use the PdfFormField#getFont() method, this will return the PdfFont object used direcly. To set a custom font, use PdfFormField#setFont(PdfFont font), this will also recreate the appearance of the form field. Use PdfFormField#setFontSize(float fontsize) to set the size of the text inside the field. Use PdfFormField#setClor(..) to set the text color.

The information you get from the getDefaultAppearance() is written in pdf syntax and can be parsed as follows:

`/Helv 12 Tf 0.25 0.25 0.25 rg`

The font size Tf instructions sets the font(subset) and size for the text instructions that follows. Fonts are referred using the name they are inserted into the pdf. In this case /Helv, which will probably point to (a subset) of Helvetica. It doesn't have to be helvetica however, some joker could've inserted Comic Sans and named it /Helv. 12 is the font size.

The x y z rg method sets the colour of the font using RGB values, with x, y and z relative intensity values in the interval [0.0,1.0].

Samuel Huylebroeck
  • 1,639
  • 11
  • 15