XFA forms are XML-based forms created by Adobe's LiveCycle Designer tool, which offer enhanced features over the old AcroForms, like changeable text fields and support for running JavaScript. Spire.PDF supports to access the XFA forms in an existing PDF file and fill the fields with data.
Step 1: Initialize an instance of PdfDocument class and load a sample PDF file containing dynamic XFA forms.
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("DynamicXFA.pdf");
Step 2: Get all form widgets from the document.
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
Step 3: Get a list of XFA Fields.
List<XfaField> xfafields = formWidget.XFAForm.XfaFields;
Step 4: Traverse each XfaField in the list and judge if it is an XfaTextField, if yes, convert the type of XfaField as an XfaTextField and then assign value to the field based on the field name.
foreach (XfaField xfaField in xfaFields)
{
if (xfaField is XfaTextField)
{
XfaTextField xf = xfaField as XfaTextField;
switch (xfaField.Name)
{
case "EmployeeName":
xf.Value = "Gary";
break;
case "Address":
xf.Value = "Chengdu, China";
break;
case "StateProv":
xf.Value = "Sichuan Province";
break;
case "ZipCode":
xf.Value = "610093";
break;
case "SSNumber":
xf.Value = "000-00-0000";
break;
case "HomePhone":
xf.Value = "86-028-81705109";
break;
case "CellPhone":
xf.Value = "123456789";
break;
case "Comments":
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF";
break;
default:
break;
}
}
}
Step 5: Save the file.
doc.SaveToFile("FillXfaField.pdf",FileFormat.PDF);
Output:

Full Code:
[C#]
using Spire.Pdf;
using Spire.Pdf.Widget;
using System.Collections.Generic;
namespace FillXFAFormFields
{
class Program
{
private static IEnumerable xfaFields;
static void Main(string []args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("DynamicXFA.pdf");
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
List xfafields = formWidget.XFAForm.XfaFields;
foreach (XfaField xfaField in xfaFields)
{
if (xfaField is XfaTextField)
{
XfaTextField xf = xfaField as XfaTextField;
switch (xfaField.Name)
{
case "EmployeeName":
xf.Value = "Gary";
break;
case "Address":
xf.Value = "Chengdu, China";
break;
case "StateProv":
xf.Value = "Sichuan Province";
break;
case "ZipCode":
xf.Value = "610093";
break;
case "SSNumber":
xf.Value = "000-00-0000";
break;
case "HomePhone":
xf.Value = "86-028-81705109";
break;
case "CellPhone":
xf.Value = "123456789";
break;
case "Comments":
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF";
break;
default:
break;
}
}
}
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF);
}
}
}
[VB.NET]
Imports Spire.Pdf
Imports Spire.Pdf.Widget
Imports System.Collections.Generic
Namespace FillXFAFormFields
Class Program
Private Shared xfaFields As IEnumerable(Of XfaField)
Private Shared Sub Main(args As String())
Dim doc As New PdfDocument()
doc.LoadFromFile("DynamicXFA.pdf")
Dim formWidget As PdfFormWidget = TryCast(doc.Form, PdfFormWidget)
Dim xfafields__1 As List(Of XfaField) = formWidget.XFAForm.XfaFields
For Each xfaField As XfaField In xfaFields
If TypeOf xfaField Is XfaTextField Then
Dim xf As XfaTextField = TryCast(xfaField, XfaTextField)
Select Case xfaField.Name
Case "EmployeeName"
xf.Value = "Gary"
Exit Select
Case "Address"
xf.Value = "Chengdu, China"
Exit Select
Case "StateProv"
xf.Value = "Sichuan Province"
Exit Select
Case "ZipCode"
xf.Value = "610093"
Exit Select
Case "SSNumber"
xf.Value = "000-00-0000"
Exit Select
Case "HomePhone"
xf.Value = "86-028-81705109"
Exit Select
Case "CellPhone"
xf.Value = "123456789"
Exit Select
Case "Comments"
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF"
Exit Select
Case Else
Exit Select
End Select
End If
Next
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF)
End Sub
End Class
End Namespace
