How to get a list of fields in an XFA form?

I am trying to get a simple list of all the fields in my XFA form. I am using this code:

private void ListFieldNames() { string pdfTemplate = @"C:\Projects\iTextSharp\SReport.pdf"; MemoryStream m = new MemoryStream(); // title the form this.Text += " - " + pdfTemplate; // create a new PDF reader based on the PDF template document PdfReader pdfReader = new PdfReader(pdfTemplate); PdfStamper pdfStamper = new PdfStamper(pdfReader, m); AcroFields formFields = pdfStamper.AcroFields; AcroFields form = pdfReader.AcroFields; XfaForm xfa = form.Xfa; StringBuilder sb = new StringBuilder(); sb.Append(xfa.XfaPresent ? "XFA form" : "AcroForm"); sb.Append(Environment.NewLine); foreach (string key in form.Fields.Keys) { sb.Append(key); sb.Append(Environment.NewLine); txtFields.Text = sb.ToString(); } txtFields.Text = sb.ToString(); } 

But all I get is an XFA form, not any fields. Any idea what I'm doing wrong?

Thank you in advance

+6
source share
1 answer

You took the sample code from chapter 8 of my book, iText in Action. The result of this code example matches what I wrote on page 273:

Running Listing 8.18 with this form as a resource will give you the following result:

AcroForm

If your question is some idea, what am I doing wrong? then the answer is simple: you stopped reading on page 270, or you used a sample code without reading the accompanying documentation. How to fix it? Read the documentation!

If you have a question, why am I not getting field information? (this is not your question, but let's say it is), the answer is this: you use the code to retrieve the AcroForm fields, but your form does not contain such fields. Your form is a pure XFA form, which means that all field information is stored only as XML and XML!

Suppose you now want to know: how can I extract this XML? then you should go to the place where you found the example that you copy / paste.

It can be here: http://itextpdf.com/examples/iia.php?id=164

Or maybe here: http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter08/XfaMovie.cs

Or even here: http://kuujinbo.info/iTextInAction2Ed/index.aspx?ch=Chapter08&ex=XfaMovie

This piece of code will return the full XFA stream:

 public string ReadXfa(PdfReader reader) { XfaForm xfa = new XfaForm(reader); XmlDocument doc = xfa.DomDocument; reader.Close(); if (!string.IsNullOrEmpty(doc.DocumentElement.NamespaceURI)) { doc.DocumentElement.SetAttribute("xmlns", ""); XmlDocument new_doc = new XmlDocument(); new_doc.LoadXml(doc.OuterXml); doc = new_doc; } var sb = new StringBuilder(4000); var Xsettings = new XmlWriterSettings() {Indent = true}; using (var writer = XmlWriter.Create(sb, Xsettings)) { doc.WriteTo(writer); } return sb.ToString(); } 

Now find the <xfa:datasets> ; it will have subtag <xfa:data> (possibly empty if the form is empty) and subtag <dd:dataDescription> . Inside the dataDescription tag dataDescription you will find something similar to XSD. This is what you need to know about what the fields in the form are.

I could guess the questions, for example: How to fill out such a form? Using the fillXfaForm() method; How can I smooth this shape? Using XFA Worker (which is a closed source library written on top of iTextSharp), but let these questions be kept for another thread; -)

+11
source

All Articles