We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

XFA form checkbox not being filled programmatically

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;
    }

@MDUpperbee

We were able to notice the issue in our environment while testing the scenario using Aspose.PDF for .NET 22.3. Therefore, an issue as PDFNET-51604 has been logged in our issue tracking system for the sake of correction. We will further look into its details and keep you posted with the status of its rectification. Please be patient and spare us some time.

We are sorry for the inconvenience.

Thank you for looking into this issue.