I am able to update the text fields but facing issues in updating the checkboxes in the first form in attached document. Its been a struggle from days. please guide me on this.
I have tried something like
string[] names = doc.Form.XFA.FieldNames;
//set field values
doc.Form.XFA[names[13]] = “Off”;
tried this also, seems the field tracking is wrong
pdfForm.FillField(“form1[0].name[13]”, “Off”);
Thanks.
Hi there,
Thanks for your query. Please note CheckBox field in XFA has child node items which contains possible values for the field. You can obtain these values and set one of these values as following.
using System.Collections.Generic;
using System.Xml;
using Aspose.Pdf;
List<string> GetCheckBoxValues(Document doc, string fieldName)
{
XmlNode tmpl = doc.Form.XFA.GetFieldTemplate(fieldName);
XmlNodeList items = tmpl.SelectNodes("tpl:items/*", doc.Form.XFA.NamespaceManager);
List<string> res = new List<string>();
foreach (XmlNode item in items)
{
res.Add(item.InnerText);
}
return res;
}
Document doc = new Document("g-28.pdf");
List<string> items = GetCheckBoxValues(doc, "form1[0].#subform[0].Line4_Checkbox[0]");
doc.Form.XFA["form1[0].#subform[0].Line4_Checkbox[0]"] = items[0];
doc.Save("result.pdf");
Please feel free to contact us for any further assistance.
Best Regards,
Thank you very much. This is what I need.
Hi,
In continuation to my above query, there are other checkboxes which are in group, they are no working if I use same logic which you have shared
List items1 = GetCheckBoxValues(doc, “form1[0].#subform[0].Line3b_Unit[2]”);
doc.Form.XFA[“form1[0].#subform[0].Line3b_Unit[2]”] = items1[0];
I wanted to check the “Petitioner” checkbox option, below is the logic I tried-didn’t work, where as if I select the “applicant” checkbox option it is working
List items = GetCheckBoxValues(doc, “form1[0].#subform[0].Line4_Checkbox[2]”);
doc.Form.XFA[“form1[0].#subform[0].Line4_Checkbox[2]”] = items[0];
Thanks for the support.
Hi there,
Thanks for your inquriy. Please note in checkbox group you need to assign value to first checkbox, it will check appropriate checkbox accordingly. Please check following code snippet, it will help you to accomplish the task.
List<string> items = GetCheckBoxValues(doc, "form1[0].#subform[0].Line4_Checkbox[2]");
doc.Form.XFA["form1[0].#subform[0].Line4_Checkbox[0]"] = items[0];
//List items = GetCheckBoxValues(doc, "form1[0].#subform[0].Line3b_Unit[2]");
//doc.Form.XFA["form1[0].#subform[0].Line3b_Unit[0]"] = items[0];
//List items = GetCheckBoxValues(doc, "form1[0].#subform[1].Line12b_Unit[2]");
//doc.Form.XFA["form1[0].#subform[1].Line12b_Unit[0]"] = items[0];
Please feel free to contact us for any further assistance.
Best Regards,
Hi ,
I have tried same approach to check the checkbox but I am not able to see the checked checkbox
after saving document.
What can be the reason?
Thanks,
Vinod
Kindly send us your source PDF document and code. We will investigate and share our findings with you.