XFDF examples
XFDF (XML Forms Data Format) is an XML-based file format for representing form data and annotations in PDFs. It stores information usable by a PDF file, like the values in a document’s form fields.
The following examples show how to use the XFDF functionality in iText Core to create and fill forms:
createxfdf
JAVA
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();
}
}
C#
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
JAVA
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();
}
}
C#
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
JAVA
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();
}
}
C#
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 <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)
{
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();
}
}
}