Skip to main content
Skip table of contents

XFDF examples

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


createxfdf

createxfdf

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.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/main/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();
    }
}


CreateXfdf

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.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/main/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();
    }
}
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.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/main/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

fillformxfdf

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.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/main/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

fillformlistxfdf 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.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/main/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 <value> tag with text
    // contents is found, it is considered to be the value of the field. All other <value> 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();
    }
}


Resources

cmp_createXfdf.xfdf

cmp_setFields.pdf

cmp_listInSetField.pdf

JavaScript errors detected

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

If this problem persists, please contact our support.