Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit image onto one PDF page using MigraDoc

Tags:

migradoc

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?

like image 755
Chris Avatar asked Dec 24 '22 17:12

Chris


1 Answers

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

like image 149
I liked the old Stack Overflow Avatar answered Jan 06 '23 10:01

I liked the old Stack Overflow