Convert PDF page to image MemoryStream not File

Hello,

I have Aspose.Total .Net License and using Aspose.Pdf .Net
In the Docs this example exists but I Do NOT want to use file or disk access only Memory Stream, is there a way for that without memory mapped files?

Document pdfDocument = new Document(“input.pdf”);
using (FileStream imageStream = new FileStream(“image.jpg”,FileMode.Create))
{
Resolution resolution = new Resolution(300);
JpegDevice jpegDevice = new JpegDevice(resolution, 100);
	jpegDevice.Process(pdfDocument.Pages[1], imageStream);
         imageStream.Close();
}



Thanks,
Sameh

Hi Sameh,

Thanks for using our products.

As per my understanding, you do not want to use FileStream object and need to use MemoryStream object when saving output of PDF to Image conversion. If so is the case, then you may consider using the following code snippet to accomplish your requirements.

In case I have not properly understood your requirement or you have any further query, please feel free to contact.

[C#]

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

// instantiate MemoryStream object
MemoryStream imageStream = new MemoryStream();

Resolution resolution = new Resolution(300);

JpegDevice jpegDevice = new JpegDevice(resolution, 100);

jpegDevice.Process(pdfDocument.Pages[1], imageStream);

//imageStream.Close();

Console.WriteLine(imageStream.Length);
imageStream.Close();

Thank you for the answer,

It works! however in my web service it was always throwing this error:
"Cannot access a closed Stream"

the problem was the pdfDocument stream itself is closed not the ImageStream

I mean if we read the pdfDocument from MemoryStream, we have to keep the stream open.
it looks like the pdf Document does not have a copy of the content, however it is using the same MemoryStream that it was read from.

I would recommend having this piece of info in the documentation
related to opening pdfDocument from MemoryStream.

Thanks Any Way For your Support.



Hi Sameh,


Can you please share some code snippet which can help us in replicating above stated issues. We apologize for this inconvenience.

Currently I don’t have problem.

but what I meant is for example assume I have a MethodExtension Like This

public static Aspose.Pdf.Document asposePdfFromBase64(string PdfContentBase64)
{
var xx = System.Convert.FromBase64String(PdfContentBase64);
Stream stream = new MemoryStream(xx);
var pdf = new Aspose.Pdf.Document(stream);
stream.Close(); // NOTE THIS
return pdf;
}

and I am creating the pdfDoc reference using this MethodExtension as following
pdfDoc = asposePdfFromBase64(whateverBase64Str);
now I cannot merge to pdfDoc because of // stream is closed

however when I commented stream.Close() as following

public static Aspose.Pdf.Document asposePdfFromBase64(string PdfContentBase64)
{
var xx = System.Convert.FromBase64String(PdfContentBase64);
Stream stream = new MemoryStream(xx);
var pdf = new Aspose.Pdf.Document(stream);
// stream.Close(); // NOTE THIS NOW
return pdf;
}

every thing worked.

Thanks
Sameh

Hi Sameh,

Thanks for sharing the details. I am glad to hear that your problem is resolved.

However, I have also tried replicating the issue using following sample code, and I am unable to notice the problem.

FileStream fs = new FileStream("c:/pdftest/PDF_Printed_Normal.pdf", FileMode.Open);
Document doc = new Document(fs);

Console.WriteLine(doc.Pages.Count);
fs.Close();

Document doc2 = new Document("c:/pdftest/html.pdf");

doc2.Pages.Add(doc.Pages);
Console.WriteLine(doc2.Pages.Count);