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)
结果文档截图: