Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Oct 7th, 2011  |  syntax: C#  |  size: 1.08 KB  |  views: 128  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public byte[] MergePdfs(IEnumerable<byte[]> inputFiles)
  2. {
  3.     MemoryStream outputStream = new MemoryStream();
  4.     Document document = new Document();
  5.     PdfWriter writer = PdfWriter.GetInstance(document, outputStream);
  6.     document.Open();
  7.     PdfContentByte content = writer.DirectContent;
  8.  
  9.     foreach(byte[] input in inputFiles)
  10.     {
  11.         PdfReader reader = new PdfReader(input);
  12.         for (int i = 1; i <= reader.NumberOfPages; i++)
  13.         {
  14.             document.SetPageSize(reader.GetPageSizeWithRotation(i));
  15.             document.NewPage();
  16.             PdfImportedPage page = writer.GetImportedPage(reader, i);
  17.             int rotation = reader.GetPageRotation(i);
  18.             if (rotation == 90 || rotation == 270)
  19.             {
  20.                 content.AddTemplate(page, 0, -1f, 1f, 0, 0,
  21.                     reader.GetPageSizeWithRotation(i).Height);
  22.             }
  23.             else
  24.             {
  25.                 content.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
  26.             }
  27.         }
  28.     }
  29.     document.Close();
  30.            
  31.     return outputStream.ToArray();
  32. }
  33.