Skip to main content
Skip table of contents

XFA examples

These example were written in answer to questions such as: 

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.

If you need to process XFA documents, we have a related blog post that may be interesting to you:

removexfa

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.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.fields.PdfFormField;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.util.Map;


public class RemoveXFA {

	public static final String DEST = "./target/sandbox/acroforms/remove_xfa.pdf";

	public static final String SRC = "./src/main/resources/pdfs/reportcardinitial.pdf";

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

		new RemoveXFA().manipulatePdf(DEST);
	}

	protected void manipulatePdf(String dest) throws Exception {
		PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
		PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

		// Method removes the XFA stream from the document.
		form.removeXfaForm();

		Map<String, PdfFormField> fields = form.getAllFormFields();

		for (Map.Entry<String, PdfFormField> name: fields.entrySet()) {
			if (name.getKey().indexOf("Total") > 0) {
				name.getValue().setColor(ColorConstants.RED);
			}

			name.getValue().setValue("X");
		}

		pdfDoc.close();
	}
}

C#
using System;
using System.Collections.Generic;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Kernel.Colors;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Acroforms
{
    public class RemoveXFA
    {
        public static readonly String DEST = "results/sandbox/acroforms/remove_xfa.pdf";

        public static readonly String SRC = "../../../resources/pdfs/reportcardinitial.pdf";

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

            new RemoveXFA().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            // Method removes the XFA stream from the document.
            form.RemoveXfaForm();

            IDictionary<String, PdfFormField> fields = form.GetAllFormFields();
            foreach (KeyValuePair<String, PdfFormField> name in fields)
            {
                if (name.Key.IndexOf("Total") > 0)
                {
                    name.Value.SetColor(ColorConstants.RED);
                }

                name.Value.SetValue("X");
            }

            pdfDoc.Close();
        }
    }
}

fillxfa

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.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.xfa.XfaForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.io.FileInputStream;


public class FillXFA {

	public static final String DEST = "./target/sandbox/acroforms/purchase_order_filled.pdf";

	public static final String SRC = "./src/main/resources/pdfs/purchase_order.pdf";

	public static final String XML = "./src/main/resources/xml/data.xml";

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

		new FillXFA().manipulatePdf(DEST);
	}

	protected void manipulatePdf(String dest) throws Exception {
		PdfDocument pdfdoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
		PdfAcroForm form = PdfFormCreator.getAcroForm(pdfdoc, true);

		XfaForm xfa = form.getXfaForm();

		// Method fills this object with XFA data under datasets/data.
		xfa.fillXfaForm(new FileInputStream(XML));
		xfa.write(pdfdoc);

		pdfdoc.close();
	}
}
C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Forms.Xfa;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Acroforms
{
    public class FillXFA
    {
        public static readonly String DEST = "results/sandbox/acroforms/purchase_order_filled.pdf";

        public static readonly String SRC = "../../../resources/pdfs/purchase_order.pdf";
        public static readonly String XML = "../../../resources/xml/data.xml";

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

            new FillXFA().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfdoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfdoc, true);

            XfaForm xfa = form.GetXfaForm();

            // Method fills this object with XFA data under datasets/data.
            xfa.FillXfaForm(new FileStream(XML, FileMode.Open, FileAccess.Read));
            xfa.Write(pdfdoc);

            pdfdoc.Close();
        }
    }
}

fillxfa2

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.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.xfa.XfaForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;

import java.io.File;
import java.io.FileInputStream;


public class FillXFA2 {

	public static final String DEST = "./target/sandbox/acroforms/xfa_example_filled.pdf";

	public static final String SRC = "./src/main/resources/pdfs/xfa_invoice_example.pdf";

	public static final String XML = "./src/main/resources/xml/xfa_example.xml";

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

		new FillXFA2().manipulatePdf(DEST);
	}

	protected void manipulatePdf(String dest) throws Exception {
		PdfReader reader = new PdfReader(SRC);
		PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));
		PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);

		XfaForm xfa = form.getXfaForm();

		// Method fills this object with XFA data under datasets/data.
		xfa.fillXfaForm(new FileInputStream(XML));
		xfa.write(pdfDoc);

		pdfDoc.close();
	}
}
C#
using System;
using System.IO;
using iText.Forms;
using iText.Forms.Fields;
using iText.Forms.Xfa;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Acroforms
{
    public class FillXFA2
    {
        public static readonly String DEST = "results/sandbox/acroforms/xfa_example_filled.pdf";

        public static readonly String SRC = "../../../resources/pdfs/xfa_invoice_example.pdf";
        public static readonly String XML = "../../../resources/xml/xfa_example.xml";

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

            new FillXFA2().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfReader reader = new PdfReader(SRC);
            PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);

            XfaForm xfa = form.GetXfaForm();

            // Method fills this object with XFA data under datasets/data.
            xfa.FillXfaForm(new FileStream(XML, FileMode.Open, FileAccess.Read));
            xfa.Write(pdfDoc);

            pdfDoc.Close();
        }
    }
}

readxfa

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.acroforms;

import com.itextpdf.forms.PdfAcroForm;
import com.itextpdf.forms.fields.PdfFormCreator;
import com.itextpdf.forms.xfa.XfaForm;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;

import java.io.File;
import java.io.FileOutputStream;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;


public class ReadXFA {

	public static final String DEST = "./target/xml/xfa_example.xml";

	public static final String SRC = "./src/main/resources/pdfs/xfa_invoice_example.pdf";

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

		new ReadXFA().manipulatePdf(DEST);
	}

	protected void manipulatePdf(String dest) throws Exception {
		PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
		PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true);
		XfaForm xfa = form.getXfaForm();

		// Get XFA data under datasets/data.
		Node node = xfa.getDatasetsNode();
		NodeList list = node.getChildNodes();

		for (int i = 0; i < list.getLength(); i++) {
			if ("data".equals(list.item(i).getLocalName())) {
				node = list.item(i);
				break;
			}
		}

		try (FileOutputStream os = new FileOutputStream(dest)) {
			Transformer transformer = TransformerFactory.newInstance().newTransformer();
			transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.transform(new DOMSource(node), new StreamResult(os));
		}

		pdfDoc.close();
	}
}
C#
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using iText.Forms;
using iText.Forms.Fields;
using iText.Forms.Xfa;
using iText.Kernel.Pdf;

namespace iText.Samples.Sandbox.Acroforms
{
    public class ReadXFA
    {
        public static readonly String DEST = "results/xml/xfa_example.xml";

        public static readonly String SRC = "../../../resources/pdfs/xfa_invoice_example.pdf";

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

            new ReadXFA().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC));
            PdfAcroForm form = PdfFormCreator.GetAcroForm(pdfDoc, true);
            XfaForm xfa = form.GetXfaForm();

            // Get XFA data under datasets/data.
            XElement node = xfa.GetDatasetsNode();
            IEnumerable<XNode> list = node.Nodes();
            foreach (XNode item in list)
            {
                if (item is XElement && "data".Equals(((XElement) item).Name.LocalName))
                {
                    node = (XElement) item;
                    break;
                }
            }

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(dest, settings);
            node.WriteTo(writer);
            writer.Close();

            pdfDoc.Close();
        }
    }
}

Resources

purchase_order.pdf

reportcardinitial.pdf

xfa_invoice_example.pdf

data.xml
xfa_example.xml

Results

cmp_purchase_order_filled.pdf

cmp_xfa_example_filled.pdf

cmp_remove_xfa.pdf



JavaScript errors detected

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

If this problem persists, please contact our support.