How to fill out an XFA form using iText?

the 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.unethicalreading = true; PdfReader pdfReader = new PdfReader(pdf); PdfStamper stamper = new PdfStamper(pdfReader, filledPdf); stamper.AcroFields.Xfa.FillXfaForm(xml); stamper.Close(); pdfReader.Close(); } 

This code does not throw an exception, and everything looks fine, but if I open the completed PDF, Adobe Reader will say something like this:

This document has included advanced features. This document has been modified since its inception, and the use of advanced features is no longer possible.

Some fields are filled out correctly, but I cannot edit them. Some fields are empty. If I manually select xml by clicking "Import Data" from Adobe Reader, the form fills in correctly, so I think there is no error in xml.

+4
xml pdf itext xfa
source share
1 answer

You are not creating the PdfStamper object correctly. Using:

 PdfStamper stamper = new PdfStamper(pdfReader, filledPdf, '\0', true) 

In your code, you are not using PdfStamper in add mode. This means that iText will reorganize various objects in your PDF file. This is usually not a problem.

However: your PDF file is included using Reader, which means that your PDF code is digitally signed using a private key owned by Adobe. By reorganizing objects inside a PDF, this signature is broken. This is stated in the message that you already mentioned:

This document has included advanced features. This document has been modified since it was created and using advanced features is no longer possible.

You have modified the document in a way that is not allowed (see section 8.7 of my book entitled "Retention of Use Rights Forms Supported by the Reader").

In order not to violate the signature, you need to use PdfStamper in add mode. Instead of reorganizing the original content, iText will now save the original file unchanged and add new content after the end of the original file.

+5
source share

All Articles