Get information from a form

Tags: formscheck boxfill formiText 5

This example was written in answer to the question How to check a check box?

Files: 
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/19698771/checking-off-pdf-checkbox-with-itextsharp
 * 
 * Given a check box in a form, how do we know which values to use in setField?
 */
package sandbox.acroforms;
 
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
 
import java.io.IOException;
 
public class CheckBoxValues {
 
    public static final String SRC = "../sandbox/resources/pdfs/datasheet.pdf";
    public static final String FIELD = "CP_1";
 
    public static void main(String[] args) throws IOException {
        CheckBoxValues app = new CheckBoxValues();
        System.out.println(app.getCheckboxValue(SRC, FIELD));
    }
 
    public String getCheckboxValue(String src, String name) throws IOException {
        PdfReader reader = new PdfReader(SRC);
        AcroFields fields = reader.getAcroFields();
        // CP_1 is the name of a check box field
        String[] values = fields.getAppearanceStates("CP_1");
        StringBuffer sb = new StringBuffer();
        for (String value : values) {
            sb.append(value);
            sb.append('\n');
        }
        return sb.toString();
    }
}
File nameRaw URLUpdated
CheckBoxValues.javaCheckBoxValues.java2015-11-06 1:40 pm
Resources: 
File nameRaw URLUpdated
datasheet.pdfdatasheet.pdf2015-11-06 1:41 pm