Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking off pdf checkbox with itextsharp

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?

like image 401
user948060 Avatar asked Oct 31 '13 05:10

user948060


1 Answers

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:

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("IsNo");
    StringBuffer sb = new StringBuffer();
    for (String value : values) {
        sb.append(value);
        sb.append('\n');
    }
    return sb.toString();
}

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:

screen shot

like image 90
Bruno Lowagie Avatar answered Sep 23 '22 04:09

Bruno Lowagie