I have a requirement similar to the original poster, but I keep getting out of memory exceptions when the call to concatenate is made.
I am trying to test by concatenating the attached, 1-page pdf 1000 times. I actually have much larger requirements, but I was trying to get a grip on the process and memory usage. Is there a more efficient way to accomplish this with Aspose that will not explode the memory? I figured with the use of streams that Aspose would be able to accomplish this task without loading up the memory with all 1000 pages at once. Any guidance would be appreciated. Thanks.
Code that I am using:
Aspose.Pdf.Kit.License license = new Aspose.Pdf.Kit.License();
license.SetLicense("Aspose.Total.lic");
List<Stream> streams = new List<Stream>();
List<string> tempFiles = new List<string>();
int count = Int32.Parse(repeatTextBox.Text);
for (int i = 0; i < count; i++)
{
tempFiles.Add(i.ToString() + ".pdf");
File.Copy(stitchFileTextBox.Text, i.ToString() + ".pdf", true);
//Create input stream objects holding the PDF files to be concatenated
// DocumentData holds the path to the stitchable stream thanks to InitializeDocuments call
FileStream stitchStream = new FileStream(i.ToString() + ".pdf", FileMode.Open);
streams.Add(stitchStream);
progressLabel.Text = string.Format("Copied file {0} of {1}", i + 1, count);
this.Update();
}
try
{
if (streams.Count > 0)
{
//Create output stream object that would contain the final PDF file
FileStream stitchedStream = new FileStream(@"c:\stitchedDocument.pdf", FileMode.Create);
//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(streams.ToArray(), stitchedStream);
//Finally close the output stream
stitchedStream.Close();
}
int closeCount = 1;
foreach (Stream fileStream in streams)
{
fileStream.Close();
progressLabel.Text = string.Format("Closed file {0} of {1}", closeCount, count);
this.Update();
closeCount++;
}
}
finally
{
// delete temp files
foreach (string file in tempFiles)
{
File.Delete(file);
}
}