Get all editable fields from a PDF file

Lots of libraries are capable to read, edit or manipulate PDF files. I have been using iText for filling in an editable form in a PDF file.
This library is pretty simple to use, the following snippet will let you know the names of the fields that are editable in a PDF file.

File f = new File("D:/myfile.pdf");
PdfReader reader = null;

// Use try-with-resources for streams, PdfReader variable cannot be declared in this statement because it does
// not implement Closeable interface (maybe it will in a future release !)
try (InputStream pdfIn = new FileInputStream(f); ByteArrayOutputStream pdfOut = new ByteArrayOutputStream()) {

    // Read PDF with iText classes
    reader = new PdfReader(pdfIn);
    PdfStamper stamper = new PdfStamper(reader, pdfOut);

    // Get the form from the PDF
    AcroFields form = stamper.getAcroFields();

    // For each field of the form : display its name
    for (Entry<String, Item> entry : form.getFields().entrySet()) {

} catch (IOException e) {
} catch (DocumentException e) {
} finally {
    // Do not forget to close the PdfStamper otherwise your ByteArrayOutputStream will be empty !

Put this snippet in a main and you can test it ! I used iText 5.5.3 in this example.

You can display other information about each form (see Java classes Item and AcroFields), for example you can display the type of each field : input text, checkbox, radio button, etc.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.