Skip to main content
Skip table of contents

Adding images to a table

This example was written to answer questions such as:


addoverlappingimage

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.DrawContext;
import com.itextpdf.layout.renderer.IRenderer;
import com.itextpdf.layout.renderer.TableRenderer;

import java.io.File;

public class AddOverlappingImage {
    public static final String DEST = "./target/sandbox/tables/add_overlapping_image.pdf";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new AddOverlappingImage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        // By default column width is calculated automatically for the best fit.
        // useAllAvailableWidth() method makes table use the whole page's width while placing the content.
        Table table = new Table(UnitValue.createPercentArray(5)).useAllAvailableWidth();

        for (int r = 'A'; r <= 'Z'; r++) {
            for (int c = 1; c <= 5; c++) {
                Cell cell = new Cell();
                cell.add(new Paragraph(((char) r) + String.valueOf(c)));
                table.addCell(cell);
            }
        }

        // Adds drawn on a canvas image to the table
        table.setNextRenderer(new OverlappingImageTableRenderer(table,
                ImageDataFactory.create("./src/main/resources/img/hero.jpg")));

        doc.add(table);

        doc.close();
    }


    private static class OverlappingImageTableRenderer extends TableRenderer {
        private ImageData image;

        public OverlappingImageTableRenderer(Table modelElement, ImageData img) {
            super(modelElement);
            this.image = img;
        }

        @Override
        public void drawChildren(DrawContext drawContext) {

            // Use the coordinates of the cell in the fourth row and the second column to draw the image
            Rectangle rect = rows.get(3)[1].getOccupiedAreaBBox();
            super.drawChildren(drawContext);

            drawContext.getCanvas().addImageAt(image, rect.getLeft() + 10, rect.getTop() - image.getHeight(), false);
        }

        // If a renderer overflows on the next area, iText uses #getNextRenderer() method to create a new renderer for the overflow part.
        // If #getNextRenderer() isn't overridden, the default method will be used and thus the default rather than the custom
        // renderer will be created
        @Override
        public IRenderer getNextRenderer() {
            return new OverlappingImageTableRenderer((Table) modelElement, image);
        }
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Tables
{
    public class AddOverlappingImage
    {
        public static readonly string DEST = "results/sandbox/tables/add_overlapping_image.pdf";
        
        public static void Main(String[] args) 
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new AddOverlappingImage().ManipulatePdf(DEST);
        }
        
        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            // By default column width is calculated automatically for the best fit.
            // useAllAvailableWidth() method makes table use the whole page's width while placing the content.
            Table table = new Table(UnitValue.CreatePercentArray(5)).UseAllAvailableWidth();

            for (int r = 'A'; r <= 'Z'; r++) 
            {
                for (int c = 1; c <= 5; c++)
                {
                    Cell cell = new Cell();
                    cell.Add(new Paragraph(((char)r) + c.ToString()));
                    table.AddCell(cell);
                }
            }

            // Adds drawn on a canvas image to the table
            table.SetNextRenderer(new OverlappingImageTableRenderer(table,
                ImageDataFactory.Create("../../../resources/img/hero.jpg")));

            doc.Add(table);

            doc.Close();
        }

        private class OverlappingImageTableRenderer : TableRenderer
        {
            private ImageData image;

            public OverlappingImageTableRenderer(Table modelElement, ImageData img)
                : base(modelElement)
            {
                image = img;
            }

        
        public override void DrawChildren(DrawContext drawContext)
        {

            // Use the coordinates of the cell in the fourth row and the second column to draw the image
            Rectangle rect = rows[3][1].GetOccupiedAreaBBox();
                base.DrawChildren(drawContext);

            drawContext.GetCanvas().AddImageAt(image, rect.GetLeft() + 10, rect.GetTop() - image.GetHeight(), false);
        }            
        
            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
        {
            return new OverlappingImageTableRenderer((Table)modelElement, image);
        }
    }
}

}



