Image auto resizes in PdfPCell with iTextSharp

35,395

Solution 1

I'm using iTextSharp v4.1.2 and I get the following behavior:

Using this code, adding the image directly to the table via the AddCell method, the image is scaled up to fit the cell:

nestedTable.AddCell(image);

Using this code, adding the image to a cell, then adding the cell to the table, the image is displayed at its original size:

PdfPCell cell = new PdfPCell(image);
cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER;
nestedTable.AddCell(cell);



Have you added the image directly to the pdf document (outside the table) just to compare/double-check the image sizes?

document.add(image);



I assume that you want the image centered in the cell with some space around it. As a last resort, you can change your image. Make it a png with a transparent background, and just make sure that there is some transparent 'margin' around all the edges of your image.

EDIT

I just downloaded the v5.0.2 and I get the same results as mentioned above. I've tried it with images that are both smaller and larger than the size of the cell, and the behavior is the same; the first method scales the image, the second method does not.

EDIT

Well, apparently I have been wrong for years about the whole DPI thing when it comes to images. I can't seem to see that it makes any difference at all what the DPI of the image is.
I created a 600x400px image at three different resolutions, 72dpi, 96 dpi, and 110 dpi. Then I added each these images to a new document that was exactly 600x400.

Dim pSize As Rectangle = New Rectangle(600, 1000)
Dim document As Document = New Document(pSize, 0, 0, 0, 0)

For each of the three image files, when added to the document with

document.add(image)

they fit the document perfectly, with no differences for the different DPI settings.

Solution 2

@Stewbob's answer does work, but it's only incidently related to the methods of the table.

The thing with iTextSharp is that it will behave differently depending on which constructor you use. This will (annoyingly) scale up the image to fill the cell:

PdfPCell c = new PdfPCell();
c.Add(image);
c.setHorizontalAlignment(Element.ALIGN_CENTER); // this will be ignored

But this will leave the image at the size you set it (and allow for alignment):

PdfPCell c = new PdfPCell(image);  
c.setHorizontalAlignment(Element.ALIGN_CENTER);

I don't know exactly why this is, it's got something to do with the cell being in 'text mode' if you add the image in the constructor versus 'composite mode' if you add it later (in which case each object is supposed to look after it's own alignment).

Some more info (in Java, but still applies) http://tutorials.jenkov.com/java-itext/table.html#cell-modes

Solution 3

So if you have to mantain the size of the image in the PdfPCell you can loock at this code :

                iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(imageFilePath);

                 // Save the image width
                float width = image.Width;
                PdfPCell cell = new PdfPCell();
                cell.AddElement(image);


                // Now find the Image element in the cell and resize it
                foreach (IElement element in cell.CompositeElements)
                {
                    // The inserted image is stored in a PdfPTable, so when you find 
                    // the table element just set the table width with the image width, and lock it.
                    PdfPTable tblImg = element as PdfPTable;
                    if (tblImg != null)
                    {
                        tblImg.TotalWidth = width;
                        tblImg.LockedWidth = true;
                    }
                }

Solution 4

The function has a property to fit image. Only add a true

cell.AddElement(image,true);
Share:
35,395
Mladen Prajdic
Author by

Mladen Prajdic

SQL Server MVP. Geek with a touch of nerd :)) Author of the SSMS add-in SSMS Tools Pack. You can also follow me on Twitter

Updated on May 24, 2020

