How to fill the XFA form fields in C# and VB.NET?
The Syncfusion Essential PDF is a feature rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
Using this library, you can fill the XFA form fields in C# and VB.NET. This application uses the following XFA form and fills its fields.
Steps to fill the XFA form fields programmatically in C#:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.Pdf.Xfa;
VB.NET
Imports Syncfusion.Pdf.Xfa
- Include the following code snippet in main method of Program.cs file, to fill the XFA form fields by manually parsing the complete field name through the TryGetFieldByCompleteName method of PdfLoadedXfaForm.
C#
//Load the existing XFA document PdfLoadedXfaDocument document = new PdfLoadedXfaDocument("../../Input/XfaFormTemplate.pdf"); //Load the XFA form from loaded XFA document PdfLoadedXfaForm form = document.XfaForm; //Fill the combo box field PdfLoadedXfaField field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Title[0]"); (field as PdfLoadedXfaComboBoxField).SelectedValue = "Mr"; //Fill the text box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].FirstName[0]"); (field as PdfLoadedXfaTextBoxField).Text = "Simons"; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].LastName[0]"); (field as PdfLoadedXfaTextBoxField).Text = "Bistro"; //Fill the date time field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].DateOfBirth[0]"); (field as PdfLoadedXfaDateTimeField).Value = new DateTime(1989, 5, 21); //Fill the text box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Company[0]"); (field as PdfLoadedXfaTextBoxField).Text = "XYZ Pvt Ltd"; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].JobTitle[0]"); (field as PdfLoadedXfaTextBoxField).Text = "Developer"; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].StreetAddress[0]"); (field as PdfLoadedXfaTextBoxField).Text = "Vinbaeltet 34"; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].City[0]"); (field as PdfLoadedXfaTextBoxField).Text = "Denmark"; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Country[0]"); (field as PdfLoadedXfaTextBoxField).Text = "US"; //Fill the combo box field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].State[0]"); (field as PdfLoadedXfaComboBoxField).SelectedIndex = 1; //Fill the numeric field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].PostalCode[0]"); (field as PdfLoadedXfaNumericField).NumericValue = 24534; //Fill the list box field. field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].SpecialDietaryNeeds[0]"); (field as PdfLoadedXfaListBoxField).SelectedItems = new string[] { "Vegan", "Diary Free" }; //Fill the check box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].E-Mail[0]"); (field as PdfLoadedXfaCheckBoxField).IsChecked = true; field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Phone[0]"); (field as PdfLoadedXfaCheckBoxField).IsChecked = true; //Fill the radio button field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].MembershipStatus[0]"); (field as PdfLoadedXfaRadioButtonGroup).Fields[1].IsChecked = true; //Save the document document.Save("FilledXFAForm.pdf"); //Close the instance of PDFLoadedXfaDocumnet document.Close(); //Open the saved document for viewing System.Diagnostics.Process.Start("FilledXFAForm.pdf");
VB.NET
'Load the existing XFA document Dim document As PdfLoadedXfaDocument = New PdfLoadedXfaDocument("../../Input/XfaFormTemplate.pdf") 'Load the XFA form from loaded XFA document Dim form As PdfLoadedXfaForm = document.XfaForm 'Fill the combo box field Dim field As PdfLoadedXfaField = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Title[0]") CType(field, PdfLoadedXfaComboBoxField).SelectedValue = "Mr" 'Fill the text box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].FirstName[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "Simons" field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].LastName[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "Bistro" 'Fill the date time field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].DateOfBirth[0]") CType(field, PdfLoadedXfaDateTimeField).Value = New DateTime(1989, 5, 21) 'Fill the text box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Company[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "XYZ Pvt Ltd" field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].JobTitle[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "Developer" field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].StreetAddress[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "Vinbaeltet 34" field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].City[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "Denmark" field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Country[0]") CType(field, PdfLoadedXfaTextBoxField).Text = "US" 'Fill the combo box field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].State[0]") CType(field, PdfLoadedXfaComboBoxField).SelectedIndex = 1 'Fill the numeric field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].PostalCode[0]") CType(field, PdfLoadedXfaNumericField).NumericValue = 24534 'Fill the list box field. field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].SpecialDietaryNeeds[0]") CType(field, PdfLoadedXfaListBoxField).SelectedItems = New String() {"Vegan", "Diary Free"} 'Fill the check box fields field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].E-Mail[0]") CType(field, PdfLoadedXfaCheckBoxField).IsChecked = True field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].Phone[0]") CType(field, PdfLoadedXfaCheckBoxField).IsChecked = True 'Fill the radio button field field = form.TryGetFieldByCompleteName("form1[0].subform1[0].subform3[0].MembershipStatus[0]") CType(field, PdfLoadedXfaRadioButtonGroup).Fields(1).IsChecked = True 'Save the document document.Save("FilledXFAForm.pdf") 'Close the instance of PDFLoadedXfaDocumnet document.Close() 'Open the saved document for viewing System.Diagnostics.Process.Start("FilledXFAForm.pdf")
You can also get the available complete field names by using the CompleteFieldNames property of PdfLoadedXfaForm.
C#
//Get the complete field names string[] completeFieldNames = form.CompleteFieldNames;
VB.NET
'Get the complete field names Dim completeFieldNames As String() = form.CompleteFieldNames
A complete work sample to fill XFA form fields can be downloaded from Fill-XFA-Form.zip.
By executing the program, you will get the PDF document with filled XFA form as follows.
The Syncfusion Essential PDF supports creating, filling and flattening the PDF document with following XFA form fields:
- Text box field
- Numeric field
- DateTime field
- Combo box field
- Radio button field
- List box field
- Check box field
- Text Element
- Button
- Line
- Rectangle
- Circle
- Image
Take a moment to peruse the documentation, where you will find other options like creating static and dynamic XFA forms, nested XFA sub forms, validating, and flattening XFA form fields and removing dynamic fields.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to create XFA form and fill XFA form fields in existing PDF document.
See Also:
Create, fill and flatten PDF form fields
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.