Fit image onto one PDF page using MigraDoc
Asked Answered
B

1

5

I am able to easily add an image to a section in the PDF file with MigraDoc. However, the image is cut in half by the page width.

Is there a way to force the picture to resize so that it completely fits on one page? Or must this be done manually?

If it has to be done manually, does anyone know the dimensions of a PDF page in MigraDoc?

Bey answered 8/7, 2015 at 18:49 Comment(0)
C
10

The image will be drawn in the size you set for the image.

Image image = section.Headers.Primary.AddImage("../../PowerBooks.png");
image.Width = "2.5cm";
image.LockAspectRatio = true;

This snippet sets the width. The height will be adjusted proportionally because LockAspectRatio was set. You can set both height and width, but then the aspect ratio won't be kept.

There is no trick like "100%" to get the full page width, but that is not needed IMHO. The page width can always be calculated from the PageSetup of the section.

The standard page size is DIN A4. Width is 21 cm, height is 29.7 cm. Many other pre-defined page sizes are also available. And custom page sizes can also be used. See the PageSetup property of your section.

Have you checked the samples that come with MigraDoc? You can also see them here:
http://pdfsharp.net/wiki/Invoice-sample.ashx

Centonze answered 8/7, 2015 at 21:21 Comment(3)
Thank you; I ended up setting the margins to zero and testing in unit points until I found the width--595. Not sure that is what I'm going with, but it works for a proof of concept.Bey
It sucks, that MigraDoc doesnt calculate the new Height at the point, when I set new Width. All the properties are left zeros...Bharal
@Bharal When used as shown here MigraDoc will maintain the aspect ratio and use the correct height. The height remains unset (null) and when you query the height, the value 0 will be returned. It's a bug, not a feature. You can simply calculate the height yourself if you need it.Centonze

© 2022 - 2024 — McMap. All rights reserved.