title: iTextSharp is giving me the error: "PDF header signature not found"
body: <p>Everything I have read about this error says the file must be missing a "%PDF-1.4" or something similar at the top; however, my file includes it.  I am not an expert in PDF formatting, but I did double check that I don't have multiple %%EOF or trailer tags, so now I'm at a loss as to what is causing my PDF header signature to be bad.  Here is a link to the file if you would like to look at it:  <a href="https://www.dropbox.com/s/heg7g3ummlnu44s/First.pdf" rel="nofollow">Poorly formatted PDF</a></p>
  
  <p>Here is what I'm doing.  I am getting each page of the PDF in the form of a MemoryStream, so I have to append each page to the end of the previous pages.  In order to do this, I am using iTextSharp's PdfCopy class.  Here is the code I am using:</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Takes two PDF streams and appends the second onto the first.
      /// &lt;/summary&gt;
      /// &lt;param name="firstPdf"&gt;The PDF to which the other document will be appended.&lt;/param&gt;
      /// &lt;param name="secondPdf"&gt;The PDF to append.&lt;/param&gt;
      /// &lt;returns&gt;A new stream with the second PDF appended to the first.&lt;/returns&gt;
      public Stream ConcatenatePdfs(Stream firstPdf, Stream secondPdf)
      {
          // If either PDF is null, then return the other one
          if (firstPdf == null) return secondPdf;
          if (secondPdf == null) return firstPdf;
          var destStream = new MemoryStream();
  
          // Set the PDF copier up.
          using (var document = new Document())
          {
              using (var copy = new PdfCopy(document, destStream))
              {
                  document.Open();
                  copy.CloseStream = false;
  
                  // Copy the first document
                  using (var reader = new PdfReader(firstPdf))
                  {
                      for (int i = 1; i &lt;= reader.NumberOfPages; i++)
                      {
                          copy.AddPage(copy.GetImportedPage(reader, i));
                      }
                  }
  
                  // Copy the second document
                  using (var reader = new PdfReader(secondPdf))
                  {
                      for (int i = 1; i &lt;= reader.NumberOfPages; i++)
                      {
                          copy.AddPage(copy.GetImportedPage(reader, i));
                      }
                  }
              }
          }
          return destStream;
      }
  </code></pre>
  
  <p>Every time I receive a new PDF page, I pass the previously concatenated pages (firstPdf) along with the new page (secondPdf) to this function.  For the first page, I don't have any previously concatenated pages, so firstPdf is null, thereby resulting in secondPdf being returned as the result.  The second time I go through, the first page is passed in as firstPdf and the new second page is passed in as secondPdf.  The concatenation works just fine and the results are actually in the First.pdf file linked above.</p>
  
  <p>The problem is when I go to add a third page.  I am using the output of the previous pass (the first two pages) as the input for the third pass, along with a new PDF stream.  The exception occus when I try to initialize the PdfReader with the PDF previously concatenated pages.</p>
  
  <p>What I find particularly interesting is that it fails to read its own output.  I feel like I must be doing something wrong, but I can neither figure out how to avoid the problem, nor why there is a problem with the header; it looks perfectly normal to me.  If someone could show me either what I'm doing wrong with the my code or at least what is wrong with the PDF file, I would really appreciate it.</p>