Get XFA Field Properties using Aspose.PDF for .NET - ArgumentException is thrown

Hi


How can get hold of the field properties for fields in an XFA form? When using an AcroForm, I can use Aspose.Pdf.Facades.Form.GetFieldFacade(fieldName). However, I get an ArgumentException when doing the same in an XFA form. I’d like to get hold of properties like PageNumber, Location, FieldType etc.

Please help.

var form = new Aspose.Pdf.Facades.Form(doc);
foreach (var fieldName in form.FieldNames)
{
var x = form.GetFieldFacade(fieldName);
}

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.


For the template, XDP, and Datasets cases, I discovered that when i re-load the “filled” form and inspect the value of the selected node (the last two lines in the sample code snippets), it does show the value I set, so apparently the data is in the form. However, if I open the filled PDF in Adobe Reader, the field shows up as blank. I’m relatively new to working with PDFs, is there some other step I need to do to actually make the data visible after I set it using one of the XML-based approaches shown above?

I can confirm that if I specifically call out the field name and set a value that way (as shown in the API documentation, http://www.aspose.com/docs/display/pdfnet/Working+with+XFA+Forms) then after a Document.Save when i open the resulting PDF in Adobe Reader I do see the data in the field. I would prefer to work with the XML if I can make that work, however.

thanks,

– Lisa

Hi Lisa,


Thanks for your inquiry. Please check our reply in your another related post. Hopefully it will help you to accomplish the task. If issue persist then please share your PDF form and XML data file. We will look into it and guide you accordingly.

Best Regards,