icondescriptiontable

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class IconDescriptionTable {
    public static final String DEST = "./target/sandbox/tables/icon_description_table.pdf";
    public static final String IMG = "./src/main/resources/img/bulb.gif";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new IconDescriptionTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(UnitValue.createPercentArray(new float[] {10, 90}));
        Image img = new Image(ImageDataFactory.create(IMG));

        // Width and height of image are set to autoscale
        table.addCell(img.setAutoScale(true));
        table.addCell("A light bulb icon");

        doc.add(table);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Tables
{
    public class IconDescriptionTable
    {
        public static readonly string DEST = "results/sandbox/tables/icon_description_table.pdf";

        public static readonly string IMG = "../../../resources/img/bulb.gif";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new IconDescriptionTable().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Table table = new Table(UnitValue.CreatePercentArray(new float[] {10, 90}));
            Image img = new Image(ImageDataFactory.Create(IMG));

            // Width and height of image are set to autoscale
            table.AddCell(img.SetAutoScale(true));
            table.AddCell("A light bulb icon");

            doc.Add(table);

            doc.Close();
        }
    }
}



imagenexttotext

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.properties.VerticalAlignment;

import java.io.File;
import java.net.MalformedURLException;

public class ImageNextToText {
    public static final String DEST = "./target/sandbox/tables/image_next_to_text.pdf";

