FDF and XFDF

This is a code example of iText PDF, discover more.

6th November 2015
admin-marketing

Switch code for this example

ImportXFDF.java
/*
 * This example was written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/30508966/saving-xfdf-as-pdf
 */
package sandbox.acroforms;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.XfdfReader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author iText
 */
public class ImportXFDF {

    public static final String SRC = "resources/pdfs/Requisition_Fillable.pdf";
    public static final String XFDF = "resources/pdfs/data.xfdf";
    public static final String DEST = "results/acroforms/xfdf_merged.pdf";

    public static void main(String[] args) throws DocumentException, IOException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ImportXFDF().fillXfdf(SRC, XFDF, DEST);
    }

    protected void fillXfdf(String SRC, String XFDF, String DEST) throws IOException, DocumentException {
        // We receive the XML bytes
        XfdfReader xfdf = new XfdfReader(new FileInputStream(XFDF));
        // We get the corresponding form
        PdfReader reader = new PdfReader(SRC);
        // We create an OutputStream for the new PDF
        FileOutputStream baos = new FileOutputStream(DEST);
        // Now we create the PDF
        PdfStamper stamper = new PdfStamper(reader, baos);
        // We alter the fields of the existing PDF
        AcroFields fields = stamper.getAcroFields();
        fields.setFields(xfdf);
        // take away all interactivity
        stamper.setFormFlattening(true);
        // close the stamper
        stamper.close();
        reader.close();
    }
}
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