When trying to convert a Pdf to images System.OutOfMemoryException is thrown

Good morning,


We recently purchased Aspose Total for .Net - Developer OEM.

We created an application that rasterizes pdfs. We keep getting System.OutOfMemoryException on all larger PDF documents. The exception is thrown when saving to a Memory Stream (doc.Save(ms); //doc is Aspose.Pdf.Document)

Here is the exception:

System.OutOfMemoryException was caught
HResult=-2147024882
Message=Exception of type ‘System.OutOfMemoryException’ was thrown.
Source=mscorlib
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 . .Write(Byte[] , Int32 , Int32 )
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()
at . .Save(Stream )
at . .Save(Stream )
at Aspose.Pdf.Document.Save(Stream output)
at ConvertingToPdf.Program.ConvertImagesToPdf(Image[] imagesToConvertToPdf) in c:\Users\timothy.LAWTRUST\Desktop\Tests\Conversion tests with Aspose\ConvertingToPdf\ConvertingToPdf\Program.cs:line 271
InnerException: null


I have attached the code we are using as well as a document sample. The code is from a simple console application.
The console application calls RasterizePdf method which in turn calls ConvertPdfToImages and then ConvertImagesToPdf

The exception is thrown when I call : doc.Save(ms); in ConvertImagesToPdf

Hi Rendu,


Thanks for your inquiry. I am afraid your attached PDF file is corrupt, its size is 0 KB. Please share it again, so we will test the scenario and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

I have attached the file again. It seems to only throw the exception when saving to a memory stream. If I save to a file it works fine.

However we are implementing your library in a web service, so we do not want to use physical files.

I have attached another file that also throws an exception. It seems to do it with larger documents, or documents with many pages.


Hi Rendu,


Thanks for sharing the sample document. But I am afraid I am unable to test your shared code due to missing reference. I will appreciate it if you please share your working sample code(console application), so we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

I have attached the entire solution as a .zip.


You will just have to change what document it is using and where it stores the resulting document.
The documents are also in the project.

Hi Rendu,


Thanks for sharing your source project. I have tested it over Win 7 64 bit, 8 MB RAM and Aspose.Pdf for .NET 10.9.0 and unable to notice OutOfMemoryException. Please use latest version of Aspose.Pdf for .NET if you are using some old one. Moreover, you may optimize the resultant PDF document file size using following code in ImagetoPdf method.

.....

.....

//Global.log.Debug("[ConvertImagesToPdf] - Saving created Pdf document to memory stream");

MemoryStream ms = new MemoryStream();

byte[] outputDocument = null;

doc.Save(ms);

doc = new Document(ms);

// Optimzie the file size by removing unused objects

doc.Optimize();

doc.OptimizeResources(new Document.OptimizationOptions()

{

AllowReusePageContent = true,

LinkDuplcateStreams = true,

RemoveUnusedObjects = true,

RemoveUnusedStreams = true,

CompressImages = true,

ImageQuality = 80

});

ms = new MemoryStream();

doc.Save(ms);

outputDocument = ms.ToArray();

.....

.....

We are sorry for the inconvenience caused.


Best Regards,