This is a series of examples written by Bruno Lowagie in answer to questions that were posted to the iText mailing list.
This example is part of Forms:
Examples explaining how to work with interactive PDF forms.
Keywords: Interactive forms, Interactive forms > XFA, Interactive forms > fill out, Interactive forms > dynamic XFA
- <N>: filled_xfa.pdf
- jars needed: lib/iText.jar
- Resource(s): datasets.xml, dynamic.pdf
This is the full source code of FillDynamicXfa:
/* * This example was written by Bruno Lowagie, author of the book * 'iText in Action' by Manning Publications (ISBN: 1932394796). * You can use this example as inspiration for your own applications. * The following license applies: * http://www.1t3xt.com/about/copyright/index.php?page=MIT */ package questions.forms; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import com.lowagie.text.DocumentException; import com.lowagie.text.pdf.PRStream; import com.lowagie.text.pdf.PdfArray; import com.lowagie.text.pdf.PdfDictionary; import com.lowagie.text.pdf.PdfName; import com.lowagie.text.pdf.PdfReader; import com.lowagie.text.pdf.PdfStamper; public class FillDynamicXfa { public static final String RESOURCE_PDF = "resources/questions/forms/dynamic.pdf"; public static final String RESOURCE_DATA = "resources/questions/forms/datasets.xml"; public static final String RESULT = "results/questions/forms/filled_xfa.pdf"; public static void main(String[] args) { try { PdfReader reader = new PdfReader(RESOURCE_PDF); File file = new File(RESOURCE_DATA); byte[] data = new byte[(int)file.length()]; FileInputStream is = new FileInputStream(file); int offset = 0; int numRead = 0; int datalength = data.length; while (offset < datalength && (numRead=is.read(data, offset, datalength - offset)) >= 0) { offset += numRead; } PdfDictionary root = reader.getCatalog(); PdfDictionary acroform = root.getAsDict(PdfName.ACROFORM); PdfArray xfa = acroform.getAsArray(PdfName.XFA); for (int i = 0; i < xfa.size(); i += 2) { if ("datasets".equals(xfa.getAsString(i).toString())) { PRStream s = (PRStream)xfa.getAsStream(i + 1); s.setData(data); } } PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT)); stamper.close(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } } }
Assuming that your working directory has a subdirectory bin (where the class files
will be written) and src/questions
(with the *.java source files), you can compile the example using this command line(s):
javac -d bin -cp "bin;lib/iText.jar" src/questions/forms/FillDynamicXfa.java
Once compiled, you can execute the code like this:
java -cp "bin;lib/iText.jar" questions.forms.FillDynamicXfa
All examples were tested on Windows Vista and Fedora Linux. Please don't use these examples if you don't understand the source code. Read the documentation (the book, tutorial,...) before asking questions. If you still have a question, please subscribe to the mailing list and refer to this page by its URL.
