VolleyJosh
VolleyJosh

Reputation: 161

How to get the content of PDF form text fields using pdfbox?

I'm using this to get the text of a PDF file using org.apache.pdfbox

File f = new File(fileName);  
      if (!f.isFile()) {
             System.out.println("File " + fileName + " does not exist.");
         return null;
    }

        try {
            parser = new PDFParser(new FileInputStream(f));
        } catch (Exception e) {
             System.out.println("Unable to open PDF Parser.");
            return null;
        }
   try {
           parser.parse();
             cosDoc = parser.getDocument();
           pdfStripper = new PDFTextStripper();           
          pdDoc = new PDDocument(cosDoc);
            parsedText = pdfStripper.getText(pdDoc);
        } catch (Exception e) {
            e.printStackTrace();
        }

It works great for the PDFs I've used it on so far. Now I have a PDF form that has editable text fields in it. My code does not return the text inside the fields. I would like to get that text. Is there a way to get it using PDFBox?

Upvotes: 4

Views: 12627

Answers (3)

J Smp
J Smp

Reputation: 1

For those trying to use this same method nowadays.

public static void listFields(PDDocument doc) throws Exception {
    PDDocumentCatalog catalog = doc.getDocumentCatalog();
    PDAcroForm form = catalog.getAcroForm();
    List<PDField> fields = form.getFields();

    for(PDField field: fields) {
        Object value = field.getValueAsString();
        String name = field.getFullyQualifiedName();
        System.out.print(name);
        System.out.print(" = ");
        System.out.print(value);
        System.out.println();
    }
}

Upvotes: 0

Sandeep
Sandeep

Reputation: 95

PDFieldTreeNode doesn't seem to be supported anymore. Try PDField

Upvotes: 1

ASu
ASu

Reputation: 156

This is how you get key/value for AcroForms: (This particular program prints it to the console.)

package pdf_form_filler;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.*;
import java.io.File;
import java.util.*;

public class pdf_form_filler {

    public static void listFields(PDDocument doc) throws Exception {
        PDDocumentCatalog catalog = doc.getDocumentCatalog();
        PDAcroForm form = catalog.getAcroForm();
        List<PDFieldTreeNode> fields = form.getFields();

        for(PDFieldTreeNode field: fields) {
            Object value = field.getValue();
            String name = field.getFullyQualifiedName();
            System.out.print(name);
            System.out.print(" = ");
            System.out.print(value);
            System.out.println();
        }
    }

    public static void main(String[] args) throws Exception {
        File file = new File("test.pdf");
        PDDocument doc = PDDocument.load(file);
        listFields(doc);
    }

}

Upvotes: 8

Related Questions