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