Image auto resizes in PdfPCell with iTextSharp

Posted by Mladen Prajdic on Stack Overflow See other posts from Stack Overflow or by Mladen Prajdic
Published on 2010-06-11T11:57:14Z Indexed on 2010/06/12 2:12 UTC
Read the original article Hit count: 1375

Filed under:
|
|

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);

© Stack Overflow or respective owner

Related posts about c#

Related posts about pdf-generation