如何使用iTextSharp正确填写XFA表单数据,以便在Acrobat XI中编辑和保存结果 [英] How to correctly fill in XFA form data using iTextSharp to allow editing and saving result in Acrobat XI

查看:420
本文介绍了如何使用iTextSharp正确填写XFA表单数据,以便在Acrobat XI中编辑和保存结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我正在使用iTextSharp填充pdf表单。

I have an application that I'm using the populate a pdf form using iTextSharp.

    /// <summary>
    /// Imports XFA Data into a new PDF file.
    /// </summary>
    /// <param name="pdfTemplate">A PDF File with an unpopulated form.</param>
    /// <param name="xmlFormData">XFA form data in XML format.</param>
    /// <returns>a memorystream containing the new PDF file.</returns>
    public static void XFAImport(System.IO.Stream pdfTemplate, System.IO.Stream xmlFormData, System.IO.Stream outputStream)
    {
        using (iTextSharp.text.pdf.PdfReader reader = new iTextSharp.text.pdf.PdfReader(pdfTemplate))
        {
            using (iTextSharp.text.pdf.PdfStamper stamper = new iTextSharp.text.pdf.PdfStamper(reader, outputStream))
            {
                stamper.Writer.CloseStream = false;
                stamper.AcroFields.Xfa.FillXfaForm(xmlFormData);
            }
        }
    }

以上代码需要pdf未填写的表单和xml数据并写入outputStream,然后将其保存到文件中。

The above code takes a pdf form that is not filled out and xml data and writes to the outputStream which is then saved to a file.

在Adobe中打开文件时,您会看到表单数据填写正确。但是,如果您从Acrobat XI保存该文件,然后重新打开它,则导入的数据将不再可见。

When you open the file in Adobe you see the form data filled out correctly. However if you then save that file from Acrobat XI, and then reopen it, the data that you imported is no longer visible.

我不相信问题出在我正在导入的XML,因为如果不是使用iTextShart,而是使用Acrobat XI的工具/表单/更多表单选项/导入数据。生成的文件可以保存并重新打开。

I don't believe the problem is with the XML that I am importing, because if instead of using iTextShart, I instead use Acrobat XI's "tools/form/more form options/import data". The resulting file is able to be saved and reopened correctly.

我的问题是:

我使用的是PdfStamper正确上面?

Am I using the PdfStamper correctly above?

我是否可以采取任何措施使结果文件正确保存?

Are there any steps I can take to make the resulting file save correctly?

PS。我注意到,在使用Acrobat XI重新保存输出pdf文件后,生成的文件与原始文件大致相同,但最后还会插入额外的11k数据。

PS. I've noticed that after resaving the output pdf file using Acrobat XI that the resulting file is mostly identical to the original, however it has an additional 11k of data inserted at the end.

输出pdf文件的结尾:

end of the output pdf file:

trailer
<</Size 51/Root 14 0 R/Info 3 0 R/ID [<56549fdaf0c5ab4e9321d77f406e6455><5b60738018e0cdac94c6d1b924fc8bed>]>>
%iText-5.4.4
startxref
529008
%%EOF

在Acrobat XI中保存后,会添加更多数据:

After saving in Acrobat XI is has more data added to it:

trailer
<</Size 51/Root 14 0 R/Info 3 0 R/ID [<56549fdaf0c5ab4e9321d77f406e6455>         <5b60738018e0cdac94c6d1b924fc8bed>]>>
%iText-5.4.4
startxref
529008
%%EOF
3 0 obj
<</CreationDate(D:20100120124725-05'00')/Creator(Adobe LiveCycle Designer ES 8.2)/ModDate(D:20140221145558-06'00')/Producer(Adobe LiveCycle Designer ES 8.2; modified using iTextSharp’ 5.4.4 ©2000-2013 1T3XT BVBA \(AGPL-version\))>>
endobj
4 0 obj
<</Length 3261/Subtype/XML/Type/Metadata>>stream
<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>

/*more data excluded*/


推荐答案

不,你没有正确使用 PdfStamper

使用数字签名实现读者启用(它需要Adobe提供的私钥。当您使用标准方式填写表单时,您将打破该签名。您需要在附加模式中填写表单。

Reader enabling is achieved using a digital signature (it requires a private key from Adobe). When you fill out the form using the "standard way", you break that signature. You need to fill out the form in append mode.

我在本书的第8.7.2节中对此进行了解释,标题为填充使用iText的启用阅读器的表格(它打败了我为什么没有人在提问之前阅读文档;人们想知道为什么一个人甚至困扰写一本书)。您可以在此处找到此部分附带的示例: ReaderEnabledForm

I've explained this in section 8.7.2 of my book, entitled "Filling out Reader-enabled forms using iText" (it beats me why nobody ever reads the documentation before asking a question; one wonders why one even bothers writing a book). You can find the example that comes with this section here: ReaderEnabledForm

您可以在相应章节在SourceForge 上找到C#版本:

You can find the C# version in of the corresponding chapter on SourceForge:

底线:你需要更换

new iTextSharp.text.pdf.PdfStamper(reader, outputStream)

with

new iTextSharp.text.pdf.PdfStamper(reader, outputStream, '\0', true)

在这种情况下,您的更改将在 %% EOF 标记后附加 Adobe应用的数字签名不会被破坏。

In this case, your changes will be appended after the %%EOF marker and the digital signature applied by Adobe won't be broken.

这篇关于如何使用iTextSharp正确填写XFA表单数据,以便在Acrobat XI中编辑和保存结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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