Apache PdfBox how to set field font size

4.6k Views Asked by At

I am using org.apache.pdfbox to read pdf and outfill some fields.

Now I have to problem that the font size is much too big.

I thought it would be easy to set font size to 12 . But it is very complicated .

Actually it is awful. Does anyone know how to do it ? This is my code without styling.

 final PDDocument document = PDDocument.load(template);
    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);

    final PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
    final Iterator<PDField> it = acroForm.getFieldIterator();

    for (PDField f : acroForm.getFields()) {
        System.out.println(f.toString());

        if (f instanceof PDTextField) {
            f.set
            f.setValue("Some value");
        }
    };
1

There are 1 best solutions below

14
On

As discussed in the comments, the result of

((PDTextField) f).getDefaultAppearance()

is

/Helv 0 Tf 0 g

which means Helvetica, size 0 (= variable size), and color black (grey colorspace, 0 is black, 1 is white).

Thus to set a specific size, call

((PDTextField) f).setDefaultAppearance("/Helv 12 Tf 0 g")