Comments

  • Mladen Prajdic
    Mladen Prajdic about 4 years

    I'm having a weird problem with images in iTextSharp library. I'm adding the image to the PdfPCell and for some reason it gets scaled up. How do i keep it to original size?

    I though that the images would be same when printed but the difference on the pic is the same on the printed version. Having to manually scale the image with ScaleXXX to get it to right seems a bit illogical and does not give a good result.

    So how do I put the image in its original size inside a PdfPCell of a table without having to scale it?

    Here's my code:

    private PdfPTable CreateTestPDF()
    {
        PdfPTable table = new PdfPTable(1);
        table.WidthPercentage = 100;
    
        Phrase phrase = new Phrase("MY TITLE", _font24Bold);
        table.AddCell(phrase);
    
        PdfPTable nestedTable = new PdfPTable(5);
        table.WidthPercentage = 100;
    
        Phrase cellText = new Phrase("cell 1", _font9BoldBlack);
        nestedTable.AddCell(cellText);
    
        cellText = new Phrase("cell 2", _font9BoldBlack);
        nestedTable.AddCell(cellText);
    
        cellText = new Phrase("cell 3", _font9BoldBlack);
        nestedTable.AddCell(cellText);
    
        iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(@"d:\MyPic.jpg");
        image.Alignment = iTextSharp.text.Image.ALIGN_CENTER;
        PdfPCell cell = new PdfPCell(image);
        cell.HorizontalAlignment = PdfPCell.ALIGN_MIDDLE;
        nestedTable.AddCell(cell);
    
        cellText = new Phrase("cell 5", _font9BoldBlack);
        nestedTable.AddCell(cellText);
    
        nestedTable.AddCell("");
    
        string articleInfo = "Test Text";
        cellText = new Phrase(articleInfo, _font8Black);
        nestedTable.AddCell(cellText);
    
        nestedTable.AddCell("");
        nestedTable.AddCell("");
        nestedTable.AddCell("");
    
        table.AddCell(nestedTable);
        SetBorderSizeForAllCells(table, iTextSharp.text.Rectangle.NO_BORDER);
        return table;
    }
    
    static BaseColor _textColor = new BaseColor(154, 154, 154);
    iTextSharp.text.Font _font8 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, _textColor);
    iTextSharp.text.Font _font8Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 8, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
    iTextSharp.text.Font _font9 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.NORMAL, _textColor);
    iTextSharp.text.Font _font9BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 9, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
    iTextSharp.text.Font _font10 = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, _textColor);
    iTextSharp.text.Font _font10Black = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);
    iTextSharp.text.Font _font10BoldBlack = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 10, iTextSharp.text.Font.BOLD, BaseColor.BLACK);
    iTextSharp.text.Font _font24Bold = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.HELVETICA, 24, iTextSharp.text.Font.BOLD, _textColor);
    
  • Mladen Prajdic
    Mladen Prajdic about 14 years
    i have tried PdfPCell cell = new PdfPCell(image); and doc.Add(image); (direct to document add) and they give the same size result. Is it possible it has something to do with DPIs?
  • Stewbob
    Stewbob about 14 years
    Yes. A lot of images, like what would be displayed in Paint.NET, are 96 dpi (or something else). iTextSharp always uses 72 dpi. I do know that if the dpi of your image is different than 72, you may get different results. I had forgotten about this, because I always create my 'for use in pdf' images at 72dpi.
  • Mladen Prajdic
    Mladen Prajdic about 14 years
    72? says here in the accepted answer it's 110: stackoverflow.com/questions/2752789/… or have i misunderstood that answer?
  • Mladen Prajdic
    Mladen Prajdic about 14 years
    i don't know anymore... whatever i try it doesn't work for me...any chance i could send you the solution and the image i have so you can try it with that? i'm totally stumped by this...
  • Tommy
    Tommy about 12 years
    That might be the strangest feature not a bug I have ever seen. Much Thanks
  • Bronek
    Bronek almost 11 years
    Which version of itextsharp or what type of variable 'cell' do you use? In my (type PdfPCell) I can only find method: AddElement(IElement element)
  • Victor Sharovatov
    Victor Sharovatov about 9 years
    Upvoting this answer because it allowed me to find a solution for the same problem. But I faced a completely different behaviour - AddElement method leaves the image at the size you set but the constructor resizes the image. I'm using iTextSharp 5.5.3.0.
  • rumblefx0
    rumblefx0 almost 8 years
    I don't have this overload either - using iTextSharp 5.5.9.