Free Support Forum - aspose.com

Shrink to Fit option when converting images to PDF

I have the following code. It works, but the problem is if the image is too wide for the page, it is simply cut off. I need a way to tell the component to essentially ‘shrink to fit’ so the entire image can be visible.


Can this be done? If so, how? Code below.

Dim pdfGen As New Aspose.Pdf.Generator.Pdf()
Dim sec1 As Aspose.Pdf.Generator.Section = pdfGen.Sections.Add()

'Create an image object in the section
Dim image1 As Aspose.Pdf.Generator.Image = New Aspose.Pdf.Generator.Image(sec1)

'Add image object into the Paragraphs collection of the section
sec1.Paragraphs.Add(image1)

'Set the image file stream
image1.ImageInfo.ImageStream = inputStream
'Set the type of image using ImageFileType enumeration

image1.ImageInfo.ImageFileType = Aspose.Pdf.Generator.ImageFileType.Png

'Save the Pdf
pdfGen.Save(outputStream)

Hi Marina,


Thanks for using our products.

In order to fulfill your requirements, either you can update the image dimensions before adding it to Section object of PDF file or you can set page dimensions equal to image dimensions.

Please try adding the following code lines before saving the PDF file, so that page dimensions are set equal to image dimensions.

[VB.NET]

’ instantiate a Bitmap object<o:p></o:p>

Dim bmp As Bitmap = New System.Drawing.Bitmap("c:/pdftest/Map.png")

' set page width equal to image width dimensions

sec1.PageInfo.PageWidth = bmp.Width

' set page height equal to image height dimensions

sec1.PageInfo.PageHeight = bmp.Height

Thanks, that worked!