We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Concatenate memorystream to memorystream?

Hi all!

I'm trying to append a pdf form as a memorystream to another form in memorystream. The examples always use Filestream objects. I cannot get Concatenate() method to work with memorystream objects.

Please advise as to whether this is possible and if so a code snippet would be greatly appreciated.

Thanks in advance!


Thanks for your interest in our products.

Please visit the following link for required information on
Concatenate PDF Files

Please load the source PDF files into MemorySteam object and pass them to Document object constructor. We apologize for your inconvenience.

Thanks for your reply. I changed my code to match the information but now I get the following error:

Insertion index was out of range. Must be non-negative and less than or equal to size.
Parameter name: index

Below is my code:

protected void AppendBAAMemoryStream(DBApplication application, ref System.IO.MemoryStream appStream)


Aspose.Pdf.Document inputDoc = new Document(appStream);

Aspose.Pdf.Document addDoc = new Document("AddDoc.pdf");




I tried using the Pages.Insert method, which works, except that I cannot insert after the last page. It inserts before the last page, which is wrong.

Hi David,

Kindly use the below mentioned code to concatenate multiple PDF files using MemoryStreams.


//create two file streams to read pdf files
FileStream fs1 = new FileStream(@"d:\pdffiles\Adobe.pdf", FileMode.Open, FileAccess.Read);
FileStream fs2 = new FileStream(@"d:\pdffiles\Adobe.pdf", FileMode.Open, FileAccess.Read);

//create byte arrays to keep the contents of PDF files
byte[] buffer1 = new byte[Convert.ToInt32(fs1.Length)];
byte[] buffer2 = new byte[Convert.ToInt32(fs2.Length)];

int i = 0;

//read PDF file contents into byte arrays
i = fs1.Read(buffer1, 0, Convert.ToInt32(fs1.Length));
i = fs2.Read(buffer2, 0, Convert.ToInt32(fs2.Length));

//now, first convert byte arrays into MemoryStreams and then concatenate those streams
using (MemoryStream pdfStream = new MemoryStream())
using (MemoryStream fileStream1 = new MemoryStream(buffer1))
using (MemoryStream fileStream2 = new MemoryStream(buffer2))
//create instance of PdfFileEditor class to concatenate streams
PdfFileEditor pdfEditor = new PdfFileEditor();

//concatenate both input MemoryStreams and save to putput MemoryStream
pdfEditor.Concatenate(fileStream1, fileStream2, pdfStream);

//convert MemoryStream back to byte array
byte[] data = pdfStream.ToArray();

//create a FileStream to save the output PDF file
FileStream output = new FileStream(@"d:\pdffiles\merged_output.pdf", FileMode.Create,

//write byte array contents in the output file stream
output.Write(data, 0, data.Length);

//close output file
//close input files

Please feel free to contact support in case you need any further assistance.

Thanks & Regards,