XFA form filling

how to fill check box and radio button fields in XFA forms

Thanks,
Chinnu

Hi Chinnu,


Thanks for contacting support.

In order to set value for CheckBox field, you may consider using following code snippet.

[C#]

//open document<o:p></o:p>

Document pdfDocument = new Document(myDir + "BCTR Main.pdf");

FormType formType = pdfDocument.Form.Type;

foreach (string field in pdfDocument.Form.XFA.FieldNames)

{

Console.WriteLine("field name is :{0}", field);

// Console.ReadKey();

}

pdfDocument.Form.XFA["BSAForm[0].Header[0].FilingName[0]"] = "its a test value.";

//Checkbox field

pdfDocument.Form.XFA["BSAForm[0].Header[0].TypeOfFiling[0].initialReport[0]"] = "";

//save updated document

pdfDocument.Save(myDir + “XFAForm_out.pdf”);


However the CheckBox field in XFA may have child node items, which may contain possible values for the field. These values may be obtained using following code snippet:

[C#]

List<string>
GetCheckBoxValues(Document doc, string fieldName)<o:p></o:p>

{

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;

}


And you could set one of these values to the field:

[C#]

Document doc = new
Document(“BCTR
Main.pdf”
);<o:p></o:p>

List<string> items = GetCheckBoxValues(doc, "BSAForm[0].Header[0].TypeOfFiling[0].initialReport[0]");

doc.Form.XFA["BSAForm[0].Header[0].TypeOfFiling[0].initialReport[0]"] = items[0];

doc.Save(“result.pdf”);


In order to set RadioButton field, please try using following code snippet.

[C#]

Aspose.Pdf.Document
doc = new Aspose.Pdf.Document(“TEST_CM111.pdf”);<o:p></o:p>

Aspose.Pdf.Facades.Form form = new Aspose.Pdf.Facades.Form(doc);

// set index value for radio button field

form.FillField("form1[0].#subform[2].MonthofFirstInstallment[0]", 1);

doc.Save(“RESULTS.pdf”);

In case you still face any issue, please share the resource/input XFA forms.

I’m having a similar issue. I have an XFA form, that I did not create nor have the ability to change, but I am attempting to fill the form in using C#. I’m running into an issue with a radio button list. I’ve scoured your site and I can’t seem to find a good example of simply how to address the radio button fields.

The code snippets for Working with XFA or Filling out XFA forms don’t deal with radio buttons.

The field in question is named: “form1[0].#subform[0].Commute[0].RadioButtonList[0]”, on the form it has 3 options: Yes, No, and N/A. I’m just trying to flag the correct option, but I can’t find the correct syntax.

Also, in my version of Aspose, the method GetCheckBoxValues() doesn’t seem to exist, or I can’t find it anywhere.

Please help.

@jjil

Thank you for contacting support.

Would you please share the PDF document with us so that we may investigate further to help you out. Moreover, please ensure using Aspose.PDF for .NET 18.11 in your environment.

C10jan18.pdf (1.5 MB)
Attached is the file. Thank you for looking at it.

@jjil

Thank you for sharing the sample file.

You may set the value of the radio button with below code snippet:

Document document = new Document(dataDir + "C10jan18.pdf");
document.Form.XFA["form1[0].#subform[0].Commute[0].RadioButtonList[0]"] = "3";
//save updated document
document.Save(dataDir + "Radio_18.12.pdf");

We hope this will be helpful. Please feel free to contact us if you need any further assistance.

Wow. I feel I should have been able to reason that one out, now that I see it. Thank you for your quick response. That seems to have worked.

I am also having trouble with adding multiple pages. I would like to add multiple versions of this document as additional pages in the same PDF. I have tried the following without result.

        Document pdfDocument = new Document(physicalPath);
        Document altDocument = new Document(physicalPath);
        pdfDocument.Pages.Add(altDocument.Pages);
        pdfDocument.Save(ms);

@jjil

Thank you for your kind feedback.

We are glad to know that suggested approach is working in your environment.

Moreover, would you please elaborate if you want to add pages of already shared PDF document C10jan18.pdf or some other document. Also mention with which file do you want to add the pages, and the problem you are currently facing. Please share all respective files so that we may investigate further to help you out.

Furthermore, please always create separate posts for separate topics so that we may assist you efficiently.

Thank you. I have created a new thread at the following location with all the details.

@jjil

Thank you for sharing all details.

We have replied you in other thread. Kindly follow up in respective thread.