Here's Java code to get all field names from a PDF file using Apache PDFBox library:
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class PDFFieldExtractor {
public static void main(String[] args) {
String pdfPath = "path/to/your/file.pdf";
try {
getAllFieldNames(pdfPath);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void getAllFieldNames(String pdfPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(pdfPath))) {
// Get the document's AcroForm
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
if (acroForm != null) {
// Get all fields
List<PDField> fields = acroForm.getFields();
System.out.println("Total fields found: " + fields.size());
System.out.println("\nField details:");
System.out.println("----------------------------------------");
for (PDField field : fields) {
// Get field name
String fieldName = field.getFullyQualifiedName();
// Get field type
String fieldType = field.getFieldType();
// Get alternate name (if exists)
String alternateFieldName = field.getAlternateFieldName();
System.out.println("Field Name: " + fieldName);
System.out.println("Field Type: " + fieldType);
if (alternateFieldName != null && !alternateFieldName.isEmpty()) {
System.out.println("Alternate Name: " + alternateFieldName);
}
System.out.println("----------------------------------------");
}
} else {
System.out.println("No AcroForm found in this PDF.");
}
}
}
// Alternative method that returns a list of field names
public static List<String> getFieldNamesList(String pdfPath) throws IOException {
List<String> fieldNames = new java.util.ArrayList<>();
try (PDDocument document = PDDocument.load(new File(pdfPath))) {
PDAcroForm acroForm = document.getDocumentCatalog().getAcroForm();
if (acroForm != null) {
for (PDField field : acroForm.getFields()) {
fieldNames.add(field.getFullyQualifiedName());
}
}
}
return fieldNames;
}
}
To use this code, you'll need to add Apache PDFBox to your project dependencies:
Maven:
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.29</version>
</dependency>
Gradle:
implementation 'org.apache.pdfbox:pdfbox:2.0.29'
The code provides two methods:
-
getAllFieldNames() - Prints detailed information about each field including:
- Field name (fully qualified name)
- Field type (e.g., "Tx" for text, "Btn" for button, "Ch" for choice, "Sig" for signature)
- Alternate field name (if available)
-
getFieldNamesList() - Returns a simple list of field names if you just need the names without additional details.
Common field types you might encounter:
- Tx: Text field
- Btn: Button (includes checkboxes, radio buttons, and push buttons)
- Ch: Choice field (includes list boxes and combo boxes)
- Sig: Signature field
The code handles all field types automatically and will extract names from text fields, checkboxes, radio buttons, dropdown lists, signature fields, and any other form field types in the PDF.