title: Add an existing PDF from file to an unwritten document using iTextSharp
body: <p>How do I correctly add the page(s) from an existing on-disk PDF file, to a currently-being-generated in-memory PDF document?</p>
  
  <p>We have a class that produces a PDF document using iTextSharp. This works fine. At the moment we add a Terms &amp; Conditions as an image at the last page:</p>
  
  <pre><code>this.nettPriceListDocument.NewPage();
  this.currentPage++;
  Image logo = Image.GetInstance("/inetpub/Applications/Trade/Reps/images/TermsAndConditions.gif");
  logo.SetAbsolutePosition(0, 0);
  logo.ScaleToFit(this.nettPriceListDocument.PageSize.Width, this.nettPriceListDocument.PageSize.Height);
  this.nettPriceListDocument.Add(logo);
  </code></pre>
  
  <p>I have this image as a PDF document and would prefer to append it. If I can figure this out it would be possible to append other PDF documents we might need to what I am generating.</p>
  
  <p>I have tried:</p>
  
  <pre><code>string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
  PdfReader reader = new PdfReader(tcfile);
  PdfWriter writer = PdfWriter.GetInstance(this.nettPriceListDocument, this.nettPriceListMemoryStream);
  PdfContentByte content = writer.DirectContentUnder;
  for (int pageno = 1; pageno &lt; reader.NumberOfPages + 1; pageno++)
  {
      this.nettPriceListDocument.NewPage();
      this.currentPage++;
      PdfImportedPage newpage = writer.GetImportedPage(reader, pageno);
      content.AddTemplate(newpage, 1f, 1f);
  }
  </code></pre>
  
  <p>Which results in a "document not open" exception at the <code>writer.DirectContentUnder</code></p>
  
  <p>I've also tried:</p>
  
  <pre><code>string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
  PdfReader reader = new PdfReader(tcfile);
  PdfConcatenate concat = new PdfConcatenate(this.nettPriceListMemoryStream);
  concat.AddPages(reader);
  </code></pre>
  
  <p>Which results in inserting a blank, oddly sized page over the usual first page of the document.</p>
  
  <p>I've also tried:</p>
  
  <pre><code>string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
  PdfReader reader = new PdfReader(tcfile);
  PdfCopy copier = new PdfCopy(nettPriceListDocument, nettPriceListMemoryStream);
  for (int pageno = 1; pageno &lt; reader.NumberOfPages + 1; pageno++)
  {
      this.currentPage++;
      PdfImportedPage newpage = copier.GetImportedPage(reader, pageno);
      copier.AddPage(newpage);
  }
  copier.Close();
  reader.Close();
  </code></pre>
  
  <p>Which results in a NullReferenceException at <code>copier.AddPage(newpage)</code>.</p>
  
  <p>I've also tried:</p>
  
  <pre><code>string tcfile = "/inetpub/Applications/Trade/Reps/images/TermsAndConditions.pdf";
  PdfReader reader = new PdfReader(tcfile);
  PdfCopyFields copier = new PdfCopyFields(nettPriceListMemoryStream);
  copier.AddDocument(reader);
  </code></pre>
  
  <p>This also results in a NullReferenceException at <code>copier.AddDocument(reader)</code>.</p>
  
  <p>I've got most of these ideas from various StackOverflow questions and answers. One thing that nobody seemed to deal with, is adding new page(s) from an existing PDF file, to an <em>already-existing in-memory document that is not yet written to a PDF file on disk</em>. This document has already been opened and had pages of data written into it. If I leave this Terms &amp; Conditions procedure out, or just write it is an image (like originally), the resulting PDF comes out just fine.</p>
  
  <p>To finish as I started: How do I correctly add the page(s) from an existing on-disk PDF file, to a currently-being-generated in-memory PDF document?</p>
  
  <p>Thanks and appreciation in advance for your musings on this. Please let me know if I can provide further information.</p>