How to check a check box?

I've tried so many different ways, but I can't get the check box to be checked! Here's what I've tried: var reader = new iTextSharp.text.pdf.PdfReader(originalFormLocation); using (var stamper = n

I've tried so many different ways, but I can't get the check box to be checked! Here's what I've tried:

var reader = new iTextSharp.text.pdf.PdfReader(originalFormLocation);
using (var stamper = new iTextSharp.text.pdf.PdfStamper(reader,ms)) {
    var formFields = stamper.AcroFields;
    formFields.SetField("IsNo", "1");
    formFields.SetField("IsNo", "true");
    formFields.SetField("IsNo", "On");
}
None of them work. Any ideas?

Posted on StackOverflow on Oct 31, 2013 by user948060

You shouldn't "guess" for the possible values. You need to use a value that is stored in the PDF. Try the CheckBoxValues example to find these possible values:

PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map fields = form.getFormFields();
PdfFormField field = fields.get(FIELD);
StringBuilder sb = new StringBuilder();
String[] states = field.getAppearanceStates();
for (String state : states) {
    sb.append(state);
    sb.append('\n');
}
System.out.println(sb);

Or take a look at the PDF using RUPS. Go to the widget annotation and look for the normal (/N) appearance (AP) states. In my example they are /Off and /Yes:

Check box appearance states
Check box appearance states

Click this link if you want to see how to answer this question in iText 5.



Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial
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