Hi
Hi Rose,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Aspose.Pdf DOM supports XFA forms via XFA class. Please see the following code segments to access / process the XFA Form Fields using Aspose.Pdf for .NET.
1) XFA property allows access to values of XFA fields, for example below code will set the value of XFA Field “name1” in your template file:
Aspose.Pdf.Generator.Pdf pdf = newAspose.Pdf.Generator.Pdf();
Documentdoc = new Document("E:\\Ap Data\\March2012\\test.pdf");
//set XFA field value
doc.Form.XFA["form[0].name1[0]"] = "Text Value";
doc.Save("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Documentdoc1 = new Document("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
//get XFA field value
Console.WriteLine(doc1.Form.XFA["form[0].name1[0]"]);
2) Template property allows access to XML which describes form template (i.e. field appearance, etc), for example below code will make “name1” field as read only:
Documentdoc = new Document("E:\\Ap Data\\March2012\\test.pdf");
XmlNamespaceManagernsmgr =
new XmlNamespaceManager(doc.Form.XFA.Template.OwnerDocument.NameTable);
nsmgr.AddNamespace("tpl", doc.Form.XFA.Template.NamespaceURI);
//get field XML node via XPath
XmlNodefield = doc.Form.XFA.Template.SelectSingleNode("//tpl:subform[@name='form']/tpl:subform/tpl:field[@name='name1']", nsmgr);
//set access attribute of field
XmlAttributeattr = field.OwnerDocument.CreateAttribute("access");
field.Attributes.Append(attr);
attr.Value = "readOnly";
doc.Save("E:\\Ap Data\\March2012\\Xfa_ReadonlyField.pdf");
3) Datasets property allows getting and setting field values, for example below code will set “name1” field value:
Documentdoc = new Document("E:\\Ap Data\\March2012\\test.pdf");
XmlNodedatasets = doc.Form.XFA.Datasets;
//get field data node
XmlNodefieldNode = datasets.SelectSingleNode("//form/name1");
//set field value
fieldNode.InnerText = "Text Field Value";
doc.Save("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Documentdoc1 = new Document("E:\\Ap Data\\March2012\\XFA_Filled.pdf");
Console.WriteLine(doc1.Form.XFA.Datasets.SelectSingleNode("//form/name1").InnerText);
Also, XDP property may be used to access to composite XML (which contains Datasets and Template)
Thank You & Best Regards,
Hi, I have a question about setting values in fields in XFA-based forms. I’ve tested out using the approaches listed above, but didn’t get the results I expected.
Hi Lisa,