iText pdf library
Website search

How to flatten a XFA PDF Form using iTextSharp?

I assume I need to flatten a XFA form in order to display properly on the UI of an application that uses Nuance's CSDK. When I process it now I get a generic message "Please Wait.... If this message is not eventually replaced."

Posted on StackOverflow on Nov 21, 2014 by Chuck

You didn't insert a code sample to show how you are currently flattening the XFA form. I assume your code looks like this:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
Map fields = form.getFormFields();
Set fieldKeys = fields.keySet();
for (String fieldKey : fieldKeys)

This code will work when trying to flatten forms based on AcroForm technology. For forms based on the XML Forms Architecture (XFA) you should use the following snippet:

PdfDocument pdfdoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfdoc, true);
XfaForm xfa = form.getXfaForm();
xfa.fillXfaForm(new FileInputStream(xml));

Where src, dest and xml are String paths to your documents.

A dynamic XFA form usually consists of a single page PDF (the one with the "Please wait..." message) that is shown in viewer that do not understand XFA. The actual content of the document is stored as an XML file.

The process of flattening an XFA form to an ordinary PDF requires that you parse the XML and convert the XML syntax into PDF syntax. This can be done using XFA Worker, which is s a separate product. Click this link to see the solution.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now