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,
So something like this?:
Hi Yves,
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 than your maximum width and height. If you want you set new height, the width will scale accordingly. Please check the following code snippet.
also, you can refine it as per maximum width and height but image width and height will be scaled as follows:
//
// Create Resolution object
var res = new Resolution(resolution);
//
// Create new 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;
//
// Check new Height
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 serialize the image to a stream
Best Regards,