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
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/19698771/checking-off-pdf-checkbox-with-itextsharp
  4.  *
  5.  * Given a check box in a form, how do we know which values to use in setField?
  6.  */
  7. package sandbox.acroforms;
  8.  
  9. import com.itextpdf.text.pdf.AcroFields;
  10. import com.itextpdf.text.pdf.PdfReader;
  11.  
  12. import java.io.IOException;
  13.  
  14. public class CheckBoxValues {
  15.  
  16.     public static final String SRC = "../sandbox/resources/pdfs/datasheet.pdf";
  17.     public static final String FIELD = "CP_1";
  18.    
  19.     public static void main(String[] args) throws IOException {
  20.         CheckBoxValues app = new CheckBoxValues();
  21.         System.out.println(app.getCheckboxValue(SRC, FIELD));
  22.     }
  23.    
  24.     public String getCheckboxValue(String src, String name) throws IOException {
  25.         PdfReader reader = new PdfReader(SRC);
  26.         AcroFields fields = reader.getAcroFields();
  27.         // CP_1 is the name of a check box field
  28.         String[] values = fields.getAppearanceStates("CP_1");
  29.         StringBuffer sb = new StringBuffer();
  30.         for (String value : values) {
  31.             sb.append(value);
  32.             sb.append('\n');
  33.         }
  34.         return sb.toString();
  35.     }
  36. }

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