Importing Image and Keep width and Height proportional to the PDF Page

I want to import an image from a file(png or jpeg) to a pdf document and I want to keep the size of the image as is if it is less than the width and height of the document.


If it is bigger I want to import it with its width and height resized proportionally.

I have this code sof ar

Image image = new Image();
image.IsInLineParagraph = false;
image.ImageStream = new FileStream(“C:\temp\images.jpg”, FileMode.Open);
Page page = pdfDocument.Pages.Add();
page.Paragraphs.Add(image);

pdfDocument.Save(“C:\temp\MyImagePDF.pdf”);

Hi Helen,

Thanks for your inquiry. Please check following documentation link for converting Image to PDF. It sets PDF page dimensions as per image dimensions. However if you want set image as per Page dimensions then do not set page properties but image FixHeight and FixWidth as following. You can compare image and page dimensions and device your logic accordingly. Hopefully it will help you to accomplish the requirements.

// Set margins so image will fit, etc.
//page.PageInfo.Margin.Bottom = 0;
//page.PageInfo.Margin.Top = 0;
//page.PageInfo.Margin.Left = 0;
//page.PageInfo.Margin.Right = 0;
//page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);
// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
image1.FixHeight = page.Rect.Height;
image1.FixWidth = page.Rect.Width

Please feel free to contact us for any further assistance.
Best Regards,