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. Image for the cookie policy date
close icon

Scaling images in PDF to page size

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


12 Replies

BP Bhuvaneswari P Syncfusion Team 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:

[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





JS Justin Saraceno 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.

Justin



BP Bhuvaneswari P Syncfusion Team 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,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



PE Pål Eilertsen 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.



SS Sri Subhashini M Syncfusion Team 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.
http://www.syncfusion.com/support/user/uploads/Sample_f32fe1d7.zip

Regards,
Suba




ER Eric Rosenblum 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);



ER Eric Rosenblum 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);


AH Amir H replied to Eric Rosenblum June 7, 2020 02:00 AM 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 was the working solution for me.

Thank you


AG Ankita Gangrade October 13, 2020 07:43 AM UTC

Hello Sir , Can you please share me your Source code 

i want to generate the pdf page size according to the image size 


GK Gowthamraj Kumar Syncfusion Team October 14, 2020 01:04 PM UTC

Hi Ankita, 

We have created a simple sample to create a pdf page based on the image size in our library. We have share the sample and output document for your reference. Kindly try the below sample in your end and let us know the result. 


Please refer the below link for more information, 

Please let us know if you need any further assistance with this. 

Regards, 
Gowthamraj K 



RB Remco Beurskens September 23, 2021 10:37 AM UTC

Is it possible to resize/scale an image using one of the PdfImage.Draw() overloads? (or resize the image before drawing it, without relying on GDI+ APIs in System.Drawing)



GK Gowthamraj Kumar Syncfusion Team September 24, 2021 03:15 PM UTC

Hi Remco, 
 
Thank you for your contacting Syncfusion support. 
 
No. It is not possible to resize/scale an image without using the DrawImage() overload in our PDF library. We can only achieve this requirement by using SkiaSharp approach to shrink/scale the Image. We have attached the sample with output for your reference, please try the below sample and let us know whether it is suites your requirement or not.

Sample: https://www.syncfusion.com/downloads/support/forum/72915/ze/ConsoleCore1656933803
Output: https://www.syncfusion.com/downloads/support/forum/72915/pd/Sample1601196958
 
 
Please let us know if you need any further assistance with this. 
 
Regards, 
Gowthamraj K 


Loader.
Live Chat Icon For mobile
Up arrow icon