We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Scaling images in PDF to page size

Thread ID:





72915 Apr 10,2008 07:19 PM UTC Sep 4,2014 09:01 PM UTC ASP.NET Web Forms (Classic) 7
Tags: PDF
Justin Saraceno
Asked On April 10, 2008 07:19 PM UTC


I have a large .jpg image that I can successfully insert in to a PDF. However, the image is so large that I'm only seeing the top left corner of it.

Is there a way to scale the image down to the size of the PDF page? The sample code I'm using is:


Bhuvaneswari P [Syncfusion]
Replied On April 11, 2008 05:17 AM UTC

Hi Justin,

Thank you for your interest in Syncfusion products.

Yes, it is possible to scale the image to PDF page size, by specifying the image Height and Width in the Draw image method. Please refer the below code snippet to do so:


PdfBitmap image = new PdfBitmap(gifImage);
//Draw the image to the size of the PDF page
g.DrawImage(image, 0, 0, page.Graphics.ClientSize.Width, page.Graphics.ClientSize.Height);

Here is the sample for your reference:


Please take a look into the sample and let us know if this helps you.


Justin Saraceno
Replied On April 11, 2008 03:07 PM UTC

Thank you but maybe I need to be clearer on what I was asking.

If the image is small enough that it doesn't exceed the page area then I want to leave its size alone.

If the image is too large for the page area, then I want to shrink it to fit the page but keep its origional aspect ratio.

The sample above stretches out every image to fit an entire page and does not maintain aspect ratio.


Bhuvaneswari P [Syncfusion]
Replied On April 24, 2008 05:10 AM UTC

Hi Justin,

You can draw image depending upon the size by getting the height of the image and check if the image height is greater than page height and then you can draw image accordingly. If the image is too large for the page area then you can get the page size using GetClientSize() method and draw the image to fit the page. If the image is small enough then you can render the image just by specifying the position. Please use below code snippet to achieve this behavior:

//Check image height is greater than page height
if (image.Height > page.Graphics.ClientSize.Height)
g.DrawImage(image, 0, 0);

Please refer the sample in the below link which illustrates the above:


Please try this and let me know if this helps.


Pål Eilertsen
Replied On June 20, 2008 01:00 PM UTC

The beforementioned code works for me but I do not want the aspect ratio to get screwed up. Is it possible to preserve the ratio when fitting the image to the pdf page?

What I am trying to do is to convert a web page to a pdf document. The page is 900 px and using the above mentiioned code truncates the image severly.

Please help.

Sri Subhashini M [Syncfusion]
Replied On June 30, 2008 05:27 AM UTC

Hi Justin,

Sorry for the delay in getting back with you.

You can convert the webpage to PDF document with the help of the HtmlConverter class. Using the AspectRadio class we draw the image to fit the page.

// Setting Aspect radio
AspectRatio dimension = AspectRatio.None;
if (rdoHeight.Checked)
dimension = AspectRatio.KeepHeight;
else if (rdoWidth.Checked)
dimension = AspectRatio.KeepWidth;

HtmlConverter html = new HtmlConverter()

//Converting the web page as image
Image img = html.ConvertToImage(txtUrl.Text, ImageType.Metafile, (int)width, -1, dimension)
PdfMetafile metafile = (PdfMetafile)PdfImage.FromImage(img);
PdfMetafileLayoutFormat format = new PdfMetafileLayoutFormat();
format.Break = PdfLayoutBreakType.FitPage;
format.Layout = PdfLayoutType.Paginate;
doc.PageSettings.Height = img.Height;
format.SplitTextLines = false;
// Drawing the metafile image
metafile.Draw(page, new RectangleF(0, 0, pageSize.Width, -1), format);

Please do find the sample from the below specified location and let me know if this helps.


Eric Rosenblum
Replied On September 4, 2014 08:51 PM UTC

 PdfImage image = new PdfBitmap(PathToImage);

                float ftImageWidth;
                float ftImageHeight;
                float ftPageWidth;
                float ftPageHeight;

                float.TryParse(image.Width.ToString(), out ftImageWidth);
                float.TryParse(image.Height.ToString(), out ftImageHeight);
                ftPageWidth = page.Graphics.ClientSize.Width;
                ftPageHeight = page.Graphics.ClientSize.Height;

                float myWidth = image.Width;
                float myHeight = image.Height;
                float shrinkFactor;

                if (myWidth > ftPageWidth)

                    shrinkFactor = myWidth / ftPageWidth;
                    myWidth = ftPageWidth;
                    myHeight = myHeight / shrinkFactor;

                if (myHeight > ftPageHeight)
                    shrinkFactor = myHeight / ftPageHeight;
                    myHeight = ftPageHeight;
                    myWidth = myWidth / shrinkFactor;

                g.DrawImage(image, (ftPageWidth - myWidth)/2, (ftPageHeight - myHeight)/2, myWidth, myHeight);

Eric Rosenblum
Replied On September 4, 2014 09:01 PM UTC

This one is a little more cleaned up :-)

 PdfImage image = new PdfBitmap(PathToImage);

                float PageWidth = page.Graphics.ClientSize.Width;
                float PageHeight = page.Graphics.ClientSize.Height;
                float myWidth = image.Width;
                float myHeight = image.Height;

                float shrinkFactor;

                if (myWidth > PageWidth)
                    shrinkFactor = myWidth / PageWidth;
                    myWidth = PageWidth;
                    myHeight = myHeight / shrinkFactor;

                if (myHeight > PageHeight)
                    shrinkFactor = myHeight / PageHeight;
                    myHeight = PageHeight;
                    myWidth = myWidth / shrinkFactor;

                float XPosition = (PageWidth - myWidth) / 2;
                float YPosition = (PageHeight - myHeight) / 2;

                g.DrawImage(image, XPosition, YPosition, myWidth, myHeight);


This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Please sign in to access our forum

This page will automatically be redirected to the sign-in page in 10 seconds.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon