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

Free Support Forum - aspose.com

Merging PDF files dynamically

I am evaluating your product. Do you have an example of how to merge PDF files where the number of files to be concatenated is different every time? Your online example only shows how to merge a specific number of files. However, I want to go through a loop and concatenate files dynamically one by one. Is this possible? I am using C# and ASP.NET 2.0. Thank you.

Hi,

The following code should do the trick:

int noOfFiles = 0;

Console.Write("How many files do you want to concatenate ? ");

string userInput = Console.ReadLine();

noOfFiles = Int32.Parse(userInput);

Stream[] files = new Stream[noOfFiles];

for (int i = 0; i < noOfFiles; i++)

{

Console.Write("Enter name of file {0} : ",i+1);

userInput = Console.ReadLine();

files[i] = (Stream)(new FileStream(userInput, FileMode.Open, FileAccess.Read));

}

FileStream outputStream = new FileStream(@"concatenated.pdf", FileMode.Create, FileAccess.Write);

PdfFileEditor fileEditor = new PdfFileEditor();

fileEditor.Concatenate(files, outputStream);

for (int i = 0; i < noOfFiles; i++)

{

files[i].Close();

}

outputStream.Close();

Thanks.

Hello. Thank you for the example. Is it also possible to do the same in-memory?

Hi,

Yes, it's possible. Please check the following code:

FileStream inStream1 = new FileStream(inFile1,FileMode.Open);
FileStream inStream2 = new FileStream(inFile2,FileMode.Open);
MemoryStream innerStream = new MemoryStream();

//Concatnate input Pdf file1 and file2, output is saved in innerStream.
PdfFileEditor editor = new PdfFileEditor();
editor.Concatenate(inStream1,inStream2,innerStream);

Thanks.

Hi. What I wanted to know, specifically, is how I can do this in-memory dynamically. I want to be able to take an unknown number of memorystream files and concatenate them into one memorystream file. Thanks.

Hi,

Now, you have two questions.

1. How can we merge n number of stream. Just make an array of streams and call concatenate function which is taking an array of stream in the first argument. Please refer to the following code:

//Store all input streams in an Array
Stream[] inStreams = new Stream[]{inStream1,inStream2,inStream3,inStream4};

//Instantiate PdfFileEditor object
PdfFileEditor pdfEditor = new PdfFileEditor();

//Call Concatenate method of PdfFileEditor object to concatenate all input streams
//into a single output stream
pdfEditor.Concatenate(inStreams,outStream);

//Finally close the output stream
outStream.Close();

2. The second question is how to read Pdf file to Memory Stream. The code is likely to be like below and please also refer to following article: http://www.yoda.arachsys.com/csharp/readbinary.html

//Reading Pdf content from Database into Memory

Byte[] byte_pdf = (Byte[])ds.Tables[0].Rows[0][0];

System.IO.MemoryStream ms = new System.IO.MemoryStream();

ms.Seek(0, SeekOrigin.Begin);

ms.Write(byte_pdf, 0, byte_pdf.Length);

Thanks.