XFA(也称为XFA表单)是指XML Forms Architecture,它用于增强Web表单的处理。Spire.PDF支持填充XFA表单,本篇文章将介绍如何使用Spire.PDF填充XFA。
包含域的源文档截图:
完整代码:
C#
//创建对象并加载PDF文件
PdfDocument doc = new PdfDocument();
doc.LoadFromFile(@"F:\DynamicXFA.pdf");
PdfFormWidget formWidget = doc.Form as PdfFormWidget;
//判断域是否为XFA表单 并且 XFA表单不为空
if (formWidget.IsDynamicForm && formWidget.XFAForm!= null)
{
//得到所有的XFA表单
List xfafields = formWidget.XFAForm.XfaFields;
foreach (XfaField xfaField in xfafields)
{
//判断当前域是否为XfaTextField
if (xfaField is XfaTextField)
{
XfaTextField xf = xfaField as XfaTextField;
//根据域名来设定域值
switch (xf.Name)
{
case "form1[0].#subform[0].EmployeeName[0]":
xf.Value = "E-iceblue";
break;
case "form1[0].#subform[0].Address[0]":
xf.Value = "Chengdu, China";
break;
case "form1[0].#subform[0].StateProv[0]":
xf.Value = "Sichuan Province";
break;
case "form1[0].#subform[0].ZipCode[0]":
xf.Value = "610093";
break;
case "form1[0].#subform[0].SSNumber[0]":
xf.Value = "000-00-0000";
break;
case "form1[0].#subform[0].HomePhone[0]":
xf.Value = "86-028-81705109";
break;
case "form1[0].#subform[0].CellPhone[0]":
xf.Value = "123456789";
break;
case "form1[0].#subform[0].Comments[0]":
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF";
break;
default:
break;
}
}
}
}
//保存结果文档
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF);
VB.NET
'创建对象并加载PDF文件
Dim doc As New PdfDocument()
doc.LoadFromFile("F:\DynamicXFA.pdf")
Dim formWidget As PdfFormWidget = TryCast(doc.Form, PdfFormWidget)
'判断域是否为XFA表单 并且 XFA表单不为空
If formWidget.IsDynamicForm AndAlso formWidget.XFAForm IsNot Nothing Then
'得到所有的XFA表单
Dim xfafields As List = formWidget.XFAForm.XfaFields
For Each xfaField As XfaField In xfafields
'判断当前域是否为XfaTextField
If TypeOf xfaField Is XfaTextField Then
Dim xf As XfaTextField = TryCast(xfaField, XfaTextField)
'根据域名来设定域值
Select Case xf.Name
Case "form1[0].#subform[0].EmployeeName[0]"
xf.Value = "E-iceblue"
Exit Select
Case "form1[0].#subform[0].Address[0]"
xf.Value = "Chengdu, China"
Exit Select
Case "form1[0].#subform[0].StateProv[0]"
xf.Value = "Sichuan Province"
Exit Select
Case "form1[0].#subform[0].ZipCode[0]"
xf.Value = "610093"
Exit Select
Case "form1[0].#subform[0].SSNumber[0]"
xf.Value = "000-00-0000"
Exit Select
Case "form1[0].#subform[0].HomePhone[0]"
xf.Value = "86-028-81705109"
Exit Select
Case "form1[0].#subform[0].CellPhone[0]"
xf.Value = "123456789"
Exit Select
Case "form1[0].#subform[0].Comments[0]"
xf.Value = "This demo shows how to fill XFA forms using Spire.PDF"
Exit Select
Case Else
Exit Select
End Select
End If
Next
End If
'保存结果文档
doc.SaveToFile("FillXfaField.pdf", FileFormat.PDF)
结果文档截图: