Out of Memory Exception while merging Documents

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)

Hi Vinay,


Thanks for contacting support.

Can you please share some sample PDF files causing this problem so that we can test the scenario at our end. We are sorry for this inconvenience.

Hi,

We are trying to merge many documents. So will not be able to provide all documents. But if you still need one of the template.

Please find the attached template.

The other point to note here is.. once we get these exceptions then even for 2 documents its failing.

Hi Vinay,


Thanks for sharing the resource file.

I have tried replicating the issue using Aspose.Pdf for .NET 9.2.1, where I have used the following code snippet and I am unable to reproduce it. Can you please share some further details which can help us in reproducing this problem in our environment.

[C#]

Stream[] finalstream = new Stream[2] { new FileStream(“c:/pdftest/123.pdf”, FileMode.Open), new FileStream(“c:/pdftest/123 -
Copy.pdf”
, FileMode.Open) };<o:p></o:p>

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;

}<o:p></o:p>

Hi,

Thanks for reply.

Can you please test it with 8.9 version and check.

Note: Remember this is happening when we try to merge somewhere around 10-20 documents,

Hi Vinay,


Thanks for your sharing the details.

I have again tried replicating the issue with latest release of Aspose.Pdf for .NET 9.2.0 where I have first created 27 copies of original 123.pdf and have placed all the files in a separate folder. Then I have loaded individual files into Stream objects and have tried concatenating the files in Visual Studio 2010 application with Target platform as .NET Framework 4.0 over Windows 7 (x64). Can you please try using the latest release and in case you still face the issue, please share some details following information.

  • What environment are you running on?
    • OS (Windows Version)
    • Architecture (32 / 64 bit)
    • .NET Framework version
    • Any Third Party Libraries / SDK in addition to Aspose API's
  • Please supply us with a stack trace detailing the issue

[C#]

// Retrieve names of all the Pdf files in a particular Directory

string[] fileEntries = Directory.GetFiles(@"C:\pdftest\concatenate\", "*.pdf");

Stream[] finalstream = new Stream[28];

for(int counter = 0; counter<fileEntries.Length;counter++)// each (string filename in fileEntries)

{

finalstream[counter] = new FileStream(fileEntries[counter], FileMode.Open);

Console.WriteLine("Current file loaded = "+fileEntries[counter]);

}

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;

}<o:p></o:p>

Hi,

Can you please try doing same with 8.9 version. We just wanted to check, if its an issue with 8.9 version then we can upgrade to 9.2. Version.

Here are the answers

  • What environment are you running on?
    • OS (Windows Version) : Windows 2008 R2<?xml:namespace prefix = o />
    • Architecture (32 / 64 bit) : 64 bit operating system
    • .NET Framework version: Net 4.0
    • Any Third Party Libraries / SDK in addition to Aspose API's : no
  • Please supply us with a stack trace detailing the issuev
  • 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 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 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 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 PrintSelected.MergeData(MemoryStream[] finalstream)

    Hi Vinay,


    Thanks for sharing the details.

    In my earlier attempts, I tested the scenario over Windows 7 (x64) and you are getting the problem over Windows 2008 (x64). We are working on replicating this issue in our environment and will get back to you soon.

    Thanks. Please test it with windows 2k8 and Aspose 8.9 and latest version(9.2). Please host the application in IIS server and test with more number of documents(around 40) and repeat this testing multiple times.

    Even we are getting the Merge documents initially but only after some time we get this issue. So its not like that its Not working at all, its working but only after some time we get this issue and once we get this issue after that not a single documents gets printed.

    Hi Vinay,


    Thanks for sharing the feedback. We will definitely consider this information during the issue replication and will keep you posted with our findings.

    Hi ,

    Did we have any update on this issue.

    Regards,

    Vinay Parekh

    Hi Vinay,


    Thanks for your patience. I am afraid I could not replicate the issue but right now I am working on replicating it over another machine and will share my findings, shortly. We are really sorry for this delay and inconvenience.

    Hi,

    Did we get any update here.

    Regards,

    Vinay Parekh

    Hi Vinay,


    Sorry for the delayed response.

    I will be sharing my findings in few moments. The tests are being conducted on a newly prepared machine.

    Hi Vinay,


    Thanks for your patience.

    We have performed a detailed testing of above stated stated where I have used around 84 PDF files (copies 123.pdf of which you have shared earlier) and I have used the code snippet shared in my earlier post 549505 and I am unable to notice any issue. Please note that for testing purposes, I have used Windows Server 2008 R2 and a web application developed in VisualStudio 2010 and I am unable to notice any issue.

    I have used 8.9.0, 9.1.0 and 9.3.0 and none of these versions produced this problem. Can you please share some solution/project which can help us in further replicating this problem. We are sorry for your inconvenience.