title: Merge many Pdf into Single pdf using Append bytes[] using C# or asp.net
body: <p><strong>Problem:</strong></p>
  
  <ol>
  <li>I have byte array in my datatable.</li>
  <li>each <code>byte[]</code> is single pdf file for one order.</li>
  <li>I just want to all these pdf <code>byte[]</code> in a single <code>byte[]</code> for single file.</li>
  </ol>
  
  <p>I just want to save it to the browser.</p>
  
  <p>I can achieve appending the <code>byte[]</code>, but it will create one <code>byte[]</code> data as pdf.</p>
  
  <p>the following the sample i did here in console application actually I want in my web application.</p>
  
  <p>here is my code:</p>
  
  <pre><code>public void NewPdf(string[] Pdf)
  {
  
      byte[] newPdf;
      MemoryStream ms = new MemoryStream();
      foreach (var onepdf in Pdf)
      {
          newPdf = System.IO.File.ReadAllBytes(onepdf);
        //newPdf=System.Convert.FromBase64String(onepdf);
        // int resultPdf = newPdf.Length;
        // ms.Write(newPdf, 0, newPdf.Length);
        // var ms = new MemoryStream(new byte[resultPdf], 0, resultPdf, true, true);
         ms.Write(newPdf, 0, newPdf.Length);
         //ms.Write(baadF00d, 0, baadF00d.Length);
  
         //System.Buffer.BlockCopy(newPdf, 0, resultPdf, 0, newPdf.Length);
      }
      byte[] merged = ms.GetBuffer();
     // File.WriteAllBytes("testpdf.pdf", merged);
      PdfReader reader = new PdfReader(merged);
      int n = reader.NumberOfPages;
      Document document = new Document(reader.GetPageSizeWithRotation(1));
      PdfWriter writer = PdfWriter.GetInstance(document, new FileStream("thugs.pdf", FileMode.Create));
      document.Open();
      PdfContentByte cb = writer.DirectContent;
      PdfImportedPage page;
      int rotation;
      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);
  
          }
      }
      reader = new PdfReader(merged);
      // we retrieve the total number of pages
      n = reader.NumberOfPages;
      document.Close();
  
  }
  </code></pre>
  
  <p>Actually i am trying to do in asp.net mvc2....</p>
  
  <p>i can return the <code>byte[]</code> in mvc2 </p>
  
  <pre><code>return File(bytes, mimeType);
  </code></pre>