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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With