javaformspdffdfpdfa

How to fill a PDF/A-1 in Java


I've got a pdf/a-1 form which I have to fill in Java.

The problem is, it works perfectly fine with normal pdf but not with pdf/a-1.

Using a normal pdf form, I display the form fields and implement an HashMap from which I generate myself an fdf file.

Then I import my fdf file into the normal pdf form using pdftk.exe and it works fine.

Using the pdf/a-1 form, I can't display fields and If I try to import an fdf file, it doesn't fill the form.

I don't know if there is a simple method which can fill a pdf/a-1 form or if I should use some mechanisms to make my pdf/a-1 fillable?

I've tried pdfbox and itext but I didn't really find anything.

Below are some really simplified code implementations which work with regular pdf forms but do not with pdf/a-1 forms whose fields seem flatten.

Display fields with iText

    File pdfFile = new File(PDF_FILE_PATH);

    // Display fields
    PdfReader reader = new PdfReader(pdfFile.getAbsolutePath());
    System.out.println("\n-----Champs du pdf-----");
    for (String field : reader.getAcroFields().getFields().keySet()) {
        if (field instanceof String) {
            System.out.println("String " + field);
        } else {
            System.out.println(field.getClass().getSimpleName() + " " + field.toString());
        }
    }
    System.out.println("----------\n");
    reader.close();

Using PDFBox

    File pdfFile = new File(PDF_FILE_PATH);

    // HashMap Test
    HashMap<String, Object> map = new HashMap<String, Object>();
    map.put("age", "37");
    map.put("name", "Jack");
    map.put("address", "930 rue des Pommes");
    map.put("lastname", "Wilson");
    map.put("description",
            "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.");


    // Load the existing pdf file
    PDDocument pdd = PDDocument.load(pdfFile);
    PDAcroForm acroForm = pdd.getDocumentCatalog().getAcroForm();


    // For each item, sets a value to the corresponding field
    for (String item : map.keySet()){
        acroForm.getField(item).setValue((String) map.get(item));
    }

    // Save
    pdd.save(pdfFile);
    pdd.close();

Using pdftk

The implementation using pdftk looks like the one above in many ways but I generate from the HashMap an fdf file and use the following command line:

pdftk.exe myForm.pdf fill_form myFdf.fdf output fulfilled_form.pdf flatten

Here are my two forms, the regular one, and the one using the pdf/a-1 format:

Regular PDF

PDF/A-1

Hope you could help.

Best regards.


Solution

  • I have downloaded the file formulaire-a1.pdf because I wanted to write a small proof of concept to fill it out. However, when I examined this file, I discovered that it has no form fields.

    See the screen shot below:

    enter image description here

    I don't see any AcroForm entry in the Catalog; I don't see any fields.

    Please compare the above screen shot with this one:

    enter image description here

    This is the "non PDF/A" version of your form. Here we do have an AcroForm and a field array. This is a form that can be filled out; formulaire-a1.pdf isn't.