Get information from a form

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

CheckBoxValues.java
/**
 * 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();
    }
}

Resources

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now