Skip to main content
Skip table of contents

Flatten XFA using pdfXFA


flattenxfadocument

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

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

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 java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.List;

public class FlattenXfaDocument {
    public static final String DEST = "./target/sandbox/xfa/flattened.pdf";

    public static final String XFA = "./src/main/resources/xfa/xfa.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new FlattenXfaDocument().manipulatePdf(DEST);
    }

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

        XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
                .setPdfVersion(XFAFlattenerProperties.PDF_1_7)
                .createXmpMetaData()
                .setTagged()
                .setMetaData(metaData);

        List<String> javascriptEvents = Arrays.asList("click");
        XFAFlattener xfaFlattener = new XFAFlattener()
                .setExtraEventList(javascriptEvents)
                .setFlattenerProperties(flattenerProperties)
                .setViewMode(XFAFlattener.ViewMode.SCREEN);

        xfaFlattener.flatten(new FileInputStream(XFA), new FileOutputStream(dest));
    }
}

C#

C#
using System;
using System.Collections.Generic;
using System.IO;
using iTextSharp.tool.xml.xtra.xfa;

namespace iText.Samples.Sandbox.Xfa
{
    public class FlattenXfaDocument
    {
        public static readonly String DEST = "results/sandbox/xfa/flattened.pdf";

        public static readonly String XFA = "../../../resources/xfa/xfa.pdf";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new FlattenXfaDocument().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            MetaData metaData = new MetaData()
                .SetAuthor("iText Samples")
                .SetLanguage("EN")
                .SetSubject("Showing off our flattening skills")
                .SetTitle("Flattened XFA");

            XFAFlattenerProperties flattenerProperties = new XFAFlattenerProperties()
                .SetPdfVersion(XFAFlattenerProperties.PDF_1_7)
                .CreateXmpMetaData()
                .SetTagged()
                .SetMetaData(metaData);

            List<String> javascriptEvents = new List<string>()
            {
                "click"
            };

            XFAFlattener xfaFlattener = new XFAFlattener()
                .SetExtraEventList(javascriptEvents)
                .SetFlattenerProperties(flattenerProperties)
                .SetViewMode(XFAFlattener.ViewMode.SCREEN);

            xfaFlattener.Flatten(new FileStream(XFA, FileMode.Open), new FileStream(dest, FileMode.Create));
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.