iText pdf library
Website search

XFDF examples

The following examples show how to use the XFDF functionality in iText 7 to create and fill forms:

CreateXfdf.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/*
 * Example written by Bruno Lowagie in answer to
 * http://stackoverflow.com/questions/36523371
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.xfdf.XfdfObject;
import com.itextpdf.forms.xfdf.XfdfObjectFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
 
import java.io.File;
import java.io.FileInputStream;
 
public class CreateXfdf {
    public static final String sourceFolder = "./src/test/resources/pdfs/";
    public static final String DEST = "./target/sandbox/acroforms/createXfdf.xfdf";
 
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new CreateXfdf().createXfdf(DEST);
    }
 
    // Currently iText xfdf implementation works in the following way:
    // data from Pdf form could be received as file with the XFDF file extension.
    public void createXfdf(String dest) throws Exception {
        String pdfDocumentName = "createXfdf.pdf";
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(new FileInputStream
                (sourceFolder + pdfDocumentName)));
 
        XfdfObjectFactory factory = new XfdfObjectFactory();
        XfdfObject xfdfObject = factory.createXfdfObject(pdfDocument, pdfDocumentName);
        xfdfObject.writeToFile(dest);
 
        pdfDocument.close();
    }
}

FillFormXfdf.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.xfdf.XfdfObject;
import com.itextpdf.forms.xfdf.XfdfObjectFactory;
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;
import java.io.FileOutputStream;
 
 
public class FillFormXfdf {
    public static final String sourceFolder = "./src/test/resources/pdfs/";
    public static final String DEST = "./target/sandbox/acroforms/setFields.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillFormXfdf().setFields(DEST);
    }
 
    // Currently iText xfdf implementation works in the following way:
    // the XFDF file is used to insert data from it directly into the PDF.
    public void setFields(String dest) throws Exception {
        String pdfForm = sourceFolder + "simpleRegistrationForm.pdf";
        String xfdf = sourceFolder + "register.xfdf";
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(new FileInputStream(pdfForm)),
                new PdfWriter(new FileOutputStream(dest)));
        XfdfObjectFactory factory = new XfdfObjectFactory();
        XfdfObject xfdfObject = factory.createXfdfObject(new FileInputStream(xfdf));
        xfdfObject.mergeToPdf(pdfDocument, pdfForm);
        pdfDocument.close();
    }
}

FillFormListXfdf.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
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.acroforms;
 
import com.itextpdf.forms.xfdf.XfdfObject;
import com.itextpdf.forms.xfdf.XfdfObjectFactory;
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;
import java.io.FileOutputStream;
 
 
public class FillFormListXfdf {
    public static final String sourceFolder = "./src/test/resources/pdfs/";
    public static final String DEST = "./target/sandbox/acroforms/listInSetField.pdf";
 
    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new FillFormListXfdf().listInSetField(DEST);
    }
 
    // Currently iText xfdf implementation works with the fields in the following way: when the  tag with text
    // contents is found, it is considered to be the value of the field. All other  tags are ignored.
    public void listInSetField(String dest) throws Exception {
        String pdfForm = sourceFolder + "simpleRegistrationForm.pdf";
        String xfdf = sourceFolder + "list_register.xfdf";
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(new FileInputStream(pdfForm)),
                new PdfWriter(new FileOutputStream(dest)));
        XfdfObjectFactory factory = new XfdfObjectFactory();
        XfdfObject xfdfObject = factory.createXfdfObject(new FileInputStream(xfdf));
        xfdfObject.mergeToPdf(pdfDocument, pdfForm);
        pdfDocument.close();
    }
}

CreateXfdf.cs

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
using System;
using System.IO;
using iText.Forms.Xfdf;
using iText.Kernel.Pdf;
 
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class CreateXfdf
    {
        public static readonly String sourceFolder = "../../resources/pdfs/";
        
        public static readonly String DEST = "results/sandbox/acroforms/createXfdf.xfdf";
 
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new CreateXfdf().createXfdf(DEST);
        }
 
        // Currently iText xfdf implementation works in the following way:
        // data from Pdf form could be received as file with the XFDF file extension.
        public void createXfdf(String dest)
        {
 
            String pdfDocumentName = "createXfdf.pdf";
 
            PdfDocument pdfDocument = new PdfDocument(new PdfReader(new FileStream
                (sourceFolder + pdfDocumentName, FileMode.Open, FileAccess.Read)));
 
            XfdfObjectFactory factory = new XfdfObjectFactory();
            XfdfObject xfdfObject = factory.CreateXfdfObject(pdfDocument, pdfDocumentName);
            xfdfObject.WriteToFile(dest);
 
            pdfDocument.Close();
        }
    }
}

FillFormXfdf.cs

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
using System;
using System.IO;
using iText.Forms.Xfdf;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class FillFormXfdf
    {
        public static readonly String sourceFolder = "../../resources/pdfs/";
 
        public static readonly String DEST = "results/sandbox/acroforms/setFields.pdf";
 
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new FillFormXfdf().setFields(DEST);
        }
 
        // Currently iText xfdf implementation works in the following way:
        // the XFDF file is used to insert data from it directly into the PDF.
        public void setFields(String dest)
        {
            String pdfForm = sourceFolder + "simpleRegistrationForm.pdf";
            String xfdf = sourceFolder + "register.xfdf";
            PdfDocument pdfDocument = new PdfDocument(
                new PdfReader(new FileStream(pdfForm, FileMode.Open, FileAccess.Read)),
                new PdfWriter(new FileStream(dest, FileMode.Create)));
            XfdfObjectFactory factory = new XfdfObjectFactory();
            XfdfObject xfdfObject = factory.CreateXfdfObject(new FileStream(xfdf, FileMode.Open, FileAccess.Read));
            xfdfObject.MergeToPdf(pdfDocument, pdfForm);
            pdfDocument.Close();
        }
    }
}

FillFormListXfdf.cs

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
using System;
using System.IO;
using iText.Forms.Xfdf;
using iText.Kernel.Pdf;
 
namespace iText.Samples.Sandbox.Acroforms
{
    public class FillFormListXfdf
    {
        public static readonly String sourceFolder = "../../resources/pdfs/";
 
        public static readonly String DEST = "results/sandbox/acroforms/listInSetField.pdf";
 
 
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
 
            new FillFormListXfdf().listInSetField(DEST);
        }
 
        // Currently iText xfdf implementation works with the fields in the following way: when the  tag with text
        // contents is found, it is considered to be the value of the field. All other  tags are ignored.
        public void listInSetField(String dest)
        {
            String pdfForm = sourceFolder + "simpleRegistrationForm.pdf";
            String xfdf = sourceFolder + "list_register.xfdf";
            PdfDocument pdfDocument = new PdfDocument(
                new PdfReader(new FileStream(pdfForm, FileMode.Open, FileAccess.Read)),
                new PdfWriter(new FileStream(dest, FileMode.Create)));
            XfdfObjectFactory factory = new XfdfObjectFactory();
            XfdfObject xfdfObject = factory.CreateXfdfObject(new FileStream(xfdf, FileMode.Open, FileAccess.Read));
            xfdfObject.MergeToPdf(pdfDocument, pdfForm);
            pdfDocument.Close();
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.



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