title: Merging multiple PDFs using iTextSharp in c#.net
body: <p>Well i'm trying to merge multiple PDFs in to one.</p>
  
  <p>I gives no errors while compiling. I tried to merge the docs first but that went wrong because I'm working with tables.</p>
  
  <p>This is the <strong>asp.net code-behind</strong></p>
  
  <pre><code>if (Button.Equals("PreviewWord")) {
  
          String eventTemplate = Server.MapPath("/ERAS/Badges/Template/EventTemp" + EventName + ".doc");
  
          String SinglePreview = Server.MapPath("/ERAS/Badges/Template/PreviewSingle" + EventName + ".doc");
  
          String PDFPreview = Server.MapPath("/ERAS/Badges/Template/PDFPreviewSingle" + EventName + ".pdf");
  
          String previewPDFs = Server.MapPath("/ERAS/Badges/Template/PreviewPDFs" + EventName + ".pdf");
  
          if (System.IO.File.Exists((String)eventTemplate))
          {
  
              if (vulGegevensIn == true)
              {
                //This creates a Worddocument and fills in names etc from database
                  CreateWordDocument(vulGegevensIn, eventTemplate, SinglePreview, false);
                  //This saves the SinglePreview.doc as a PDF @param place of PDFPreview
                  CreatePDF(SinglePreview, PDFPreview);
  
  
                  //Trying to merge
                  String[] previewsSmall=new String[1];
                  previewsSmall[0] = PDFPreview;
                  PDFMergenITextSharp.MergeFiles(previewPDFs, previewsSmall);
              }
  
  
              // merge PDFs here...........................;
              //here
              //no here//
              //...
  
  
      } }
  </code></pre>
  
  <p><strong>This is the PDFMergenITextSharpClass</strong></p>
  
  <p>public static class PDFMergenITextSharp
  {</p>
  
  <pre><code>public static void MergeFiles(string destinationFile, string[] sourceFiles)
  {
  
      try
      {
          int f = 0;
          // we create a reader for a certain document
          PdfReader reader = new PdfReader(sourceFiles[f]);
          // we retrieve the total number of pages
          int n = reader.NumberOfPages;
          //Console.WriteLine("There are " + n + " pages in the original file.");
          // step 1: creation of a document-object
          Document document = new Document(reader.GetPageSizeWithRotation(1));
          // step 2: we create a writer that listens to the document
          PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));
          // step 3: we open the document
          document.Open();
          PdfContentByte cb = writer.DirectContent;
          PdfImportedPage page;
          int rotation;
          // step 4: we add content
          while (f &lt; sourceFiles.Length)
          {
              int i = 0;
              while (i &lt; n)
              {
                  i++;
                  document.SetPageSize(reader.GetPageSizeWithRotation(i));
                  document.NewPage();
                  page = writer.GetImportedPage(reader, i);
                  rotation = reader.GetPageRotation(i);
                  if (rotation == 90 || rotation == 270)
                  {
                      cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                  }
                  else
                  {
                      cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                  }
                  //Console.WriteLine("Processed page " + i);
              }
              f++;
              if (f &lt; sourceFiles.Length)
              {
                  reader = new PdfReader(sourceFiles[f]);
                  // we retrieve the total number of pages
                  n = reader.NumberOfPages;
                  //Console.WriteLine("There are " + n + " pages in the original file.");
              }
          }
          // step 5: we close the document
          document.Close();
      }
      catch (Exception e)
      {
          string strOb = e.Message;
      }
  }
  
  public static int CountPageNo(string strFileName)
  {
      // we create a reader for a certain document
      PdfReader reader = new PdfReader(strFileName);
      // we retrieve the total number of pages
      return reader.NumberOfPages;
  }
  }
  </code></pre>