9

I am in the process of filling in a pdf form created using Acrobat pro with iTextSharp & C#, and have found myself stuck when attempting to tick a checkbox field.

I have it working for radio buttons and textboxes but cannot seem to get the checkbox working. I have also confirmed the checkbox name in this case "Q7b" is correct in the acrobat document and can find it on the form using the following code

private string getfieldnames(AcroFields fields)
{
    StringBuilder sb = new StringBuilder();

    foreach (string key in fields.Fields.Keys)
    {

        sb.Append(key + Environment.NewLine);
    }
    return sb.ToString();

}

The code I am using to update the checkbox is below

using (MemoryStream pdfFlat = new MemoryStream())
{

    PdfReader pdfReader = new PdfReader(strPath);
    PdfStamper pdfStamp = new PdfStamper(pdfReader, pdfFlat);
    AcroFields fields = pdfStamp.AcroFields;

    //textfields
    fields.SetField("Initiating_Doctor", "Doctor A");
    fields.SetField("Speciality", "Surgeon");

    //Radiobuttons
    fields.SetField("PRELIM_Q1", "Yes");
    fields.SetField("PRELIM_Q2", "No");
    fields.SetField("PRELIM_Q3", "No");
    fields.SetField("PRELIM_Q4", "No");

    //checkbox - Set the checkbox to checked but this does not work.
    fields.SetField("Q7b", "Yes");

    pdfReader.Close();
    pdfStamp.FormFlattening = true;
    pdfStamp.FreeTextFlattening = true;
    pdfStamp.Writer.CloseStream = false;
    pdfStamp.Close();
}

Any help would be greatly appreciated.

Brad

7 Answers 7

17

Setting the field value to the export value of the checkbox will cause it to be checked. So if the export value is "Yes", then setting the value of the field to "Yes" will tick the checkbox. If the export value is something else (e.g. "On"), you will need to set the field value to that in order to tick the box.

4

I also tried On and Off for checkbox and it did not work. Then I opened in Adobe LiveCycle Designer. In the checkbox binding property Changed On Value Of Value properties. Set them To Yes and NO. Now it is working for me.

3

Checkbox values are usually "On" and "Off". Radio groups can use whatever values they wish (plus "Off").

3
  • Thanks for your help Mark. Upon taking a break and a closer look at the pdf document I found the Export value property of the checkbox had a strange value. setting this to On and changing the code to match has resolved the issue fields.SetField("Q7b", "On");
    – Brad Jones
    Jan 8, 2011 at 0:34
  • 1
    Careful... that "strange value" might be necessary wherever the form is normally submitted. If you're only ever using it internally, fine. But if you ever try to go back to the original submit target, you'll need to translate that field value back again. Easier to revert it and use the strange value in your code. Jan 8, 2011 at 1:07
  • Thanks for the warning. It is an internal thing so chaning the value will have no flow on effect.
    – Brad Jones
    Jan 18, 2011 at 9:56
3

For checkboxes you need to pass "1" as value.

Try following

fields.SetField("PRELIM_Q1", "1");
1

Not sure if it's the library or how my PDF template was created, but I tried yes/no, on/off, true/false ... none of those options worked. Using "1" and "0" worked though.

I'd suggest experimenting with these 4 options until you find the values that work for you.

0

fields.SetField("your_field", "On"); On works for me by default.

1
  • 1
    looks like aaron has already provided a more accurate version of this answer.
    – MilkyTech
    Jun 6, 2014 at 14:59
0

You can use this tool https://www.pdfescape.com/ to obtain the export value.

  • Upload the pdf file
  • Right click on the checkbox and select Unlock form field.
  • Again right click on the checkbox and select Object Properties.
  • Now, you can see the attributes of the control, there's the famous export value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.