0

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");
        }
    };
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
Marcela28
  • 17
  • 1
  • 5

1 Answers1

1

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")
Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97
  • I think that one is "HeBo". See the "CreateSimpleForm.java" example and change it to use `PDType1Font.HELVETICA_BOLD`. – Tilman Hausherr Aug 31 '20 at 11:44
  • ok thanks. Yeah that should help . But my case is even more complicated. I have a text field with 3 words. First word not bold , second word bold, third word again not bold. :) – Marcela28 Aug 31 '20 at 11:46
  • Then you need three fields.Or a richtext field, but that one gets really complicated and beyond my capabilities. – Tilman Hausherr Aug 31 '20 at 11:47
  • I only have one field. – Marcela28 Aug 31 '20 at 11:50
  • Then it's not standard unless it is a richtext field (and even there I'm not sure). You could format the field yourself, i.e. do the appearance stream yourself. For that, get the PDFBox source code and look for AppearanceGeneratorHelper.java and PlainTextFormatter.java. – Tilman Hausherr Aug 31 '20 at 11:59
  • There is also PDVariableText . Maybe that I could use. Hm – Marcela28 Aug 31 '20 at 12:14
  • No, that is an abstract class that is the base class of several fields. – Tilman Hausherr Aug 31 '20 at 12:16
  • Ok. Are you familiar with setRichText() and setRichTextValue() ? Why is it so difficult to have a text field with many words and just one word should be bold . That is a normal use case. So the library should definitely offer this choice . It seems Apache PdfBox is not very user friendly respectively does not adhere to general principles. I never saw you specifiy font size in a string instead for example in apache poi you say setFontSize() . Finished. Very easy. – Marcela28 Aug 31 '20 at 14:22
  • The created pdf file has read write access for owner of file. But no rights for group or other. Do you know how I can set these rights with Apache pdfBox ? – Marcela28 Aug 31 '20 at 16:40
  • No I'm not familiar with setRichText() and setRichTextValue(). Re setting rights, see https://pdfbox.apache.org/1.8/cookbook/encryption.html – Tilman Hausherr Sep 01 '20 at 02:44
  • ok.thx. But I do not want to encrypt the file. I just want to set like chmod 777 . Do you understand – Marcela28 Sep 01 '20 at 08:24
  • Then call `document.setAllSecurityToBeRemoved(true);`. Please open new questions on SO for new questions, your original question has been answered, and this is now the 4th extra question you asked. (1. bold, 2. mixed, 3. richtext, 4. security). Please stay focused. – Tilman Hausherr Sep 01 '20 at 10:03
  • ok. I tried it. But still access rights on Linux is 640 and not 777. – Marcela28 Sep 01 '20 at 15:43
  • File object itself has setReadable(true) and setWritable(true) I found out – Marcela28 Sep 01 '20 at 16:49