Skip to main content
Skip table of contents

How to precisely position an image on top of a PdfPTable?

I need to precisely position an image over a table, for example:

Precise positioning of image on table

Observe that the image overlays a specific set of rows and columns: it overlays row 5 and row 13 partially, and rows 8-12 completely; it also overlays columns C and D partially.

In other words, I want to say that the top-left of the image should be in the cell C5, and 4pt below and 6pt to the right of the top-left of C5.

How do I do this? I thought I'll add the text to the table, add the table to the Document, and then query the table to get the absolute positions of the rows and columns, and then add the image to the Document at that position, perhaps in direct content mode.

But the table may split across pages, because it may have hundreds of rows. In that case, I need to add the image to the right page, and at the right position.

Posted on StackOverflow on Feb 28, 2014 by Kartick Vaddadi

I've written some sample code that uses a table event. In the AddOverlappingImage class, we create a table and declare a table event. This table event looks like this:

public class ImageContent implements PdfPTableEvent {
    protected Image content;
    public ImageContent(Image content) {
        this.content = content;
    public void tableLayout(PdfPTable table, float[][] widths,
            float[] heights, int headerRows, int rowStart,
            PdfContentByte[] canvases) {
        try {
            PdfContentByte canvas = canvases[PdfPTable.TEXTCANVAS];
            float x = widths[3][1] + 10;
            float y = heights[3] - 10 - content.getScaledHeight();
            content.setAbsolutePosition(x, y);
        } catch (DocumentException e) {
            throw new ExceptionConverter(e);

I pass an Image to the event class and in the tableLayout() method, I add the image to the 4th row and the 2nd column (the index starts counting at 0).

I hard-coded the cell position. Obviously, you should use some parameters, for instance a List of images and coordinates.

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

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.