Flatten XFA using pdfXFA

2nd March 2017
iText
FlattenXfaDocument.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2019 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
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<String> javascriptEvents;
 
    @Override
    protected void beforeManipulatePdf() {
        LicenseKey.loadLicenseFile(System.getenv("ITEXT7_LICENSEKEY") + "/all-products.xml");
 
        this.javascriptEvents = new ArrayList<>();
 
        this.javascriptEvents.add("click");
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
                .setPdfVersion(XFAFlattenerProperties.PDF_1_7)
                .createXmpMetaData()
                .setTagged()
                .setMetaData(
                        new MetaData()
                            .setAuthor("iText Samples")
                            .setLanguage("EN")
                            .setSubject("Showing off our flattening skills")
                            .setTitle("Flattened XFA"));
 
        XFAFlattener xfaf = new XFAFlattener()
                .setFontSettings(new XFAFontSettings().setEmbedExternalFonts(true))
                .setExtraEventList(this.javascriptEvents)
                .setFlattenerProperties(flattenerProperties)
                .setViewMode(XFAFlattener.ViewMode.SCREEN);
 
 
        xfaf.flatten(new FileInputStream(XFA), new FileOutputStream(dest));
    }
}

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