title: iTextSharp is producing a corrupt PDF
body: <p>The code snippet below returns a corrupt PDF document however if I return mergedDocument instead it always returns a valid PDF.  mergedDocument is based on a PDF file i created using Word, whereas completed document is entirely programmatically generated. The code "works" in that it throws no exceptions.  Why is iTextSharp creating a corrupt PDF?</p>
  
  <pre><code>byte[] completedDocument = null;            
  using (MemoryStream streamCompleted = new MemoryStream())
  {
      using (Document document = new Document())
      {                    
          PdfCopy copy = new PdfCopy(document, streamCompleted);
          document.Open();
          copy.Open();                    
  
          foreach (var item in eventItems)
          {
              byte[] mergedDocument = null;
              PdfReader reader = new PdfReader(pdfTemplates[item.DataTokens[NotifyTokenType.OrganisationID]]);
              using (MemoryStream streamTemplate = new MemoryStream())
              {
                  using (PdfStamper stamper = new PdfStamper(reader, streamTemplate))
                  {
                      foreach (var token in item.DataTokens)
                      {
                          if (stamper.AcroFields.Fields.Any(fld =&gt; fld.Key == token.Key.ToString()))
                          {
                              stamper.AcroFields.SetField(token.Key.ToString(), token.Value);
                          }
                      }
                      stamper.FormFlattening = true;
                      stamper.Writer.CloseStream = false;
                  }
  
                  mergedDocument = new byte[streamTemplate.Length];
                  streamTemplate.Position = 0;
                  streamTemplate.Read(mergedDocument, 0, (int)streamTemplate.Length);
              }
              reader = new PdfReader(mergedDocument);
  
              for (int i = 1; i &lt;= reader.NumberOfPages; i++)
              {
                  document.SetPageSize(PageSize.A4);
                  copy.AddPage(copy.GetImportedPage(reader, i));
              }
          }
          completedDocument = new byte[streamCompleted.Length];
          streamCompleted.Position = 0;
          streamCompleted.Read(completedDocument, 0, (int)streamCompleted.Length);
      }                
  }
  return completedDocument;
  </code></pre>