    public static final String IMG1 = "./src/main/resources/img/javaone2013.jpg";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ImageNextToText().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(UnitValue.createPercentArray(new float[] {1, 2}));
        table.addCell(createImageCell(IMG1));
        table.addCell(createTextCell("This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));

        doc.add(table);

        doc.close();
    }

    private static Cell createImageCell(String path) throws MalformedURLException {
        Image img = new Image(ImageDataFactory.create(path));
        img.setWidth(UnitValue.createPercentValue(100));
        Cell cell = new Cell().add(img);
        cell.setBorder(Border.NO_BORDER);
        return cell;
    }

    private static Cell createTextCell(String text) {
        Cell cell = new Cell();
        Paragraph p = new Paragraph(text);
        p.setTextAlignment(TextAlignment.RIGHT);
        cell.add(p).setVerticalAlignment(VerticalAlignment.BOTTOM);
        cell.setBorder(Border.NO_BORDER);
        return cell;
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Borders;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Tables
{
    public class ImageNextToText
    {
        public static readonly string DEST = "results/sandbox/tables/image_next_to_text.pdf";

        public static readonly string IMG1 = "../../../resources/img/javaone2013.jpg";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new ImageNextToText().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Table table = new Table(UnitValue.CreatePercentArray(new float[] {1, 2}));
            table.AddCell(CreateImageCell(IMG1));
            table.AddCell(CreateTextCell(
                "This picture was taken at Java One.\nIt shows the iText crew at Java One in 2013."));

            doc.Add(table);

            doc.Close();
        }

        private static Cell CreateImageCell(string path)
        {
            Image img = new Image(ImageDataFactory.Create(path));
            img.SetWidth(UnitValue.CreatePercentValue(100));
            Cell cell = new Cell().Add(img);
            cell.SetBorder(Border.NO_BORDER);
            return cell;
        }

        private static Cell CreateTextCell(string text)
        {
            Cell cell = new Cell();
            Paragraph p = new Paragraph(text);
            p.SetTextAlignment(TextAlignment.RIGHT);
            cell.Add(p).SetVerticalAlignment(VerticalAlignment.BOTTOM);
            cell.SetBorder(Border.NO_BORDER);
            return cell;
        }
    }
}

imagesinchunkincell

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.element.Text;

import java.io.File;

public class ImagesInChunkInCell {
    public static final String DEST = "./target/sandbox/tables/images_in_chunk_in_cell.pdf";

    public static final String IMG = "./src/main/resources/img/bulb.gif";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ImagesInChunkInCell().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image image = new Image(ImageDataFactory.create(IMG));
        Table table = new Table(new float[] {120});
        Paragraph listOfDots = new Paragraph();

        for (int i = 0; i < 40; i++) {
            listOfDots.add(image);
            listOfDots.add(new Text(" "));
        }

        table.addCell(listOfDots);

        doc.add(table);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;

namespace iText.Samples.Sandbox.Tables
{
    public class ImagesInChunkInCell
    {
        public static readonly string DEST = "results/sandbox/tables/images_in_chunk_in_cell.pdf";

        public static readonly string IMG = "../../../resources/img/bulb.gif";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new ImagesInChunkInCell().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Image image = new Image(ImageDataFactory.Create(IMG));
            Table table = new Table(new float[] {120});
            Paragraph listOfDots = new Paragraph();

            for (int i = 0; i < 40; i++)
            {
                listOfDots.Add(image);
                listOfDots.Add(new Text(" "));
            }

            table.AddCell(listOfDots);

            doc.Add(table);

            doc.Close();
        }
    }
}


imagesnexttoeachother

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;
import java.net.MalformedURLException;

public class ImagesNextToEachOther {
    public static final String DEST = "./target/sandbox/tables/image_next_to_each_other.pdf";

    public static final String IMG1 = "./src/main/resources/img/javaone2013.jpg";
    public static final String IMG2 = "./src/main/resources/img/berlin2013.jpg";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new ImagesNextToEachOther().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Table table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.addCell(createImageCell(IMG1));
        table.addCell(createImageCell(IMG2));

        doc.add(table);

        doc.close();
    }

    private static Cell createImageCell(String path) throws MalformedURLException {
        Image img = new Image(ImageDataFactory.create(path));
        return new Cell().add(img.setAutoScale(true).setWidth(UnitValue.createPercentValue(100)));
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Tables
{
    public class ImagesNextToEachOther
    {
        public static readonly string DEST = "results/sandbox/tables/image_next_to_each_other.pdf";

        public static readonly string IMG1 = "../../../resources/img/javaone2013.jpg";
        public static readonly string IMG2 = "../../../resources/img/berlin2013.jpg";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new ImagesNextToEachOther().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.AddCell(CreateImageCell(IMG1));
            table.AddCell(CreateImageCell(IMG2));

            doc.Add(table);

            doc.Close();
        }

        private static Cell CreateImageCell(string path)
        {
            Image img = new Image(ImageDataFactory.Create(path));
            return new Cell().Add(img.SetAutoScale(true).SetWidth(UnitValue.CreatePercentValue(100)));
        }
    }
}

multipleimagesincell

JAVA

JAVA
$body

JAVA

JAVA
$body


multipleimagesintable

JAVA

JAVA
$body

JAVA

JAVA
$body


positioncontentincell

JAVA

JAVA
$body

JAVA

JAVA
$body


positioncontentincell2

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class MultipleImagesInCell {
    public static final String DEST
            = "./target/sandbox/tables/multiple_images_in_cell.pdf";

    public static final String IMG1
            = "./src/main/resources/img/brasil.png";

    public static final String IMG2
            = "./src/main/resources/img/dog.bmp";

    public static final String IMG3
            = "./src/main/resources/img/fox.bmp";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new MultipleImagesInCell().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        Image img1 = new Image(ImageDataFactory.create(IMG1));
        Image img2 = new Image(ImageDataFactory.create(IMG2));
        Image img3 = new Image(ImageDataFactory.create(IMG3));

        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        table.setWidth(UnitValue.createPercentValue(50));
        table.addCell("Different images, one after the other vertically:");

        Cell cell = new Cell();

        // There's no image autoscaling by default
        cell.add(img1.setAutoScale(true));
        cell.add(img2.setAutoScale(true));
        cell.add(img3.setAutoScale(true));
        table.addCell(cell);
        doc.add(table);
        doc.add(new AreaBreak());

        // In the snippet after this autoscaling is not needed
        // Notice that we do not need to create new Image instances since the images had been already flushed
        img1.setAutoScale(false);
        img2.setAutoScale(false);
        img3.setAutoScale(false);
        table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        table.addCell("Different images, one after the other vertically, but scaled:");

        cell = new Cell();
        img1.setWidth(UnitValue.createPercentValue(20));
        cell.add(img1);
        img2.setWidth(UnitValue.createPercentValue(20));
        cell.add(img2);
        img3.setWidth(UnitValue.createPercentValue(20));
        cell.add(img3);
        table.addCell(cell);

        table.addCell("Different images, one after the other horizontally:");

        // Notice that the table is not flushed yet so it's strictly forbidden to change image properties yet
        img1 = new Image(ImageDataFactory.create(IMG1));
        img2 = new Image(ImageDataFactory.create(IMG2));
        img3 = new Image(ImageDataFactory.create(IMG3));
        Paragraph p = new Paragraph();
        img1.scale(0.3f, 0.3f);
        p.add(img1);
        p.add(img2);
        p.add(img3);
        table.addCell(p);
        table.addCell("Text and images (mixed):");

        img2 = new Image(ImageDataFactory.create(IMG2));
        img3 = new Image(ImageDataFactory.create(IMG3));
        p = new Paragraph("The quick brown ");
        p.add(img3);
        p.add(" jumps over the lazy ");
        p.add(img2);
        cell = new Cell();
        cell.add(p);
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }
}

JAVA

JAVA
using System;
using System.IO;
using iText.IO.Image;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
using iText.Layout.Renderer;

namespace iText.Samples.Sandbox.Tables
{
    public class PositionContentInCell2
    {
        public static readonly string DEST = "results/sandbox/tables/position_content_in_cell2.pdf";

