问题
I have an XDP and a Dynamic PDF file with dynamic fields.
I am able to fill out the text fields and alike (as shown in the code below).
The issue is that I have a dynamic table field and I am not sure how to populate it. I found many nice tutorials to create a table "from scratch", but here the table is a field already predefined (width, number of columns, etc.)
How could I populate a dynamic table field? Any help will be much appreciated.
PS - Here is the code I am using to populate text fields and alike, based on this article: http://bit.ly/12Xu1QY
string template = "template.pdf";
string new = "new.pdf";
var reader = new PdfReader(template);
var stamper = new PdfStamper(reader, new FileStream(file, FileMode.Create));
AcroFields fields = stamper.AcroFields;
fields.SetField("FIELD_NAME_1", "VALUE1");
fields.SetField("FIELD_NAME_2", "VALUE2");
stamper.Close();
EDIT:
Judging from @Bruno Lowagie answer, I am afraid I didn't use the correct wording. I have a LiveCycle Designer File (XDP) and I save it as a Dynamic PDF Form File (PDF). This is coming from an SAP application. The code above worked nicely for me, so I am understanding it is actually a static PDF form.
Then, the table field is called "FIELD" - I will need a way to populate the rows.
I'm sorry for the confusion and appreciate the help.
回答1:
Special thanks to @Bruno Lowagie for pointing me to the right direction.
Now I will reproduce the solution.
I started out with an XDP file (Adobe LiveCycle file) with XFA forms. I then saved it to a PDF file, and filled out the forms with Acrobat Pro, and finally exported the form data to an XML file.
This is eventually the XML file that I fed the PdfStamper
with, in order to populate the PDF file.
So first I dynamically generated the XML in this way:
var doc = new XDocument(new XDeclaration("1.0", "UTF-8", ""),
new XElement("CUSTOMER",
new XElement("FIRST_NAME", FirstNameTextBox.Text),
new XElement("LAST_NAME", LastNameTextBox.Text)
));
And then I fed it to the PdfStamper
in this way:
var reader = new PdfReader(templateFilename);
var stamper = new PdfStamper(reader, new FileStream(bolFilename, FileMode.Create));
var xml = GenerateXml(); // Above code
var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(xml ?? String.Empty));
stamper.AcroFields.Xfa.FillXfaForm(stream);
stamper.Close();
(Don't forget to put the code within a using statement.)
回答2:
You are mixing up two different technologies.
The article you're referring to fills out a static form. That is: either a PDF containing nothing but AcroForm fields, or a hybrid form consisting of a definition of the form based on AcroForm technology as well as based on the XML Forms Architecture (XFA).
You're talking about a dynamic form, which means you're talking about pure XFA. You need a completely different example: Java / C#
Once you have filled out the form using the fillXfaForm()
method, you can flatten the form using the closed source XFA Worker. See also the download page.
来源:https://stackoverflow.com/questions/16704091/itextsharp-populating-a-dynamic-table-field