I tried 1.6 and 1.7 both, and the problem is same, it can't read any form from 
the test file

        PDAcroForm acroForm = catalog.getAcroForm(); //return null

the acroForm is null but the test pdf file does have  a form, can anybody help 
me?

post the src file and test file
///////////////////////////////
package Pdfbox.test;

import java.net.URL;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;

import java.util.Iterator;
import java.util.List;

import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentCatalog;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDField;

import org.apache.pdfbox.exceptions.CryptographyException;
import org.apache.pdfbox.exceptions.InvalidPasswordException;

public class ReadForms {

    private static final String pdfFile = "/root/test_readforms.pdf";// 
Book1.pdf";

    public ReadForms() {

    }

    public static void processField(PDField field, String sLevel, String 
sParent)
            throws IOException {
        List kids = field.getKids();
        if (kids != null) {
            Iterator kidsIter = kids.iterator();
            if (!sParent.equals(field.getPartialName())) {
                sParent = sParent + "." + field.getPartialName();
            }
            System.out.println(sLevel + sParent);
            // System.out.println(sParent + " is of type " +
            // field.getClass().getName());
            while (kidsIter.hasNext()) {
                Object pdfObj = kidsIter.next();
                if (pdfObj instanceof PDField) {
                    PDField kid = (PDField) pdfObj;
                    processField(kid, "|  " + sLevel, sParent);
                }
            }
        } else {
            String outputString = sLevel + sParent + "."
                    + field.getPartialName() + " = " + field.getValue()
                    + ",  type=" + field.getClass().getName();

            System.out.println(outputString);
        }
    }

    /**
     * This will read a PDF file and print out the form elements. <br /> see
     * usage() for commandline
     *
     * @param args
     *            command line arguments
     *
     * @throws IOException
     *             If there is an error importing the FDF document.
     * @throws CryptographyException
     *             If there is an error decrypting the document.
     */
    public static void main(String[] args) throws IOException,
            CryptographyException {

        // Create the PDF Document
        PDDocument doc = PDDocument.load(pdfFile);

        // Extract the catalog
        PDDocumentCatalog catalog = doc.getDocumentCatalog();

        // Retrieve the AcroForm
        PDAcroForm acroForm = catalog.getAcroForm();

        // Retrieve all fields that should be change
        List listField = acroForm.getFields();
        Iterator<PDField> it = listField.iterator();

        // Loop on each field
        while (it.hasNext()) {
            PDField field = (PDField) it.next();
            processField(field, "|--", field.getPartialName());
        }

    }
}

Reply via email to