I am trying to fill a XFA pdf form with data automatically using iText 7 in Java. I use Maven with IntelliJ IDEA to setup the environment and import iText libraries. Since it is a XFA pdf the pdf file can only be opened with adobe acrobat reader or similar adobe software.
With iText I have successfully extracted the XML file with all the fields and the data entered in those fields. The code I used to that is:
protected void manipulatePdf() throws Exception {
PdfDocument pdf = new PdfDocument(new PdfReader(SRC));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
XfaForm xfa = form.getXfaForm();
Document doc = xfa.getDomDocument();
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, result);
writer.flush();
System.out.println(writer.toString());
}
The resulting XML file is very long. One thing that I noticed is that if I leave the input Pdf fields empty, the form will not be included in the XML file. However, if I fill all the fields out with dummy data the form and all of the fields that has been filled is included in the XML file.
In the iText documentation they provide an example of how to fill a XFA pdf form here.
The code provided to do exactly what I want is:
public void manipulatePdf2(String src, String xml, String dest)
throws IOException {
PdfReader reader = new PdfReader(src);
PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
XfaForm xfa = form.getXfaForm();
xfa.fillXfaForm(new FileInputStream(xml));
xfa.write(pdfDoc);
pdfDoc.close();
}
The problem is this code doesn't seem to work. I have tried to change the form values in the XML file outputted by the code mentioned in the start of this post. I then enter that XML file as the input for the "xfa.fillXfaForm(new FileInputStream(xml));".
Ive tried to fill both the empty input PDF and the input PDF with the dummy data, and the output will always be a PDF with the same data as the input, so the fields doesn't change. The only difference is that the output PDF will have all input fields disabled, so it's not possible to enter values into the fields anymore.
I will share a zip file including the PDF I want to fill out and the output XML file from the first code snippet provided here. I would be very thankful if anyone here had experience with iText 7 and could help me with solving this issue if it is possible.
The other similar posts here on StackOverflow either use iText 5 with pdfstamp which is not available anymore, or include the same code I've already mentioned here which I can't get to work. The PDF is in Swedish, could it be an encoding issue?
Here is the zip file.
Update
So I thanks to the comment I realized I forgot to use Append Mode in order to not break the Pdf signature. The updated code is as follows:
public void manipulatePdf2() throws IOException {
PdfReader reader = new PdfReader(SRC);
PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(DEST), new StampingProperties().useAppendMode());
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
XfaForm xfa = form.getXfaForm();
xfa.fillXfaForm(new FileInputStream(XML));
xfa.write(pdfDoc);
pdfDoc.close();
}
The problem remains however that when I change the data in the XML file and then use that updated XML file to fillXfaForm the output Pdf remains unchanged. How do you format the XML to update the data in the PDF XfaForm correctly?