title: iTextSharp Out of Memory Exception when stamping over ~1500 pages
body: <p>I am attempting to create a single pdf document from a list of approximately 5000 forms that I am stamping from a list of objects in my ASP.NET MVC website written in C#. I am getting an Out of Memory Exception once I perform the stamping process of approximately 1500 forms. If I restrict my list of objects to under 1500 objects I do not get the error. </p>
  
  <p>Here is the data object which mimics the form data:</p>
  
  <pre><code>public class MyPageData
  {
      public string Name { get; set; }
      public string Description { get; set; }
      public decimal DueAmount { get; set; }
      public decimal PaidAmount { get; set; }
  }
  </code></pre>
  
  <p>The code to create the document. NOTE The template is a pdf form and the Stream is a FileStream object created with write access and no file sharing.</p>
  
  <pre><code>public class DocumentBuilder
  {
      public static void FillDocument(IList&lt;MyPageData&gt; pages, string template, Stream outputFile)
      {
          using (Document document = new Document())
          {
              using (PdfSmartCopy copy = new PdfSmartCopy(document, outputFile))
              {
                  document.Open();
                  foreach (var page in pages)
                  {
                      PdfReader reader = new PdfReader(StampPage(template, page));
                      copy.AddPage(copy.GetImportedPage(reader, 1));
                      copy.FreeReader(reader);
                  }
              }
          }
      }
  
      private static byte[] StampPage(string template, MyPageData page)
      {
          PdfReader reader = new PdfReader(template);
          using (MemoryStream stream = new MemoryStream())
          {
              using (PdfStamper stamper = new PdfStamper(reader, stream))
              {
                  AcroFields fields = stamper.AcroFields;
                  FillFields(fields, page);
                  stamper.FormFlattening = true;
                  stamper.Writer.Flush();
              }
              return stream.ToArray();
          }
      }
  
      private static void FillFields(AcroFields fields, MyPageData page)
      {
          fields.SetField("Name", page.Name);
          fields.SetField("Description", page.Description);
          fields.SetField("DueAmount", page.DueAmount.ToString());
          fields.SetField("PaidAmout", page.PaidAmount.ToString());
      }
  }
  </code></pre>
  
  <p>I have been all over the iText and iTextSharp document and I believe my code is correct. I did attempt to zip the document as demonstrated <a href="http://sourceforge.net/p/itextsharp/code/HEAD/tree/book/iTextExamplesWeb/iTextExamplesWeb/iTextInAction2Ed/Chapter06/DataSheets1.cs/%20here" rel="nofollow">Here</a> but the error is still thrown when stamping. Any thoughts or suggestion?</p>