iText pdf library
Website search

FDF and XFDF

iText 5 is no longer being developed, except for security fixes. Click the following link to see an updated example for iText 7.

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

ImportXFDF.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
48
49
50
51
/*
 * 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();
    }
}

Tags
xfdf PDF

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