        public static readonly String IMG = "../../../resources/img/info.png";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new PositionContentInCell2().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(String dest)
        {
            // 1. Create a Document which contains a table:
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            Table table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            Cell cell1 = new Cell();
            Cell cell2 = new Cell();
            Cell cell3 = new Cell();
            Cell cell4 = new Cell();
            Cell cell5 = new Cell();
            Cell cell6 = new Cell();
            Cell cell7 = new Cell();
            Cell cell8 = new Cell();

            // 2. Inside that table, make each cell with specific height:
            cell1.SetHeight(50);
            cell2.SetHeight(50);
            cell3.SetHeight(50);
            cell4.SetHeight(50);
            cell5.SetHeight(50);
            cell6.SetHeight(50);
            cell7.SetHeight(50);
            cell8.SetHeight(50);
            
            Image img = new Image(ImageDataFactory.Create(IMG));
            
            // 3. Each cell has the same background image
            // 4. Add text in front of the image at specific position
            cell1.SetNextRenderer(new ImageAndPositionRenderer(cell1, 0, 1, img,
                "Top left", TextAlignment.LEFT));
            cell2.SetNextRenderer(new ImageAndPositionRenderer(cell2, 1, 1, img,
                "Top right", TextAlignment.RIGHT));
            cell3.SetNextRenderer(new ImageAndPositionRenderer(cell3, 0.5f, 1, img,
                "Top center", TextAlignment.CENTER));
            cell4.SetNextRenderer(new ImageAndPositionRenderer(cell4, 0.5f, 0, img,
                "Bottom center", TextAlignment.CENTER));
            cell5.SetNextRenderer(new ImageAndPositionRenderer(cell5, 0.5f, 0.5f,
                new Image(ImageDataFactory.Create(IMG)),
                "Middle center", TextAlignment.CENTER));
            cell6.SetNextRenderer(new ImageAndPositionRenderer(cell6, 0.5f, 0.5f,
                new Image(ImageDataFactory.Create(IMG)),
                "Middle center", TextAlignment.CENTER));
            cell7.SetNextRenderer(new ImageAndPositionRenderer(cell7, 0, 0, img,
                "Bottom left", TextAlignment.LEFT));
            cell8.SetNextRenderer(new ImageAndPositionRenderer(cell8, 1, 0, img,
                "Bottom right", TextAlignment.RIGHT));

            // Wrap it all up!
            table.AddCell(cell1);
            table.AddCell(cell2);
            table.AddCell(cell3);
            table.AddCell(cell4);
            table.AddCell(cell5);
            table.AddCell(cell6);
            table.AddCell(cell7);
            table.AddCell(cell8);
            doc.Add(table);
            
            doc.Close();
        }

        private class ImageAndPositionRenderer : CellRenderer
        {
            private Image img;

            private String content;

            private TextAlignment? alignment;

            private float wPct;

            private float hPct;

            public ImageAndPositionRenderer(Cell modelElement, float wPct, float hPct, Image img,
                String content, TextAlignment? alignment)
                : base(modelElement)
            {
                this.img = img;
                this.content = content;
                this.alignment = alignment;
                this.wPct = wPct;
                this.hPct = hPct;
            }            
            
