如何使用iText填写XFA表格?

时间:2022-11-13 21:08:49

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:

这段代码没有异常,一切似乎都没问题,但是如果我打开填充的pdf,Adobe Reader会说出类似的话:

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.

某些字段填写正确,但我无法编辑它。有些字段是空的。如果我通过单击Adobe Reader中的“导入数据”手动选择xml,表单填写正确,所以我猜xml中没有错误。

1 个解决方案

#1


5  

You are not creating the PdfStamper object correctly. Use:

您没有正确创建PdfStamper对象。使用:

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.

在您的代码中,您没有在追加模式下使用PdfStamper。这意味着iText将重新组织PDF中的不同对象。通常这不是问题。

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:

但是:您的PDF启用了Reader,这意味着您的PDF使用Adobe拥有的私钥进行数字签名。通过重新组织PDF中的对象,该签名被破坏。您已经提到的消息清楚地表明了这一点:

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").

您以不允许的方式更改了文档(请参阅我的书“保留启用Reader的表单的使用权限”一节中的第8.7节)。

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.

为避免破坏签名,您需要在追加模式下使用PdfStamper。现在,iText不会重新组织原始内容,而是保持原始文件的完整性,并在原始文件结束后附加新内容。

#1


5  

You are not creating the PdfStamper object correctly. Use:

您没有正确创建PdfStamper对象。使用:

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.

在您的代码中,您没有在追加模式下使用PdfStamper。这意味着iText将重新组织PDF中的不同对象。通常这不是问题。

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:

但是:您的PDF启用了Reader,这意味着您的PDF使用Adobe拥有的私钥进行数字签名。通过重新组织PDF中的对象,该签名被破坏。您已经提到的消息清楚地表明了这一点:

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").

您以不允许的方式更改了文档(请参阅我的书“保留启用Reader的表单的使用权限”一节中的第8.7节)。

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.

为避免破坏签名,您需要在追加模式下使用PdfStamper。现在,iText不会重新组织原始内容,而是保持原始文件的完整性,并在原始文件结束后附加新内容。