Using MemoryStream with PdfConverter results in blank JPEG

I searched quite a bit and did not find this exact scenario, so I apologize if it is a duplicate.

When I use PdfConverter with a MemoryStream for the output, the output bytes are all zeros (blank). The files are the same size…just the one is blank. Does it not support a memory stream in some way?

This is 7.4.0

using (var stream = new MemoryStream(pdfBytes))
{
var converter = new PdfConverter();
converter.BindPdf(stream);
converter.StartPage = 1;
converter.EndPage = 1;
converter.DoConvert();
using (var outStream = new MemoryStream())
{
converter.GetNextImage(outStream, ImageFormat.Jpeg);
var outBytes = new byte[outStream.Length];
outStream.Read(outBytes, 0, outBytes.Length);
File.WriteAllBytes(@“d:\junk\from_memory.jpg”, outBytes);
}
converter.Close();
}

using (var stream = new MemoryStream(pdfBytes))
{
var converter = new PdfConverter();
converter.BindPdf(stream);
converter.StartPage = 1;
converter.EndPage = 1;
converter.DoConvert();
converter.GetNextImage(@“d:\junk\straight_to_file.jpg”, ImageFormat.Jpeg);
converter.Close();
}

Hi Matt,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thank you for sharing the sample code.

You will need to make a small update in your code to make it work. Please set the position of the stream to 0 before reading its contents. Please see the following updated code for your reference (Specially Bold part):

// Create a FileStream object to read the imag file

FileStream fs = File.OpenRead(@"D:\AP Data\December2012\input.pdf");

// Read the image into Byte array

byte[] pdfBytes = new byte[fs.Length];

fs.Read(pdfBytes, 0, pdfBytes.Length);

using (var stream = new MemoryStream(pdfBytes))

{

var converter = new PdfConverter();

converter.BindPdf(stream);

converter.StartPage = 1;

converter.EndPage = 1;

converter.DoConvert();

using (var outStream = new MemoryStream())

{

converter.GetNextImage(outStream, ImageFormat.Jpeg);

var outBytes = new byte[outStream.Length];

outStream.Position = 0;

outStream.Read(outBytes, 0, outBytes.Length);

File.WriteAllBytes(@"D:\AP Data\December2012\output.jpg", outBytes);

}

converter.Close();

}

Please feel free to contact support in case you need any further assistance.

Thank You & Best Regards,

Oh my, I now feel foolish. Thank you!

Hi Matt,


I am glad to know that your problem is resolved. Please continue using our products and in the event of any further query, please feel free to contact.