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.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now