Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
560 views
in Technique[技术] by (71.8m points)

itext - How can I set XFA data in a static XFA form in iTextSharp and get it to save?

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.

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.

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.

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

enter image description here

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):

enter image description here

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.

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

Any help would be very much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...