PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

migradoc add image
https://forum.pdfsharp.net/viewtopic.php?f=2&t=4052
Page 1 of 1

Author:  Jessica [ Mon Nov 11, 2019 3:37 pm ]
Post subject:  migradoc add image

Hi

I add images to my migradoc pdf using this code:

Code:
section.AddImage(fileName);


This works great, except big images are only displayed partly inside the PDF (a part is outside the page and not displayed because the image doesn't fit the page (it's too big)).
How can I add an image to a migradoc PDF that is resized to fit the page perfectly while retaining the original image dimensions (width and heigth).

thx

Author:  Thomas Hoevel [ Tue Nov 12, 2019 9:46 am ]
Post subject:  Re: migradoc add image

Jessica wrote:
How can I add an image to a migradoc PDF that is resized to fit the page perfectly while retaining the original image dimensions (width and heigth).
Set either the image height or the image width and the aspect ratio will be maintained.
If you see both width and height the image will be stretched to fill the region.

Author:  Jessica [ Tue Nov 12, 2019 5:03 pm ]
Post subject:  Re: migradoc add image

Thanks this works!

But now I have another problem: I want to check if height or width of the image is too big to fit on a page (all images have different height and width). In order to adjust the height or width.

I put my image in the pdf with this code:

Code:
Image image = section.AddImage(fileName);
image.Height = "10cm";
image.LockAspectRatio = true;


Can somebody give me a code line that does this: "if image's height is bigger then page's height" (same for width)

Thanks!

Author:  TH-Soft [ Tue Nov 12, 2019 10:12 pm ]
Post subject:  Re: migradoc add image

Jessica wrote:
Can somebody give me a code line that does this: "if image's height is bigger then page's height" (same for width)
Open the image with a PDFsharp XImage object, determine the aspect ratio and, depending on aspect ratio and target rectangle, decide whether to set image height or image width.

Author:  Jessica [ Wed Nov 13, 2019 1:12 am ]
Post subject:  Re: migradoc add image

Hi

1) I use migradoc

2) I want to use a full page as target. How to know the dimensions?

Please give me some example migradoc code. I'm a beginner!

Author:  TH-Soft [ Wed Nov 13, 2019 7:48 am ]
Post subject:  Re: migradoc add image

Jessica wrote:
1) I use migradoc
And MigraDoc uses PDFsharp to load images and create the PDF. If you do not know the dimensions of the images then you can use PDFsharp to determine them.

Jessica wrote:
2) I want to use a full page as target. How to know the dimensions?
DIN A 4? Letter? Something else?

The question about calculating image sizes has been asked before on this forum and may even have some sample code.

Old threads:
viewtopic.php?f=2&t=2293
viewtopic.php?f=2&t=947
viewtopic.php?f=2&t=2010
viewtopic.php?f=2&t=3733

Beginners learn best by doing some research and writing own code.

Author:  Jessica [ Wed Nov 13, 2019 3:31 pm ]
Post subject:  Re: migradoc add image

- I use standard migradoc examples. What page size do I have then? I didn't change anything.
If it's A4, what's its height and width in cm?

- I add images using migradoc:

Code:
Image image = section.AddImage(fileName);
image.Height = "10cm";


Can I use image.Height to get the height of an image in cm and compare this to the height of a page? Or can I only use it for setting the height in cm (not reading the height from the image in cm)?

I prefer not to use pdfsharp, it's too complicated for me.

Author:  TH-Soft [ Wed Nov 13, 2019 7:05 pm ]
Post subject:  Re: migradoc add image

Jessica wrote:
Can I use image.Height to get the height of an image in cm and compare this to the height of a page?
No - as you can see in the threads linked above. I recommend using the XImage class. It is not complicated.

MigraDoc default page size is always DIN A4 IIRC. 21 cm x 29.7 cm.

Author:  Jessica [ Wed Nov 13, 2019 7:21 pm ]
Post subject:  Re: migradoc add image

Can you give me 1 code line please?

How to get the height and width of the image (in cm) using the class you say?
I just want to compare this with the size of A4.

thx

Author:  TH-Soft [ Wed Nov 13, 2019 9:14 pm ]
Post subject:  Re: migradoc add image

Jessica wrote:
I just want to compare this with the size of A4.
I don't see a point in doing this.

Jessica wrote:
How to get the height and width of the image (in cm) using the class you say?
You can query the pixel dimensions and also the DPI values. This allows you to calculate the dimensions in inches, cm, and other units.
Not all image files include DPI values and the real-world dimensions of those images are not known - DPI values will be standard values in those cases.

In my experience it is enough to query the aspect ratio of the images.

Author:  Jessica [ Wed Nov 13, 2019 10:57 pm ]
Post subject:  Re: migradoc add image

I compare with the page to see if it's bigger then the page's height or width. Most of my images are. I need to find if the image's height or width doesn't fit on 1 page.

Please give me code that does all things you say. I don't know how to calculate cm ...
I just want to find out if my image has too big width or height to fit on a page, in order to make it smaller.

You must realise some people are no experts. You talk to me as I'm a professional software developer.
So many answers but nobody gives me a single code line that gives me image's height or width in cm.

Author:  TH-Soft [ Thu Nov 14, 2019 7:14 am ]
Post subject:  Re: migradoc add image

Dimensions in cm are overrated.
Some pictures have 50 by 50 pixel and 50 by 50 inches, some pictures have 5000 by 5000 pixels and 1 by 1 inch.
I'd resize them to fit the space.

I gave you links that show code. I don't have time to write code to solve your particular problem. Maybe someone else will.

Multiplying the pixel size with the DPI value will give you the size in inches. Divide that by 2.54 to get the size in cm.

"Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime."
https://quoteinvestigator.com/2015/08/28/fish/

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/