TPdfWriter page size...

Hello,

Using,

TPdfWriter, TUIImage.FromFile ,TPdfWriter.DrawImage

to add images to the end of a PDF export.

The last issue, that I see, the size of an image fitting on the page.
When the image is loaded from file I can get the size in pixels, I assume not related to the size of the PDF page.

The goal is, if the image fits on the page, no change. If the image will not fit on the page the image needs to be scaled to fit on the page.

Ideas?

Thanks,

Mark

The size of the image in pixels is not really relevant, as it depends on the resolution of the monitor where you are looking at the image. I mean, an image with 192 pixels width would be an inch wide in a monitor with 192 pixels per inch. But it would be 2 inches in a monitor that is 96 pixels per inch. The image will be half the size when using double the resolution. For that reason, and because PDF is resolution independent, it never uses physical pixels.

PDF always uses "points" (like the ones in the font size, 12 points) and a point is simple 1/72 of an inch. All the dimensions you would see in the PDF api use points as measurement unit.

Now, images by definition are just an array of pixels, and so the physical image depends on the size of each of those pixels. If pixels are half the size (higher resolution), the image will be half the size too. When you print the image (at 600 dpi), it would be way smaller if you used physical pixels, as those are really small when printing. So resolution in pixels doesn't really matter.

You can assume the image has 96 dpi (a standard in windows), so to convert to pdf size, you can just multiply by 72.0/96.0. (some people call 1/96 of an inch a "virtual pixel"). For some pngs, you can even get the resolution the image was taken, but that value is wrong in many images.

So my advice would be to just forget about how many pixels the image has. As said, even if you knew the image has 96 dpi and so you convert the size to points by multiplying it by 72/96, when you print it, the image will have to be scaled up anyway, because you need to print a lot of more pixels to get the same size, even if those pixels need to be interpolated because they aren't in the image.

What you need to be aware of is the aspect ratio. If you draw the image to say, 2 inches wide, and the image is 96px by 192px, then it has to be 4 inches high. So you need the pixels dimensions to calculate the aspect ratio. But for the width, it really depends on the resolution of the image. And just remember that whatever size you specify in the pdf, is in 1/72 inches units.

Thanks for the response.