How to get text and image in the same cell?

Tags: tablescell eventimageswatermarkiText 5

I have create a table which has 48 images. I put the images to the right side in the cell. Now I would like to put the number of the image in the same cell at the left side.

Here is my part of code :

for (int i = x; i <= 48; i += 4)
{
    int number = i + 4;
    imageFilePath = "images/image" + number + ".jpeg";
    jpg = iTextSharp.text.Image.GetInstance(imageFilePath);
    jpg.ScaleAbsolute(166.11023622047f, 124.724409448829f);
    jpg.BorderColor = BaseColor.BLUE;
    string numberofcard = i.ToString();
    cell = new PdfPCell(jpg);
    cell.FixedHeight = 144.56692913386f;
    cell.Border = 0;
    cell.HorizontalAlignment = Element.ALIGN_RIGHT;
    cell.VerticalAlignment = Element.ALIGN_MIDDLE;
    table5.AddCell(cell);
}
How could I insert the number of the image at the left corner of cell?

Posted on StackOverflow on May 9, 2014 by NickName

You need to create a custom IPdfPCellEvent implementation.

private class MyEvent : IPdfPCellEvent {
    string number;
    public MyEvent(string number) {
         this.number = number;
    }
    public void CellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
        ColumnText.ShowTextAligned(
            canvases[PdfPTable.TEXTCANVAS],
            Element.ALIGN_LEFT,
            new Phrase(number),
            position.Left + 2, position.Top - 16, 0);
    }
}

As you can see, we add a Phrase with the content number at an absolute position. In this case: 2 user units apart from the left border of the cell and 16 user units below the top of the cell.

In your code snippet you then add:

cell.CellEvent = new my_event(n);

Where n is the string value of number. Now the number will be drawn every time a cell is rendered.