Skip to main content
Skip table of contents

How to flatten a XFA PDF Form using pdfXFA

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:

JAVA
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)
    form.getField(fieldKey).setValue("X");
form.flattenFields();
pdfDoc.close();

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:

JAVA
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));
xfa.write(pdfdoc);
XFAFlattener xFAFlattener = new XFAFlattener();
xFAFlattener.flatten(new FileInputStream("input.pdf"),new FileOutputStream(getDestFile()));
pdfdoc.close();

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 viewers that do not understand XFA. The actual content of the document is stored as an XML file.

Creating XFA forms required the use of Adobe LiveCycle Designer, however, Adobe LiveCycle was discontinued in March 2018. In addition, XFA forms were deprecated with the publication of the ISO PDF 2.0 specification in 2017.

We developed the pdfXFA add-on for iText Core to assist with existing XFA workflows. However, for new workflows we recommend alternative solutions such as Fluent, which offers dynamic data-driven document generation in a wide range of document formats, including PDF, PDF/A and PDF/UA.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.