2229E.pdf (2.2 MB)
Hello,
I’m trying to fill all fields inside this PDF file programmatically but for some reason it won’t work for checkboxes. Can you tell me what I’m doing wrong? Here is my code. As you can see I tried several things but none of them work.
public override void Run()
{
var pdf = new Aspose.Pdf.Document("2229E.pdf");
var xfaForm = new Aspose.Pdf.Facades.Form(pdf);
var fieldNames = pdf.Form.XFA.FieldNames;
// Tests
//var values = GetCheckBoxValues(pdf, "form1.page1.body.section3.question1.yes");
//pdf.Form.XFA["form1.page1.body.section3.question1"] = values[0];
//pdf.Form.XFA["form1.page1.body.section3.question1.yes"] = values[0];
//pdf.Form.XFA["form1.page1.body.section3.question1.yes"] = "";
//pdf.Form.XFA["form1.page1.body.section3.question1.yes"] = "1";
// More tests
//xfaForm.FillField("form1.page1.body.section3.question1", "1");
//xfaForm.FillField("form1.page1.body.section3.question1.yes", "1");
//xfaForm.FillField("form1.page1.body.section3.question1.yes", true);
foreach (var fieldName in fieldNames)
{
var type = xfaForm.GetFieldType(fieldName);
// More tests
if (type == Aspose.Pdf.Facades.FieldType.CheckBox || type == Aspose.Pdf.Facades.FieldType.Radio)
{
var items = GetCheckBoxValues(pdf, fieldName);
pdf.Form.XFA[fieldName] = items[0];
}
else if (type == Aspose.Pdf.Facades.FieldType.DateTime)
xfaForm.FillField(fieldName, DateTime.Today.ToShortDateString());
else
xfaForm.FillField(fieldName, fieldName.Right(10));
}
var path = $"{DateTime.Now.Ticks}.pdf";
pdf.Save(path);
System.Diagnostics.Process.Start(path);
}
private 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;
}