title: Using itextsharp to merge pdf files within a folder
body: <p>I'm trying to use codes below to merge the pdf files in a folder and output into a new file but apparently the generated file seems corrupted.</p>
  
  <pre><code>public Boolean MergeForm(String destinationFile, String sourceFolder)
      {
          try
          {
              using (MemoryStream stream = new MemoryStream())
              using (Document doc = new Document())
              using (PdfCopy pdf = new PdfCopy(doc, stream))
              {
                  doc.Open();
  
                  PdfReader reader = null;
                  PdfImportedPage page = null;
  
                  foreach (var file in Directory.GetFiles(sourceFolder))
                  {
                      reader = new PdfReader(file);
                      for (int i = 0; i &lt; reader.NumberOfPages; i++)
                      {
                          page = pdf.GetImportedPage(reader, i + 1);
                          pdf.AddPage(page);
                      }
  
                      pdf.FreeReader(reader);
                      reader.Close();
                  }
                  using (FileStream streamX = new FileStream(destinationFile, FileMode.Create))
                  {
                      stream.WriteTo(streamX);
                  }
              }
              return true;
          }
          catch (Exception)
          {
              return false;
          }
      }
  </code></pre>
  
  <p>Can anyone spot on where's the problem? Thank you.</p>