How to fill XFA form using iText?

强颜欢笑 提交于 2019-12-19 09:09:42

问题


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 throws no exception and everything seems to be ok, but if I open filled pdf, Adobe Reader says something like that:

This document enabled extended features. This document was changed since it was created and using extended features isn't possible anymore.

Some fields are filled properly, but I can't edit it. Some fields are empty. If I choose xml manually by clicking 'Import data' from Adobe Reader, form is filled properly, so I guess there is no error in xml.


回答1:


You are not creating the PdfStamper object correctly. Use:

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

In your code, you are not using PdfStamper in append mode. This means that iText will reorganize the different objects in your PDF. Usually that isn't a problem.

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:

This document enabled extended features. This document was changed since it was created and using extended features isn't possible anymore.

You changed the document in a way that isn't allowed (see section 8.7 of my book entitled "Preserving the usage rights of Reader-enabled forms").

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.



来源:https://stackoverflow.com/questions/26629498/how-to-fill-xfa-form-using-itext

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!