Flatten XFA using pdfXFA

This is a code example of iText PDF, discover more.

2nd March 2017
iText PDF

Switch code for this example

    This file is part of the iText (R) project.
    Copyright (c) 1998-2018 iText Group NV
    Authors: iText Software.

    For more information, please contact iText Software at this address:
package com.itextpdf.samples.sandbox.xfa;

import com.itextpdf.licensekey.LicenseKey;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import com.itextpdf.tool.xml.xtra.xfa.MetaData;
import com.itextpdf.tool.xml.xtra.xfa.XFAFlattener;
import com.itextpdf.tool.xml.xtra.xfa.XFAFlattenerProperties;
import com.itextpdf.tool.xml.xtra.xfa.font.XFAFontSettings;
import org.junit.experimental.categories.Category;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

 * @author Michael Demey
@Category( SampleTest.class)
public class FlattenXfaDocument extends GenericTest {
    public static final String XFA = "./src/test/resources/xfa/xfa.pdf";
    public static final String DEST = "./target/test/resources/xfa/flattened.pdf";

    private List javascriptEvents;

    protected void beforeManipulatePdf() {
        LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/all-products.xml");

        this.javascriptEvents = new ArrayList<>();


    protected void manipulatePdf(String dest) throws Exception {
        XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
                        new MetaData()
                            .setAuthor("iText Samples")
                            .setSubject("Showing off our flattening skills")
                            .setTitle("Flattened XFA"));

        XFAFlattener xfaf = new XFAFlattener()
                .setFontSettings(new XFAFontSettings().setEmbedExternalFonts(true))

        xfaf.flatten(new FileInputStream(XFA), new FileOutputStream(dest));



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