Hi
Hi Rose,
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.
-
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 = new Aspose.Pdf.Generator.Pdf(); Document doc = 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");
To get a XFA field value:
Document doc1 = new Document("E:\\Ap Data\\March2012\\XFA_Filled.pdf"); //get XFA field value Console.WriteLine(doc1.Form.XFA["form[0].name1[0]"]);
-
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:
Document doc = new Document("E:\\Ap Data\\March2012\\test.pdf"); XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.Form.XFA.Template.OwnerDocument.NameTable); nsmgr.AddNamespace("tpl", doc.Form.XFA.Template.NamespaceURI); //get field XML node via XPath XmlNode field = doc.Form.XFA.Template.SelectSingleNode("//tpl:subform[@name='form']/tpl:subform/tpl:field[@name='name1']", nsmgr); //set access attribute of field XmlAttribute attr = field.OwnerDocument.CreateAttribute("access"); field.Attributes.Append(attr); attr.Value = "readOnly"; doc.Save("E:\\Ap Data\\March2012\\Xfa_ReadonlyField.pdf");
-
Datasets property allows getting and setting field values, for example below code will set “name1” field value:
Document doc = new Document("E:\\Ap Data\\March2012\\test.pdf"); XmlNode datasets = doc.Form.XFA.Datasets; //get field data node XmlNode fieldNode = datasets.SelectSingleNode("//form/name1"); //set field value fieldNode.InnerText = "Text Field Value"; doc.Save("E:\\Ap Data\\March2012\\XFA_Filled.pdf"); Document doc1 = 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,