Fit Image to page

Hi,

Is there a way to fit an image in to a pdf if the image is too big to be show entirely on the page.

Example a image in landscape in a standard pdf document without changing the page layout.

Kind Regards Ibbe

Hello Ibbe,

Thanks for considering Aspose.

You can set the Image Width & Height equal to the page Width & Height to accomplish your requirement. Also please specify the Left & Top margin of PDF document as 0, so that it can easily accommodate the contents. Please try using the following code snippet.

[C#]

//Instantiate a Pdf object by calling its empty constructor
Pdf pdf1 = new Pdf();
//set the page margin
pdf1.PageSetup.Margin.Left = 0;
pdf1.PageSetup.Margin.Top = 0;
//Create a section in the Pdf object
Aspose.Pdf.Section sec1 = pdf1.Sections.Add();<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

//Create an image object in the section
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1);
//Set the path of image file
image1.ImageInfo.File = "d:/pdftest/journal.JPG";
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;

// set the Image Height and Width equal to page Width & Height
image1.ImageInfo.FixHeight = pdf1.PageSetup.PageHeight;
image1.ImageInfo.FixWidth = pdf1.PageSetup.PageWidth;

//Save the Pdf
pdf1.Save(@"d:/pdftest/Scannedimagetest2.pdf");

In case it does not satisfy your requirement or you've any further query, please feel free to contact.

Hi,

Thanks for your answer it was very helpful, but not entirely what I was searching for. Now if the image is smaller than the page it is stretched out and that shouldn’t be happening. I'm going to need to compare the image width with the page with but ImageInfo doesn't seem to provide that information. Is there a solution for that?

Kind Regards Ibbe

Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Sorry for replying you so late.

Yes you are correct. ImageInfo doesn't return the information regarding image Height & Width until or unless the PDF document is saved. Once the file is saved, you can get the Height/Width information of the image. I'm afraid getting the Height/Width information prior to saving the file is not supported.

Please try using the following code snippet to get the Image Height & Width information.

[C#]

Pdf pdf = new Pdf();
//Create a section in the Pdf object
Aspose.Pdf.Section sec1 = pdf.Sections.Add();

//Create an image object in the section
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
//Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1);
//Set the path of image file
image1.ImageInfo.File = "d:/pdftest/tulip.jpg";
//Set the type of image using ImageFileType enumeration
image1.ImageInfo.ImageFileType = ImageFileType.Jpeg;
//Set image title
image1.ImageInfo.Title = "JPEG image";

MemoryStream ms = new MemoryStream();
pdf.Save(ms);
ms.Close();

MessageBox.Show("" + image1.ImageHeight.ToString() + "----Width=---" + image1.ImageHeight.ToString());