Scale image to fit to A4 page - Migradoc

msbarnard picture msbarnard · Oct 9, 2014 · Viewed 7.1k times · Source

I am really struggling to get this right, any help would be appreciated.

I have a series of images that I want to build in to a PDF using MigraDoc (1 image = 1 page) Each image must be displayed on a separate page but may not extend over the page it must fit on to the page perfectly.

So, how do I scale an image (of any size) to fit to a page using MigraDoc?

Answer

You call AddImage() to add the image - and in return you get an Image object that allows you to set width and/or height of the image.

What you have to do: check the dimensions of the image, calculate which is the limiting factor (width or height), then set this limiting factor on the Image object and also set LockAspectRatio.

Or set both Width and Height and leave LockAspectRatio off.

For DIN A4, you may allow e.g. 19 cm x 27.7 cm as maximum image size. For an image with 1000x1000 pixel you would set the width to 19 cm (assuming LockAspectRatio is on). Height will then also be 19 cm automatically. For an image with 1000x2000 pixel you would set the height to 27.7 cm. Width will then be 50% of the height.