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

Free Support Forum - aspose.com

Keep image height/width ratio unchanged when inserting a large image

Please see the code in JPG to PDF: The image saved as rotated (Vb .NET) - support at the Imaging forum suggested I request this in this forum.

When a large image is inserted into a new PDF page, the image is resized to fit the available space on the page. This, however, is done in such a way that the height and width are resized independently, causing the image to appear “squeezed” or “stretched”.

I suggest you add a property or method to allow inserting an image while keeping the height to width ratio unchanged.

At the moment I have to do all this work myself. Some relevant snippets

                        oImage = Aspose.Imaging.Image.Load(filePath:=sTempFile)
                Dim dProportion As Double = 1
                    nPageHeight = oPDF.Pages.Item(1).PageInfo.Height - oPDF.Pages.Item(1).PageInfo.Margin.Top - oPDF.Pages.Item(1).PageInfo.Margin.Bottom
                    nPageWidth = oPDF.Pages.Item(1).PageInfo.Width - oPDF.Pages.Item(1).PageInfo.Margin.Left - oPDF.Pages.Item(1).PageInfo.Margin.Right
                    If CDbl(oImage.Width) > oImage.Height Then
                        If (CDbl(oImage.Width) <= nPageWidth) And (CDbl(oImage.Height) <= nPageHeight) Then
                            'no need to do anything. Image will fit fine "as is"
                        Else
                            oImage.RotateFlip(Aspose.Imaging.RotateFlipType.Rotate90FlipNone)
                        End If
                    End If
                    If nPageHeight < CDbl(oImage.Height) Then
                        dProportion = nPageHeight / CDbl(oImage.Height)
                    End If
                    If nPageWidth < CDbl(oImage.Width) Then
                        If nPageWidth / CDbl(oImage.Width) < dProportion Then
                            dProportion = nPageWidth / CDbl(oImage.Width)
                        End If
                    End If
                    oStream = New IO.MemoryStream
                    oImage.Save(oStream)
                    oPDFImage = New Aspose.Pdf.Image
                    oPDFImage.ImageStream = oStream
                    If dProportion < 1 Then
                        oPDFImage.ImageScale = dProportion
                    End If
                    oPDF.Pages.Item(1).Paragraphs.Add(oPDFImage)
                    oPDF.Save(sToFileName)

It would be nice if there was a property, for example oPDFImage.KeepImageRatio with values True/False, default False to keep current functionality intact, so that I wouldn’t have to go through all that rigmarole to calculate and apply dProportion.

@rozeboosje,

Can you please share source files an generated pdf with us so that we may further investigate to help you out.

fullastarz.jpg (123.1 KB)
fullastarz3.pdf (301.7 KB)
PDFImage.zip (11.8 KB)

Please find attached:
fullastarz.jpg - the original Image file
fullastarz3.pdf - a resulting PDF file.

PDFImage.zip contains a Visual Studio Solution to illustrate the problem.

Please note that ASP.vb would contain our own code to set the Licences. Replace that with yours. You won’t be needing clsEncryption.vb then, so I have excluded both ASP.vb and clsEncryption.vb from the ZIP.

The code will produce a PDF with two pages. The first page shows what happens when I don’t calculate the ImageScale myself. As you can see, the image is stretched in both directions (vertical and horizontal) but by very different proportions so that the image appears distorted. On the second page I manually calculate an appropriate ImageScale that will allow the image to be inserted without distortion.

As you can see, though, in order to calculate the ImageScale myself I will need to use Aspose.Imaging to get the actual image Width and Height in pixels. So there are two issues here. Firstly I need to use TWO products (Aspose.Imaging as well as Aspose.PDF) to get the result I want, and secondly I need to calculate the correct ImageScale myself.

My proposal therefore is to add a new Property to the Aspose.Pdf.Image class, called KeepAspectRatio or something like that, values True/False. Default False. If the developer sets it to True, however, then instead of resizing the image as you currently do (which leads to distortion), implement a solution that resizes it more along the lines of how I illustrate it here.

@rozeboosje,

I have observed your comments. Can you please check following sample code on your end as workaround. I have also added a ticket with ID PDFNET-47522 in our issue tracking system to further investigate and if possible add property which you suggested.

FileStream mystream = new FileStream(dataDir + “download.png”, FileMode.Open);
Bitmap b = new Bitmap(mystream);
Document doc = new Document();
Page page = doc.Pages.Add();
// 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.PageInfo.Height = b.Height;
page.PageInfo.Width = b.Width;

// Create an image object
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
// Add the image into paragraphs collection of the section
page.Paragraphs.Add(image1);
// Set the image file stream
image1.ImageStream = mystream;
// Save resultant PDF file
doc.Save(dataDir + “PNG2PDF.pdf”);

1 Like

Thanks for the suggestion… which will, of course, work as you’re creating a PDF Page with the same dimensions as the image itself; but we may be restricted to a default size (e.g. Letter or A4).

For the moment our own solution will suffice, but I’ll look forward to hearing more regarding PDFNET-47522, but there is no rush. Thanks again!

@rozeboosje,

We will share good news with you soon.

1 Like