How to fill XFA form using iText without breaking usage rights?
If I choose the XML manually by clicking 'Import data' from Adobe Reader, form is filled properly, so I guess there is no error in the XML.
This is my code:
using (FileStream pdf = new FileStream("C:/test.pdf", FileMode.Open))
using (FileStream xml = new FileStream("C:/test.xml", FileMode.Open))
using (FileStream filledPdf = new FileStream("C:/test_f.pdf", FileMode.Create))
{
PdfReader pdfReader = new PdfReader(pdf);
PdfStamper stamper = new PdfStamper(pdfReader, filledPdf);
stamper.AcroFields.Xfa.FillXfaForm(xml);
stamper.Close();
pdfReader.Close();
}
This code throws no exception and everything seems to be OK, but if I open filled pdf, Adobe Reader says something like this:
This document enabled extended features. This document was changed since it was created and using extended features isn't possible anymore.
If I choose the XML manually by clicking 'Import data' from Adobe Reader, form is filled properly, so I guess there is no error in the XML.
Posted on StackOverflow on Oct 29, 2014 by paldir
To avoid breaking the signature you should use the following code in iText 7 and later versions:
protected void manipulatePdf(String src, String dest) throws Exception {
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();
}
However: your PDF is Reader-enabled, which means that your PDF is digitally signed using a private key owned by Adobe. By reorganizing the objects inside the PDF, that signature is broken. This is made clear by the message you already mentioned.
To avoid breaking the signature, you need to use PdfStamper
in append mode. Instead of reorganizing the original content, iText will now keep the original file intact and append new content after the end of the original file. Note that PdfStamper
does not exist in iText 7 and later versions, so you have to use StampingProperties
in the PdfDocument
constructor.
Click How to fill XFA form using iText without breaking usage rights? if you want to see how to answer this question in iText 5.
Creating XFA forms required the use of Adobe LiveCycle Designer, however, Adobe LiveCycle was discontinued in March 2018. In addition, XFA forms were deprecated with the publication of the ISO PDF 2.0 specification in 2017.
We developed the pdfXFA add-on for iText Core to assist with existing XFA workflows. However, for new workflows we recommend alternative solutions such as Fluent, which offers dynamic data-driven document generation in a wide range of document formats, including PDF, PDF/A and PDF/UA.
If you need to process XFA documents, we have a related blog post that may be interesting to you: