Adding an image to a PDF using iTextSharp and scale it properly

alt text

here's my code. It correctly adds the pictures I want and everything works except that the images are using their native resolution, so if the image is big it's being cropped to fit the page. Is there some way to have the picture use like a Zoom feature to stretch to fit, but also maintain the aspect ratio? There has to be something I'm missing there. :P Here's a picture to illustrate the problem:

using System; using System.IO; using iTextSharp.text; using iTextSharp.text.pdf; using System.Drawing; using System.Collections.Generic; namespace WinformsPlayground < public class PDFWrapper < public void CreatePDF(Listimages) < if (images.Count >= 1) < Document document = new Document(PageSize.LETTER); try < // step 2: // we create a writer that listens to the document // and directs a PDF-stream to a file PdfWriter.GetInstance(document, new FileStream("Chap0101.pdf", FileMode.Create)); // step 3: we open the document document.Open(); foreach (var image in images) < iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg); document.Add(pic); document.NewPage(); >> catch (DocumentException de) < Console.Error.WriteLine(de.Message); >catch (IOException ioe) < Console.Error.WriteLine(ioe.Message); >// step 5: we close the document document.Close(); > > > > 
asked Dec 1, 2010 at 14:06 delete delete

6 Answers 6

I solved it using the following:

foreach (var image in images) < iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(image, System.Drawing.Imaging.ImageFormat.Jpeg); if (pic.Height >pic.Width) < //Maximum height is 800 pixels. float percentage = 0.0f; percentage = 700 / pic.Height; pic.ScalePercent(percentage * 100); >else < //Maximum width is 600 pixels. float percentage = 0.0f; percentage = 540 / pic.Width; pic.ScalePercent(percentage * 100); >pic.Border = iTextSharp.text.Rectangle.BOX; pic.BorderColor = iTextSharp.text.BaseColor.BLACK; pic.BorderWidth = 3f; document.Add(pic); document.NewPage(); > 
answered Dec 1, 2010 at 15:22 delete delete

Personally, I use something close from fubo's solution and it works well:

image.ScaleToFit(document.PageSize); image.SetAbsolutePosition(0,0); 
answered Mar 21, 2016 at 12:10 141 1 1 silver badge 2 2 bronze badges

wORKED FOR ME, except that I changed image.ScaleToFit(document.PageSize); to image.ScaleToFit(document.PageSize.Width,document.PageSize.Height);

Commented Feb 26, 2019 at 12:40

You can try something like this:

 Image logo = Image.GetInstance("pathToTheImage") logo.ScaleAbsolute(500, 300) 
answered Dec 1, 2010 at 14:15 1,177 8 8 silver badges 9 9 bronze badges So, in your case it will be: pic.ScaleAbsolute(width, height); Commented Dec 1, 2010 at 14:16

This method is no use because it's scales it absolute. It stretches the image and distorts it. I need a way to have it get bigger while maintaining aspect ratio and keeping within the document.

Commented Dec 1, 2010 at 14:36

Here is an article about the image resolution in iTextSharp. I haven't used it. But you can try. mikesdotnetting.com/Article/87/iTextSharp-Working-with-images

Commented Dec 2, 2010 at 4:01 article posted by @Hps helped me Commented Aug 29, 2016 at 19:10
image.ScaleToFit(500f,30f); 

this method keeps the aspect ratio of the image

answered Jul 18, 2013 at 14:34 45.7k 19 19 gold badges 106 106 silver badges 138 138 bronze badges
image.SetAbsolutePosition(1,1); 
2,302 8 8 gold badges 27 27 silver badges 34 34 bronze badges answered Nov 16, 2016 at 4:08 Mạc Văn Cương Mạc Văn Cương 27 1 1 bronze badge Add some explanation with answer for how this answer help OP in fixing current issue Commented Nov 16, 2016 at 4:33

Setting only the position merely shifts the visible section of the image, it does not implicitly scale it.

Commented Nov 16, 2016 at 5:44

As iTextSharp is EOL, and has been replaced by iText 7. The answers above are correct, but a little bit out of date. There is some solution for iText 7:

// Adding usings for image creation using iText.IO.Image; using iText.Layout.Element; // Adding usings for document creation using iText.Layout; using iText.Pdfa; using iText.Kernel.Pdf; // Creating a PDF //fileName is the name of the future PDF file //filePath is the path where PDF file will be saved PdfADocument pdf = new PdfADocument( new PdfWriter(fileName), PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", new FileStream(filePath, FileMode.Open, FileAccess.Read))); //Creating a PDF document var doc = new Document(pdf); // Creating an ImageData object // imageFilePath is a path to yours image file ImageData imageData = ImageDataFactory.Create(imageFilePath); // Creating an Image object Image image = new Image(imageData); // Adding image to the document doc.Add(image);