views:

902

answers:

1

Hi,

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? Here's the image of the PDF at 100% and the image in its original size opened in paint.net. alt text 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);
+3  A: 

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_MIDDLE;
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.

Stewbob
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?
Mladen Prajdic
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.
Stewbob
72? says here in the accepted answer it's 110: http://stackoverflow.com/questions/2752789/how-to-calculate-the-correct-image-size-in-out-pdf-using-itextsharpor have i misunderstood that answer?
Mladen Prajdic
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...
Mladen Prajdic
Stewbob