如何在iTextSharp中以静态XFA格式设置XFA数据并将其保存? [英] How can I set XFA data in a static XFA form in iTextSharp and get it to save?

查看:165
本文介绍了如何在iTextSharp中以静态XFA格式设置XFA数据并将其保存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iText / iTextSharp(通过NuGet的iTextSharp 5.3.3)中使用XFA Forms时遇到了一个非常奇怪的问题。我正在尝试填写静态XFA样式表单,但是我的更改没有进行。

I'm having a very strange issue with XFA Forms in iText / iTextSharp (iTextSharp 5.3.3 via NuGet). I am trying to fill out a static XFA styled form, however my changes are not taking.

我有两个版本的iText in Action并且一直在咨询第二版以及来自本书的iTextSharp代码样本转换。

I have both editions of iText in Action and have been consulting the second edition as well as the iTextSharp code sample conversions from the book.

背景:我有一个XFA表单,我可以在我的计算机上使用Adobe Acrobat手动填写。使用iTextSharp我可以读取Xfa XML数据是什么,并查看数据的结构。我基本上试图用iText模仿它。

Background: I have an XFA Form that I can fill out manually using Adobe Acrobat on my computer. Using iTextSharp I can read what the Xfa XML data is and see the structure of the data. I am essentially trying to mimic that with iText.

当我添加数据并保存在Acrobat中时数据是什么样的(注意:这只是数据集的特定部分)

What the data looks like when I add data and save in Acrobat (note: this is only the specific section for datasets)

这是我试图读取的XML文件来替换现有数据(注意:这是该文件的整个上下文):

Here is the XML file I am trying to read in to replace the existing data (note: this is the entire contexts of that file):

但是,当我将路径传递给替换XML文件并尝试设置数据时,创建的新文件(替换数据的原始文件的副本)没有任何错误被抛出,但数据没有被更新。我可以看到新文件已创建,我可以打开它,但文件中没有数据。

However, when I pass the path to the replacement XML File in and try to set the data, the new file created (a copy of the original with the data replaced) without any errors being thrown, but the data is not being updated. I can see that the new file is created and I can open it, but there is no data in the file.

这是用于替换数据的代码或第一次填充,这是 http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter08/XfaMovie.cs

Here is the code being utilized to replace the data or populate for the first time, which is a variation of http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter08/XfaMovie.cs

public void Generate(string sourceFilePath, string destinationtFilePath, string replacementXmlFilePath)
    {
        PdfReader pdfReader = new PdfReader(sourceFilePath);
        using (MemoryStream ms = new MemoryStream())
        {
            using (PdfStamper stamper = new PdfStamper(pdfReader, ms))
            {
                XfaForm xfaForm = new XfaForm(pdfReader);
                XmlDocument doc = new XmlDocument();
                doc.Load(replacementXmlFilePath);
                xfaForm.DomDocument = doc;
                xfaForm.Changed = true;
                XfaForm.SetXfa(xfaForm, stamper.Reader, stamper.Writer);
            }

            var bytes = ms.ToArray();
            File.WriteAllBytes(destinationtFilePath, bytes);
        }
    }

非常感谢任何帮助。

推荐答案

我赞成你的回答,因为它没有错误(我很高兴我对这个演示的引用让你再看看你的代码),但现在我再看看你的原始代码,我认为最好使用书籍示例

I upvoted your answer, because it's not incorrect (I'm happy my reference to the demo led you to have another look at your code), but now that I have a second look at your original code, I think it's better to use the book example:

public byte[] ManipulatePdf(String src, String xml) {
  PdfReader reader = new PdfReader(src);
  using (MemoryStream ms = new MemoryStream()) {
    using (PdfStamper stamper = new PdfStamper(reader, ms)) {
      AcroFields form = stamper.AcroFields;
      XfaForm xfa = form.Xfa;
      xfa.FillXfaForm(XmlReader.Create(new StringReader(xml)));
    }
    return ms.ToArray();
  }
}

如您所见,没有必要更换整个XFA XML。如果你使用 FillXfaForm 方法,那么数据就足够了。

As you can see, it's not necessary to replace the whole XFA XML. If you use the FillXfaForm method, the data is sufficient.

注意:对于C#版本的示例,请参阅 http://tinyurl.com/iiacsCH08 (将08更改为01到16之间的数字作为示例其他章节。)

Note: for the C# version of the examples, see http://tinyurl.com/iiacsCH08 (change the 08 into a number from 01 to 16 for the examples of the other chapters).

这篇关于如何在iTextSharp中以静态XFA格式设置XFA数据并将其保存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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