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.
Unfortunately, activation email could not send to your email. Please try again.

Scaling images in PDF to page size

Thread ID:

Created:

Updated:

Platform:

Replies:

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

Hello,

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:

http://www2.syncfusion.com/ASPsamples/42653_PDFMerge/main.htm


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

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:

[C#]

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:


http://websamples.syncfusion.com/samples/PDF.Windows/F72915/main.htm


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

Regards,
Bhuvana




Justin Saraceno
Replied On April 11, 2008 11:07 AM

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.

Justin


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

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,PointF.Empty,doc.Pages[0].GetClientSize());
else
g.DrawImage(image, 0, 0);


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


http://websamples.syncfusion.com//samples/PDF.Windows/ImageRender/main.htm


Please try this and let me know if this helps.

Regards,
Bhuvana


Pål Eilertsen
Replied On June 20, 2008 09:00 AM

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 01:27 AM

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.
http://www.syncfusion.com/support/user/uploads/Sample_f32fe1d7.zip

Regards,
Suba



Eric Rosenblum
Replied On September 4, 2014 04:51 PM

 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 05:01 PM

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);

CONFIRMATION

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.

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.

;