Hello,
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 ".jpeg":
case ".jpg":
case ".png":
case ".gif":
case ".bmp":
var image = new PdfBitmap(companyProjFileDir + @"\" + fn + fileExt);
var pg = tmpPdf.Pages.Add();
var 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()));
break;