FDF and XFDF

Tags: fdfxfdfformsfill formiText 5

This example was written in answer to the question How to save an .xfdf file as a .pdf file?

Files: 
/*
 * 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();
    }
}
File nameRaw URLUpdated
ImportXFDF.javaImportXFDF.java2015-11-06 1:09 pm
Resources: 
File nameRaw URLUpdated
Requisition_Fillable.pdfRequisition_Fillable.pdf2015-11-06 1:11 pm
data.xfdfdata.xfdf2015-11-06 1:11 pm
Results: 
File nameRaw URLUpdated
cmp_xfdf_merged.pdfcmp_xfdf_merged.pdf2015-11-06 1:14 pm