Filling multiple pdf files

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
{
   // Iterate thru all persons and populate a PDF for each
   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){
  // handle error
}finally{
   if (Stamper != null)
   {
      Stamper.Close();
   }
   if (docReader != null)
   {
      docReader.Close();
   }
}
+5
source share
3 answers

I created a working solution, I hope this helps someone along the way.

Create a PopulatePDF() method that takes an object Person and returns byte[] :

private byte[] PopulatePersonPDF(Person obj)
{
   MemoryStream ms = new MemoryStream();
   PdfStamper Stamper = null;

   try
   {
      PdfCopyFields Copier = new PdfCopyFields(ms);
      Copier.AddDocument(GetReader("Person.pdf"));
      Copier.Close();

      PdfReader docReader = new PdfReader(ms.ToArray());
      ms = new MemoryStream();
      Stamper = new PdfStamper(docReader, ms);
      AcroFields Fields = Stamper.AcroFields;
      Fields.SetField("FirstName", obj.FirstName);
   }
   finally
   {
      if (Stamper != null)
      {
         Stamper.Close();
      }
   }
   return ms.ToArray();
}

MergePDFs() , MemoryStream :

private MemoryStream MergePDFs(List<byte[]> pdfs)
{
   MemoryStream ms = new MemoryStream();
   PdfCopyFields Copier = new PdfCopyFields(ms);

   foreach (var pdf in pdfs)
      Copier.AddDocument(new PdfReader(pdf));
   Copier.Close();
   return ms;
}

:

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

List<byte[]> pdfs = new List<byte[]>();

foreach(var person in persons)
   pdfs.Add(PopulatePersonPDF(person));

MemoryStream ms = MergePDFs(pdfs);
+2

PdfStamper, , , .

0

here may be another answer to your solution: Batch pdf generation

0
source

All Articles