I use itextsharpto fill in my pdf files. I have no problem with this. Basically what I am doing is getting the PDF and filling in the fields in memory, and then returning MemoryStreamto display on the web page. All this works with a single PDF document.
What I'm trying to understand now is to merge several PDF files into one MemoryStream. I cannot understand that the documents that I fill out are identical. So, for example, I have List<Person>one that contains 5 people. I want to fill out a PDF for each person and combine them all into one, in memory. I say that I am going to fill out the same type of document for each person.
The problem that I get is that when I try to add a second copy of the same PDF file that will be filled in for the second iteration, it just overwrites the first completed PDF file because it is the same document, so it does not add the second copy for the second Persongenerally.
So basically, if I had 5 people, I would have one page with 5th person data instead of a PDF with 5 pages containing each person’s data, respectively.
Here is the code ...
MemoryStream ms = ms = new MemoryStream();
PdfReader docReader = null;
PdfStamper Stamper = null;
List<Person> persons = new List<Person>() {
new Person("Larry", "David"),
new Person("Dustin", "Byfuglien"),
new Person("Patrick", "Kane"),
new Person("Johnathan", "Toews"),
new Person("Marian", "Hossa")
};
try
{
foreach(var person in persons){
PdfCopyFields Copier = new PdfCopyFields(ms);
Copier.AddDocument(GetReader("Person.pdf"));
Copier.Close();
docReader = new PdfReader(ms.ToArray());
Stamper = new PdfStamper(docReader, ms);
AcroFields Fields = Stamper.AcroFields;
Fields.SetField("FirstName", person.FirstName);
}
}catch(Exception e){
}finally{
if (Stamper != null)
{
Stamper.Close();
}
if (docReader != null)
{
docReader.Close();
}
}
Gabe source
share