Save PDF to PNG and set only width and scale height automatically

Hello,

I tried to convert the first page of a pdf document to a png file.
Works fine so far. I can set resolution and width, height.

As I wanted to create a thumbnail as preview image, I do only want to set the width parameter. In this case the height should be set automatically to not change the scale of the png.

How can I achieve this?

Kind regards, Yves

Hi Yves,


Thanks for your inquiry. You can get existing width and height of page using page.Rect.Width and page.Rect.Height, later find proportion/scaling factor between these and use it to calculate height on the basis of new width for PDF to image conversion. Hopefully it will help you to accomplish the task.

Best Regards,

So something like this?:


// Create Resolution object
var res = new Resolution(resolution);

// now scale width and height
var page = pdfDocument.Pages[pageCount];
var scale = page.Rect.Width/page.Rect.Height;
var maxWidth = width;
var maxHeight = maxWidth / scale;

if ( Convert.ToInt32(maxHeight) > height && height > 0)
{
maxHeight = Convert.ToDouble(height);
maxWidth = Convert.ToInt32(maxHeight * scale);
}

// Create PNG device with specified attributes
var pngDevice = new PngDevice(maxWidth, Convert.ToInt32(maxHeight), res);
// Convert a particular page and save the image to stream

Hi Yves,


Thanks for sharing the code snippet.

You may accomplish your requirement by setting width value and set image height information equal to page height dimension. In case I am still unable to understand this requirement, please share some further details.

Ok for explanation.

I want to create a preview of the first pdf page as PNG file to preview it fast in a web environment. In the past I used Image Magicks and Ghostscript for this task.

Now with Aspose PDF I can directly save a PDF document to a PNG file.
But the requirement is max width AND/OR max height.
So in my case I set for example 800x600 max width and height. So I needed to determine the scale of the page and set width to max 800 / 600 but width and height should not be higher then the max values. So whatever scale the PDF document has I can create a preview with the correct scale.

Hope this clarify my question and my solution after the correct in the first answer.

Bye Yves

Hi Yves,


Thanks for your feedback. As per my understanding you want to set new width of resultant image and need height should scale automatically, and both width and height should not be greater that your maximum width and Height. Otherwise you set new height and want to width scale accordingly. Please check following code snippet, you can refine it as per maximum width and height but image width and height will be scaled as following.

// Create Resolution object<o:p></o:p>

var res = new Resolution(resolution);

// now scale width and height

var page = pdfDocument.Pages[pageCount];

var height_scale = page.Rect.Height / page.Rect.Width;

var width_scale = page.Rect.Width / page.Rect.Height;

var newWidth = width;

var newHeight = newWidth * height_scale;

if (Convert.ToInt32(newHeight) > height && height > 0)

{

newHeight = Convert.ToDouble(height);

newWidth = Convert.ToInt32(newHeight * width_scale);

}

// Create PNG device with specified attributes

var pngDevice = new PngDevice(newWidth, Convert.ToInt32(newHeight), res);

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

Best Regards,