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

Adjust page size according to image size
https://forum.pdfsharp.net/viewtopic.php?f=2&t=453
Page 1 of 1

Author:  blackjack2150 [ Thu Aug 21, 2008 7:53 am ]
Post subject:  Adjust page size according to image size

Hi.
I wrote a simple image to PDF converter using your library. I noticed that for some image files which are wide, they are trimmed and only the left side is visible in the PDF page produced.

So I decided to make an improvement and to make the PDF page to have the exact size of the source image. However, the effect is not what I expect it to be. The PDF page always comes out bigger than the initial image. I think that the measurement units are not the same, but I am not sure. Here is my code:

Code:
PdfDocument doc = new PdfDocument();
                doc.Pages.Add(new PdfPage());
                XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
                XImage img = XImage.FromFile(source);


                doc.Pages[0].Width = img.Size.Width;
                doc.Pages[0].Height = img.Size.Height ;
                                             
               
                xgr.DrawImage(img, 0, 0);
                doc.Save(destinaton);
                doc.Close();



How can I improve it, so that the PDF Page will have the exact size of the image?

Thanks.

Author:  blackjack2150 [ Thu Aug 21, 2008 11:14 am ]
Post subject: 

Well I found the solution myself :). Here it is, in case anyone else has this issue:

Code:
PdfDocument doc = new PdfDocument();
doc.Pages.Add(new PdfPage());
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source);

doc.Pages[0].Width = XUnit.FromPoint(img.Size.Width);
doc.Pages[0].Height = XUnit.FromPoint(img.Size.Height);
               
xgr.DrawImage(img, 0, 0, img.Size.Width, img.Size.Height);               

doc.Save(destinaton);
doc.Close();

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