Hi ,
We are trying to merge all pdf documents in to one pdf document. But while merging we are seeing out of memory exceptions. We are using 8.9 version of Aspose.pdf.dll.
We are merging some 20 documents each of average size of 1 mb.
Here is the code snippet
private void MergeData(MemoryStream[] finalstream)
{
MemoryStream objnewpdfstream = new MemoryStream();
PdfFileEditor pdfEditor = new PdfFileEditor();
pdfEditor.KeepFieldsUnique = true;
pdfEditor.UniqueSuffix = "_%NUM%";
pdfEditor.Concatenate(finalstream, objnewpdfstream);
long length = objnewpdfstream.Length;
byte[] buff = new byte[objnewpdfstream.Length];
objnewpdfstream.Read(buff, 0, (int)objnewpdfstream.Length);
if (length > 0)
{
MemoryStream outstream = AddJavascriptToMergedPdf(objnewpdfstream);
if (objnewpdfstream != null)
{
objnewpdfstream.Dispose();
}
}
}
private MemoryStream AddJavascriptToMergedPdf(MemoryStream ms)
{
LoggingHandler.Entering("Entering AOPDOCUI.PrintSelected : AddJavascriptToMergedPdf");
PdfContentEditor contentEditor = new PdfContentEditor();
//1=UST BRAND, 2= BAML BRAND, 3=BAML BRAND PLUS FLASH, 4= UST INSTL BRAND
string strCode = " function docLevelChangeLogos() { console.println(\"i am the document level js\")};";//some javascript
contentEditor.BindPdf(ms);
contentEditor.AddDocumentAdditionalAction(PdfContentEditor.DocumentOpen, strCode);
MemoryStream outstream = new MemoryStream();
contentEditor.Save(outstream);
contentEditor.Close();
LoggingHandler.Exiting("Exiting AOPDOCUI.PrintSelected : AddJavascriptToMergedPdf");
return outstream;
} private MemoryStream AddJavascriptToMergedPdf(MemoryStream ms)
{
PdfContentEditor contentEditor = new PdfContentEditor();
string strCode = " function docLevelChangeLogos() { console.println(\"i am the document level js\")};";//some javascript
contentEditor.BindPdf(ms);
contentEditor.AddDocumentAdditionalAction(PdfContentEditor.DocumentOpen, strCode);
MemoryStream outstream = new MemoryStream();
contentEditor.Save(outstream);
contentEditor.Close();
LoggingHandler.Exiting("Exiting AOPDOCUI.PrintSelected : AddJavascriptToMergedPdf");
return outstream;
}
Here is the exception
Error in PrintSelected: MergeData System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.BinaryWriter.Write(Byte[] buffer, Int32 index, Int32 count) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? , ) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? , ) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? streamWriter, pdfPrimitive, Int64& offset) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? streamWriter, pdfPrimitive, Int64& offset) at ?..Save(? , ? ) at ?..Save(Stream ) at Aspose.Pdf.Document.Save(Stream output) at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream) at BOA.UST.Application.AOPUI.WebPages.PrintSelected.MergeData(MemoryStream[] finalstream)with StackTrace = at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.BinaryWriter.Write(Byte[] buffer, Int32 index, Int32 count) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? , ) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? , ) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? streamWriter, pdfPrimitive, Int64& offset) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? streamWriter, pdfPrimitive, Int64& offset) at ?..Save(? , ? ) at ?..Save(Stream ) at Aspose.Pdf.Document.Save(Stream output) at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream) at WebPages.PrintSelected.MergeData(MemoryStream[] finalstream) Error in PrintSelected: MergeData System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown. at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.BinaryWriter.Write(Byte[] buffer, Int32 index, Int32 count) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? , ) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? , ) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? streamWriter, pdfPrimitive, Int64& offset) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? streamWriter, pdfPrimitive, Int64& offset) at ?..Save(? , ? ) at ?..Save(Stream ) at Aspose.Pdf.Document.Save(Stream output) at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream) at BOA.UST.Application.AOPUI.WebPages.PrintSelected.MergeData(MemoryStream[] finalstream)with StackTrace = at System.IO.MemoryStream.set_Capacity(Int32 value) at System.IO.MemoryStream.EnsureCapacity(Int32 value) at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at System.IO.BinaryWriter.Write(Byte[] buffer, Int32 index, Int32 count) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? , ) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? , ) at ?.?...?(? , , Boolean , Int64& ) at ?.?...?(? streamWriter, pdfPrimitive, Int64& offset) at ??.??.?(? , , Boolean , Int64& ) at ??.??.?(? streamWriter, pdfPrimitive, Int64& offset) at ?..Save(? , ? ) at ?..Save(Stream ) at Aspose.Pdf.Document.Save(Stream output) at Aspose.Pdf.Facades.PdfFileEditor.Concatenate(Stream[] inputStream, Stream outputStream) at WebPages.PrintSelected.MergeData(MemoryStream[] finalstream)