Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF form fill with PDFBox doesn't work

I have a PDF file with some form fields that I need to fill in from Java code. I use PDFBox library for this, and this code:

PDDocument pdfDoc = PDDocument.load("C:\\Users\\igor\\Desktop\\test.pdf");
PDDocumentCatalog docCatalog = pdfDoc.getDocumentCatalog();
PDAcroForm acroForm = docCatalog.getAcroForm();
PDField field = acroForm.getField("applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]");
if (field != null) {
    field.setValue("Milan");
} else {
    System.err.println("No field found with name:" + "applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]");
}
pdfDoc.save("C:\\Users\\igor\\Desktop\\testout.pdf");
pdfDoc.close();

The PDF is not created by me, so I don't know what type of form the file uses (if I understand correctly, there are FDF and XFA forms). Since the PDF is not created by me, I used this tool http://support.persits.com/pdf/demo_formfields.asp to find out the names of the form fields, and it gave me this:

applicationPrepaid[0].#pageSet[0].Pagina1[0].txtFirstName[0]

When I use this long field name, I don't get any errors, but the resulting PDF does not contain the value I put in the field. I thought that maybe there was something wrong with the field name, so I used Pdftk tool which gave me just txtFirstName for the field name. But when I use just that, I get the No field found with name: txtFirstName error. Help?

like image 527
stojke Avatar asked Nov 19 '12 10:11

stojke


People also ask

Is PDFBox free for commercial use?

Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative ...

How do I make a PDF editable in Java?

Create Fillable PDF Forms using Java You can create fillable PDF form in Java applications by following the steps below: Create a PDF file with a blank page. Add TextBox field and RadioButton field on the page. Save the output PDF file.


1 Answers

Well I realise this question is very old now, but I stumbled across it and thought I should post my solution.

You can find out the id's of the fields in the PDF form using PDFBox. It has a very rich API, it just unfortunately requires lots of reading to figure out what you have to do.

To get the name of the form field, you want to use getFullyQualifiedName which is part of the PDField class.

Unfortunately, you can't get all the filenames in one go (that I can tell) from the PDAcroform class, so you can simply create an array of the form names and then loop through each one.

e.g.

    // Load the pdfTemplate
    pdfTemplate = PDDocument.load(file);

    PDDocumentCatalog docCatalog = pdfTemplate.getDocumentCatalog();
    PDAcroForm acroForm = docCatalog.getAcroForm();

    // Get field names
    List<PDField> fieldList = acroForm.getFields();

    // String the object array
    String[] fieldArray = new String[fieldList.size()];
    int i = 0;
    for (PDField sField : fieldList) {
        fieldArray[i] = sField.getFullyQualifiedName();
        i++;
    }

    // Loop through each field in the array and do something
    for (String f : fieldArray) {
        PDField field = acroForm.getField(f);

        System.out.println("f is: " + f);
        if (f.contains("EXAMPLE FORM FIELD NAME")) {
            DO SOMETHING
                            String value = "example value";
            field.setValue(value);
            System.out.println("printed: " + value + " to: " + f);
        }
    }

    // Save edited file
    pdfTemplate.save(sPdfTemplate);
    pdfTemplate.close();

Hope this helps someone.

Cheers

like image 181
sam9046 Avatar answered Sep 30 '22 13:09

sam9046