Getting Out of memory exception while converting pdf document to tiff using FACADES

Hey Team,

We are geting the OUT OF MEMORY exception while using the below code attached the pDF file of size 420 KB


var input = new FileStream(inputPath, FileMode.Open); // Made it FileStream, instead of MemoryStream
Stream doc = new MemoryStream(); // TODO-A: avoid using memory stream;
AsposeLicenseMgr.SetLicense(AsposeLicense.Pdf, AsposeLicense.PdfKit);

Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter();
pdfConverter.BindPdf(input);
pdfConverter.DoConvert();

while (pdfConverter.HasNextImage())
pdfConverter.GetNextImage(DateTime.Now.Ticks.ToString() + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
var tiffTempFile = Utility.NewTempFile().FullName;
using (var tiffTempStream = new TempFileStream(tiffTempFile))
{
pdfConverter.SaveAsTIFF(tiffTempStream);
tiffTempStream.Position = 0;
tiffTempStream.CopyTo(doc);

pdfConverter.Close();
}

Hi,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 10.4.0 in Visual Studio 2012 project with target platform as .NET Framework 4.0 and I am unable to notice any issue. As per my observations, the resultant JPEG and TIFF files are properly being generated.

For your reference, I have also attached the TIFF image generated over my end. Please try using the latest release and in case you still face any issue, please share some details regarding your working environment. We are sorry for your inconvenience.

[C#]

var input = new FileStream(“c:/pdftest/Sampleimages.pdf”, FileMode.Open); // Made it
FileStream, instead of MemoryStream
<o:p></o:p>

Stream doc = new MemoryStream(); // TODO-A: avoid using memory stream;

Aspose.Pdf.Facades.PdfConverter pdfConverter = new Aspose.Pdf.Facades.PdfConverter();

pdfConverter.BindPdf(input);

pdfConverter.DoConvert();

int counter=1;

while (pdfConverter.HasNextImage())

{

pdfConverter.GetNextImage("c:/pdftest/Sampleimages_" +counter+ ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

counter += 1;

}

pdfConverter.SaveAsTIFF(“c:/pdftest/Sampleimages.tiff”);

Hey Nayyer,


What if we try to save the genrated .tiff to Memory stream and add the stream to .rtf ??

tiffTempStream.CopyTo(doc);


and moreover the sample you attached .tiff image is of size 2.4 MB

which was very large for a pdf doc file of size 420KB

hey Team,


Do we have any update on this???



Hi,


Thanks for sharing the details.

Since you have been using a legacy Aspose.Pdf.Facades approach to convert PDF file to TIFF image, so in order to have an optimized file, please try using value from CompressionType enumeration. Please take a look over following code snippet.

[C#]

//open
document
<o:p></o:p>

Document pdfDocument = new Document("c:/pdftest/Sampleimages.pdf");

//create Resolution object

Resolution resolution = new Resolution(300);

//create TiffSettings object

TiffSettings tiffSettings = new TiffSettings();

tiffSettings.Compression = CompressionType.CCITT4;

tiffSettings.Depth = Aspose.Pdf.Devices.ColorDepth.Default;

tiffSettings.SkipBlankPages = false;

//create TIFF device

TiffDevice tiffDevice = new TiffDevice(resolution, tiffSettings);

//convert a particular page and save the image to stream

tiffDevice.Process(pdfDocument, "c:/pdftest/Sampleimages_output.tif");

MemoryStream Stream = new MemoryStream();

// save output in stream object

tiffDevice.Process(pdfDocument, Stream);



When using above code snippet, the resultant TIFF is 722KB. Furthermore, when using this approach, you can also save the output in Stream object and then use the same object for further manipulation.

hey Nayyer,


We tried using the compression type .In the compression type we are seeing the document as attached which was not similar to the oiginal file w.r.t Color

Moreover the 722KB you are mentioning is for just on pdf Page but not for the complete PDF document i shared.The document which i shared has 5 pages.Does that mean for the complete PDF document will it take 3.6 MB of .tiff image file ??

First thing,

Even after adding the stream of .tiff image of size 720KB to RTF/.DOC using aspose, we are still getting the OUT OF Memoery exception

Second thing,
If we use the latest version of PDF is there any possibility to get rid of this out of memory exception


munnismiles:
We tried using the compression type .In the compression type we are seeing the document as attached which was not similar to the oiginal file w.r.t Color
Hi,

When setting compression options, the color information is changed and in case you need to have colored output, you can use CompressionType as None.
munnismiles:
Moreover the 722KB you are mentioning is for just on pdf Page but not for the complete PDF document i shared.The document which i shared has 5 pages.Does that mean for the complete PDF document will it take 3.6 MB of .tiff image file ??
Hi,

The TIFF image generated with earlier shared code contains 5 frames and the total size of resultant image is 722KB. For your reference, I have also attached the resultant file generated over my end.
munnismiles:
Even after adding the stream of .tiff image of size 720KB to RTF/.DOC using aspose, we are still getting the OUT OF Memoery exception

Second thing,
If we use the latest version of PDF is there any possibility to get rid of this out of memory exception
Hi,

PDF to TIFF is properly being generated. However from above problem description, it appears to be an issue with Aspose.Words where exception is being added when trying to add TIFF image to DOC/DOCX file. I have intimated my fellow worker from Aspose.Words team to further look into this matter. We apologize for this inconvenience.

Hi,

You can use the code from Convert Image to PDF example to convert your TIFF to DOC/RTF. Just change the output file name to DOC/RTF in this example.

If you want to insert TIFF image at a specified location in the document, you can use DocumentBuilder.InsertImage method for this purpose. Please share your complete code to insert TIFF in the document and a sample TIFF if you still see out of memory exception.

Best Regards,

Hey,

please find the sample code here

Aspose.Pdf.Document document = new Aspose.Pdf.Document(inputPath);
Aspose.Pdf.Devices.Resolution resolution = new Aspose.Pdf.Devices.Resolution(300);
Aspose.Pdf.Devices.TiffSettings tiffSettings = new Aspose.Pdf.Devices.TiffSettings();
tiffSettings.Compression = Aspose.Pdf.Devices.CompressionType.CCITT4;// = Compression.BZip2.StringInputStream;
tiffSettings.Depth = Aspose.Pdf.Devices.ColorDepth.Default;
tiffSettings.SkipBlankPages = false;
Aspose.Pdf.Devices.TiffDevice tiffDevice = new Aspose.Pdf.Devices.TiffDevice(resolution, tiffSettings);
var tiffTempFile = Utility.NewTempFile().FullName;
using (var tiffTempStream = new TempFileStream(tiffTempFile))
{
tiffDevice.Process(document, "c:/pdfcache/Sampleimages_output.tif");
Aspose.Words.Document rtfdoc = new Aspose.Words.Document();
Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(rtfdoc);

builder.InsertImage("c:/pdfcache/Sampleimages_output.tif");
rtfdoc.Save("c:/pdfcache/Sampleimages_output.rtf",Aspose.Words.SaveFormat.Rtf);
tiffDevice.Process(document, tiffTempStream);
tiffTempStream.Position = 0;
tiffTempStream.CopyTo(doc);
}

any update??

Hi,

Sorry, I was unable to reproduce this issue at my end. Your PDF was converted to TIFF and TIFF was added to RTF without any issue. I was not able to use doc variable and TempFileStream method as these were not declared anywhere in your code. Remaining code was working fine.

Can you please share a runnable application with all required methods and variables to reproduce the issue?

Best Regards,