How to resize an Image to fit it into a PdfPCell?

Tags: tablesimage in cellimagescell widthimage sizeiText 7

These charts look like this:

Color charts

vulnerabilityDetailsTable.AddCell(new PdfPCell(img) {
    Border = PdfPCell.RIGHT_BORDER,
    BorderColor = new BaseColor(79, 129, 189),
    BorderWidth = 1,
    Padding = 5, MinimumHeight = 30, PaddingTop = 10 });
The problem is that the chart image is too big for the cell and I obtain this horrible result:

Resulting PDF

So I have the following 2 questions:

  1. Can I resize the Image size into my code or do I have to do this with an image editor? If it's possible, where do have I to do this? Right after I load the image or when I put the image into my PDF table cell?
  2. When I look at the image using my Windows Photo Viewer at 100% of the size, I see it much smaller than in the PDF. Why is this?

Posted on StackOverflow on Mar 12, 2014 by Andrea Nobili

As documented, this option doesn't scale the image (which is what you want). If you want to scale the image, you could use this:

Cell cell = new Cell();

By adding the boolean parameter true, you ask iText to scale the image. My code is written in Java, but it won't take much time to port it to C#. Just replace the lower case with the upper while using methods.

Another option is to use addCell() like this:


This will also scale the image, but use the properties of the default cell. If you don't change these properties, there will be a padding of 2 user units.

You can also change the width percentage of the image, for instance:


This line will make sure that the width of the image is 50% of the available width of the cell.

Color charts
Color charts

Click this link if you want to see how to answer this question in iText 5.