Hi, I found that when I add images that has a width or height that exceeds the width and height of the PDF document page, they will be shrank down to the page without maintaining their original aspect ratio.
var pdfDocument = new Document();
var page = pdfDocument.Pages.Add();
page.Paragraphs.Add(new Image {File = "PNG.png"});
I have attached a sample PNG image: PNG.png (30.9 KB)
I would like to know if there’s a way to maintain the aspect ratio of the image in this case.
@leon.zhou
The API does allow you to se the size of added image in PDF and you can control that. However, in case the image size that you are trying to add is beyond the size PDF page and you also want to maintain the actual aspect ratio of the image, then you have to find the scaling factor between actual image size and size of PDF page. Based on calculated scaling factor, you can then set the image width and height w.r.t PDF page. The following example will serve the purpose for you.
static String path = @"c:\PDFTest\";
public static void AspectRatioImage()
{
try
{
var pdfDocument = new Document();
System.Drawing.Image srcimg = (System.Drawing.Image)new Bitmap(path + "PNG.png");
Aspose.Pdf.Image image = new Aspose.Pdf.Image();
image.File = path + "PNG.png";
Page page = pdfDocument.Pages.Add();
page.PageInfo.Width = 1024;
page.PageInfo.Height = 768;
page.PageInfo.Margin.Top = (0);
page.PageInfo.Margin.Bottom = (0);
page.PageInfo.Margin.Left = (0);
page.PageInfo.Margin.Right = (0);
double PDFPageScale = page.PageInfo.Width > page.PageInfo.Height ? (page.PageInfo.Height - page.PageInfo.Margin.Top - page.PageInfo.Margin.Bottom) : (page.PageInfo.Width - page.PageInfo.Margin.Right - page.PageInfo.Margin.Left);
double ImageMaxHeigtWidth = srcimg.Height > srcimg.Width ? srcimg.Height : srcimg.Width;
double ScalingFactor = PDFPageScale > ImageMaxHeigtWidth ? 1 : (PDFPageScale / ImageMaxHeigtWidth);
image.FixWidth=srcimg.Width* ScalingFactor;
image.FixHeight= srcimg.Width * ScalingFactor;
image.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Center;
image.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Center;
page.Paragraphs.Add(image);
pdfDocument.Save(path + "saved.pdf");
}
catch(Exception e)
{
String s=e.StackTrace;
}
}
saved.pdf (128.6 KB)
Thanks @mudassir.fayyaz, this works perfectly for me!
@leon.zhou
It’s good to know that suggested solution has worked on your end.
This line: image.FixHeight= srcimg.Width * ScalingFactor;
Should be: image.FixHeight= srcimg.Height* ScalingFactor;
1 Like
@deisenberg
Are you facing any issue or you were already able to get it sorted out? Please share some more information with us in case there is an issue at your end while using the API. We will further proceed to assist you accordingly.
I am correcting a line of code shown above in the solution written by @mudassir.fayyaz back in March of 2021 for future developers.
It is also unnecessary to set the page width, height, margins, or alignment in order to calculating the scaling factor. The important part here is the simple scaling calculations (with the correction):
double PDFPageScale = page.PageInfo.Width > page.PageInfo.Height ? (page.PageInfo.Height - page.PageInfo.Margin.Top - page.PageInfo.Margin.Bottom) : (page.PageInfo.Width - page.PageInfo.Margin.Right - page.PageInfo.Margin.Left);
double ImageMaxHeigtWidth = srcimg.Height > srcimg.Width ? srcimg.Height : srcimg.Width;
double ScalingFactor = PDFPageScale > ImageMaxHeigtWidth ? 1 : (PDFPageScale / ImageMaxHeigtWidth);
image.FixWidth = srcimg.Width * ScalingFactor;
image.FixHeight= srcimg.Height * ScalingFactor;
@deisenberg
We really appreciate you sharing this insight for the code shared above. It would definitely help others dealing with the same scenario.