A predecessor of mine used an old (acrobat 3) SDK to create an API which
allows for fields to be placed on a PDF via a small GUI tool.  Using iText,
I am unable to create an AcroFields object based on the fields created with
this tool.  I have attached a sample PDF which has a single field
(EVENT_DATE) which was added using the tool.  The error seems to be deep in
the AcroFields class:

Error: java.lang.Exception: Error during field operation:
java.lang.Exception: Error getting AcroFields: java.lang.ClassCastException
java.lang.ClassCastException
        at com.lowagie.text.pdf.AcroFields.fill(AcroFields.java:99)
        at com.lowagie.text.pdf.AcroFields.<init>(AcroFields.java:75)
        at
com.lowagie.text.pdf.PdfStamperImp.getAcroFields(PdfStamperImp.java:305)
        at
com.lowagie.text.pdf.PdfStamper.getAcroFields(PdfStamper.java:215)
        at AcroFieldTest.main(AcroFieldTest.java:57)

The sample code below and the sample PDF attached (created via Acrobat4 and
the API) can be used to replicate the error.  I have several hundred PDFs
that have between 5 and 50 fields which I am hoping to use iText on.

Any ideas would be greatly appreciated.

Thank you
Noah Mattern
Enterprise Information Solutions

<---- sample code ---->

/*
 * AcroFieldTest.java
 *
 * Created on January 21, 2005, 9:46 AM
 */

/**
 *
 * @author  Noah Mattern
 */

import java.util.*;
import java.sql.*;
import java.io.*;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class AcroFieldTest {

    public static void main(String args[]) {

    try {
        byte[] pdfBytesOut = null;
        PdfReader reader = null;
        ByteArrayOutputStream pdfOutput  = null;
        PdfStamper stamper = null;
        AcroFields form = null;
        HashMap fieldInfo = null;
        String tokenValue = "";

        System.out.println("Start");

        // a PDF made via old API (attached)
        String filename = "c:\\temp\\FormFieldsAPI.pdf";   

        FileInputStream inStream = new FileInputStream( filename );
        int byteCount = inStream.available();
        byte[] pdfBytes = new byte[byteCount];
        inStream.read(pdfBytes, 0, byteCount);

        // read the PDF into an object
        reader = new PdfReader( pdfBytes );

        // make the output object
        pdfOutput = new ByteArrayOutputStream();

        stamper = new PdfStamper(reader, pdfOutput);       

        // create an AcroFields based on the fields on the form
        try {                
            stamper.setFormFlattening(true);
            try{
                /******* THIS LINE FAILS *******/
                form = (AcroFields) stamper.getAcroFields();     
            } catch( Exception e ){
                e.printStackTrace();
                throw new Exception( "Error getting AcroFields: " +
e.toString() );
            }   

        } catch( Exception e ){
            throw new Exception( "Error during field operation: " +
e.toString() );
        }
        
        // close the stamper object
        try{
            stamper.close();     
        }
        catch( Exception e ){
            System.out.println( "Unable to close the PdfStamper: " +
e.toString() );
        }

        System.out.println("End");

        }
        catch( Exception e ) {
            System.out.println( "Error: " + e.toString() );
        }
    }
}

Attachment: FormFieldsAPI.pdf
Description: Adobe PDF document

Reply via email to