We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Insert Image centered in pdf page

We have the need to convert JPEG, PNG, GIF, and BMP files into a PDF file.
I'm using the PDFImage class but not sure how to center the image within the PDFDocument page.
I can load the image and see its Height and Width in pixels as well as the PhysicalDimensions in Points.
Can you help me determine how to calc the point to place the image so that it is centered on the portrait page?
I guess we need to convert the Image pixel size into points using the PDFUnitConvertor.  This value should allow me to calc the X,Y Point that will center the graphic.
Any help would be appreciated, below is my current code. 
Thx...Bob Baldwin
Trabon Solutions

var tmpPdf = new PdfDocument();
case ".jpg":
case ".png":
case ".gif":
case ".bmp":
var image = new PdfBitmap(companyProjFileDir + @"\" + fn + fileExt);
var pg = tmpPdf.Pages.Add();
ar g = pg.Graphics;
g.DrawImage(image, 0, 0, image.Width, image.Height);
Debug.WriteLine(string.Format("image.Width={0}", image.Width));
Debug.WriteLine(string.Format("image.Height={0}", image.Height));
Debug.WriteLine(string.Format("image.HorizontalResolution={0}", image.HorizontalResolution));
Debug.WriteLine(string.Format("image.VerticalResolution={0}", image.VerticalResolution));
Debug.WriteLine(string.Format("image.PhysicalDimensions={0}", image.PhysicalDimension.ToString()));
Debug.WriteLine(string.Format("pg.Size={0}", pg.Size.ToString()));
Debug.WriteLine(string.Format("pg.ClientSize={0}", pg.GetClientSize().ToString()));
Debug.WriteLine(string.Format("PageSettings.Size={0}", tmpPdf.PageSettings.Size.ToString()));
Debug.WriteLine(string.Format("PageSettings.Margins={0}", tmpPdf.PageSettings.Margins.ToString()));
Debug.WriteLine(string.Format("PageSettings.Height={0}", tmpPdf.PageSettings.Height));
Debug.WriteLine(string.Format("PageSettings.Width={0}", tmpPdf.PageSettings.Width));
PdfUnitConvertor con = new PdfUnitConvertor();
SizeF size = con.ConvertToPixels(image.PhysicalDimension, PdfGraphicsUnit.Point);
Debug.WriteLine(string.Format("image.PhysicalDim2Pixel={0}", size.ToString()));

2 Replies

BB Bob Baldwin May 9, 2013 07:03 PM UTC

Nevermind, I found it after more searching on your forums:

PH Praveenkumar H Syncfusion Team May 11, 2013 08:59 AM UTC

Hi Bob,

Thank you for using Syncfusion products,

We are happy to hear that you got the solution.

Please let us know if you have any concern

With Regards,

Live Chat Icon For mobile
Up arrow icon