Flatten XFA using pdfXFA

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

2nd March 2017
iText PDF

Switch code for this example

FlattenXfaDocument.java
  1. /*
  2.     This file is part of the iText (R) project.
  3.     Copyright (c) 1998-2019 iText Group NV
  4.     Authors: iText Software.
  5.  
  6.     For more information, please contact iText Software at this address:
  7.     sales@itextpdf.com
  8.  */
  9. package com.itextpdf.samples.sandbox.xfa;
  10.  
  11. import com.itextpdf.licensekey.LicenseKey;
  12. import com.itextpdf.samples.GenericTest;
  13. import com.itextpdf.test.annotations.type.SampleTest;
  14. import com.itextpdf.tool.xml.xtra.xfa.MetaData;
  15. import com.itextpdf.tool.xml.xtra.xfa.XFAFlattener;
  16. import com.itextpdf.tool.xml.xtra.xfa.XFAFlattenerProperties;
  17. import com.itextpdf.tool.xml.xtra.xfa.font.XFAFontSettings;
  18. import org.junit.experimental.categories.Category;
  19.  
  20. import java.io.FileInputStream;
  21. import java.io.FileOutputStream;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24.  
  25. /**
  26.  * @author Michael Demey
  27.  */
  28. @Category( SampleTest.class)
  29. public class FlattenXfaDocument extends GenericTest {
  30.     public static final String XFA = "./src/test/resources/xfa/xfa.pdf";
  31.     public static final String DEST = "./target/test/resources/xfa/flattened.pdf";
  32.  
  33.     private List<String> javascriptEvents;
  34.  
  35.     @Override
  36.     protected void beforeManipulatePdf() {
  37.         LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/all-products.xml");
  38.  
  39.         this.javascriptEvents = new ArrayList<>();
  40.  
  41.         this.javascriptEvents.add("click");
  42.     }
  43.  
  44.     @Override
  45.     protected void manipulatePdf(String dest) throws Exception {
  46.         XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
  47.                 .setPdfVersion(XFAFlattenerProperties.PDF_1_7)
  48.                 .createXmpMetaData()
  49.                 .setTagged()
  50.                 .setMetaData(
  51.                         new MetaData()
  52.                             .setAuthor("iText Samples")
  53.                             .setLanguage("EN")
  54.                             .setSubject("Showing off our flattening skills")
  55.                             .setTitle("Flattened XFA"));
  56.  
  57.         XFAFlattener xfaf = new XFAFlattener()
  58.                 .setFontSettings(new XFAFontSettings().setEmbedExternalFonts(true))
  59.                 .setExtraEventList(this.javascriptEvents)
  60.                 .setFlattenerProperties(flattenerProperties)
  61.                 .setViewMode(XFAFlattener.ViewMode.SCREEN);
  62.  
  63.  
  64.         xfaf.flatten(new FileInputStream(XFA), new FileOutputStream(dest));
  65.     }
  66. }

Resources

Contact

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