We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Center and convert Image to pdf


I'm using following code to convert an image to a pdf:

Pdf pdf = new Pdf();

pdf.PageSetup.Margin.Bottom = 0;

pdf.PageSetup.Margin.Top = 0;

pdf.PageSetup.Margin.Left = 0;

pdf.PageSetup.Margin.Right = 0;

pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;

pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;

Aspose.Pdf.Section sec = pdf.Sections.Add();

Aspose.Pdf.Image image = new Aspose.Pdf.Image(sec);


image.ImageInfo.File = ImageFileName;

image.ImageInfo.ImageFileType = GetImageFileType();


But now i want to convert a smaller image to pdf, Is there a way to center the image?

What is the measuring unit of Aspose.Pdf.PageSize.A4Height? Points, Pixels, Inches, Centimeters,…

Kind Regards, Ibbe

Hello Ibbe,

Thanks for considering Aspose.

Please try using the following code lines to center align the image in resultant PDF document.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


image.Margin.Left= pdf.PageSetup.PageWidth /2 - image.ImageWidth;
image.Margin.Top= pdf.PageSetup.PageHeight / 2 - image.ImageHeight;

You can also Center align (Horizontally) the image while using ImageInfo.Alignment property of Image class.


image.ImageInfo.Alignment = AlignmentType.Center;

Regarding your query over measuring unit for PageSize.A4Height, its Point unit. Please visit the following link for some more detailed information Set Page Size and Margins.