PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Dec 08, 2019 10:05 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: migradoc add image
PostPosted: Mon Nov 11, 2019 3:37 pm 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
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


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Tue Nov 12, 2019 9:46 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3012
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Tue Nov 12, 2019 5:03 pm 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Tue Nov 12, 2019 10:12 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 508
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 1:12 am 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
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!


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 7:48 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 508
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 3:31 pm 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
- 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.


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 7:05 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 508
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 7:21 pm 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
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


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 9:14 pm 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 508
Location: CCAA
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.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Wed Nov 13, 2019 10:57 pm 
Offline

Joined: Sun Nov 03, 2019 7:51 pm
Posts: 37
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: migradoc add image
PostPosted: Thu Nov 14, 2019 7:14 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 508
Location: CCAA
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/

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 29 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group