如何使用iText填写XFA表格? [英] How to fill XFA form using iText?

查看:215
本文介绍了如何使用iText填写XFA表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

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();
}

此代码不会抛出任何异常,一切似乎都没问题,但如果我打开填写pdf,Adobe Reader说出类似的内容:

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.

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

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.

推荐答案

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

You are not creating the PdfStamper object correctly. Use:

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

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

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.

但是:您的PDF启用了Reader,这意味着您的PDF使用Adobe拥有的私钥进行数字签名。通过重新组织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:


本文档启用了扩展功能。该文档自创建以来已更改为
,并且不再使用扩展功能

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

您以不允许的方式更改了文档(请参阅我的书籍第8.7节,题为保留使用权)启用阅读器的表单)。

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

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

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.

这篇关于如何使用iText填写XFA表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