            // If renderer overflows on the next area, iText uses getNextRender() method to create a renderer for the overflow part.
            // If getNextRenderer isn't overriden, the default method will be used and thus a default rather than custom
            // renderer will be created
            public override IRenderer GetNextRenderer()
            {
                return new ImageAndPositionRenderer((Cell) modelElement, wPct, hPct, img, content, alignment);
            }

            public override void Draw(DrawContext drawContext)
            {
                base.Draw(drawContext);
                drawContext.GetCanvas().AddXObjectFittedIntoRectangle(img.GetXObject(), GetOccupiedAreaBBox());
                drawContext.GetCanvas().Stroke();

                UnitValue fontSizeUv = GetPropertyAsUnitValue(Property.FONT_SIZE);
                float x = GetOccupiedAreaBBox().GetX() + wPct * GetOccupiedAreaBBox().GetWidth();
                float y = GetOccupiedAreaBBox().GetY() + hPct *
                          (GetOccupiedAreaBBox().GetHeight() - (fontSizeUv.IsPointValue()
                               ? fontSizeUv.GetValue()
                               : 12f) * 1.5f);
                new Canvas(drawContext.GetDocument().GetFirstPage(), drawContext.GetDocument().GetDefaultPageSize())
                    .ShowTextAligned(content, x, y, alignment);
            }
        }
    }
}


simpletable8

JAVA

JAVA
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2023 Apryse Group NV
    Authors: Apryse Software.

    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
package com.itextpdf.samples.sandbox.tables;

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

public class SimpleTable8 {
    public static final String DEST = "./target/sandbox/tables/simple_table8.pdf";

    public static final String SRC = "./src/main/resources/pdfs/";

    public static void main(String[] args) throws Exception {
        File file = new File(DEST);
        file.getParentFile().mkdirs();

        new SimpleTable8().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC + "header_test_file.pdf"));
        PdfFormXObject header = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);


        Table table = new Table(UnitValue.createPercentArray(3)).useAllAvailableWidth();
        Cell cell = new Cell(1, 3).add(new Image(header).setWidth(UnitValue.createPercentValue(100))
                .setAutoScale(true));
        table.addCell(cell);

        for (int row = 1; row <= 50; row++) {
            for (int column = 1; column <= 3; column++) {
                table.addCell(String.format("row %s, column %s", row, column));
            }
        }

        srcDoc = new PdfDocument(new PdfReader(SRC + "footer_test_file.pdf"));
        PdfFormXObject footer = srcDoc.getFirstPage().copyAsFormXObject(pdfDoc);

        cell = new Cell(1, 3).add(new Image(footer).setWidth(UnitValue.createPercentValue(100))
                .setAutoScale(true));
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }
}

JAVA

JAVA
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Xobject;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

namespace iText.Samples.Sandbox.Tables
{
    public class SimpleTable8
    {
        public static readonly string DEST = "results/sandbox/tables/simple_table8.pdf";

        public static String SRC = "../../../resources/pdfs/";

        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new SimpleTable8().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            PdfDocument srcDoc = new PdfDocument(new PdfReader(SRC + "header_test_file.pdf"));
            PdfFormXObject header = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);

            Table table = new Table(UnitValue.CreatePercentArray(3)).UseAllAvailableWidth();
            Cell cell = new Cell(1, 3).Add(new Image(header).SetWidth(UnitValue.CreatePercentValue(100))
                .SetAutoScale(true));
            table.AddCell(cell);

            for (int row = 1; row <= 50; row++)
            {
                for (int column = 1; column <= 3; column++)
                {
                    table.AddCell(String.Format("row {0}, column {1}", row, column));
                }
            }

            srcDoc = new PdfDocument(new PdfReader(SRC + "footer_test_file.pdf"));
           
            PdfFormXObject footer = srcDoc.GetFirstPage().CopyAsFormXObject(pdfDoc);

            cell = new Cell(1, 3).Add(new Image(footer).SetWidth(UnitValue.CreatePercentValue(100))
                .SetAutoScale(true));
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}

JavaScript errors detected

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

If this problem persists, please contact our support.