Skip to main content
Skip table of contents

Cell and table widths

Examples that explain how to define the dimensions of a table, its columns and its cells.


columnwidthexample

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.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.DeviceGray;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
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.TextAlignment;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

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

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

        new ColumnWidthExample().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, PageSize.A4.rotate());

        float[] columnWidths = {1, 5, 5};
        Table table = new Table(UnitValue.createPercentArray(columnWidths));

        PdfFont f = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        Cell cell = new Cell(1, 3)
                .add(new Paragraph("This is a header"))
                .setFont(f)
                .setFontSize(13)
                .setFontColor(DeviceGray.WHITE)
                .setBackgroundColor(DeviceGray.BLACK)
                .setTextAlignment(TextAlignment.CENTER);

        table.addHeaderCell(cell);

        for (int i = 0; i < 2; i++) {
            Cell[] headerFooter = new Cell[]{
                    new Cell().setBackgroundColor(new DeviceGray(0.75f)).add(new Paragraph("#")),
                    new Cell().setBackgroundColor(new DeviceGray(0.75f)).add(new Paragraph("Key")),
                    new Cell().setBackgroundColor(new DeviceGray(0.75f)).add(new Paragraph("Value"))
            };

            for (Cell hfCell : headerFooter) {
                if (i == 0) {
                    table.addHeaderCell(hfCell);
                } else {
                    table.addFooterCell(hfCell);
                }
            }
        }

        for (int counter = 0; counter < 100; counter++) {
            table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph(String.valueOf(counter + 1))));
            table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("key " + (counter + 1))));
            table.addCell(new Cell().setTextAlignment(TextAlignment.CENTER).add(new Paragraph("value " + (counter + 1))));
        }

        doc.add(table);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.IO.Font.Constants;
using iText.Kernel.Colors;
using iText.Kernel.Font;
using iText.Kernel.Geom;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;

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

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

            new ColumnWidthExample().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, PageSize.A4.Rotate());

            float[] columnWidths = {1, 5, 5};
            Table table = new Table(UnitValue.CreatePercentArray(columnWidths));

            PdfFont f = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            Cell cell = new Cell(1, 3)
                .Add(new Paragraph("This is a header"))
                .SetFont(f)
                .SetFontSize(13)
                .SetFontColor(DeviceGray.WHITE)
                .SetBackgroundColor(DeviceGray.BLACK)
                .SetTextAlignment(TextAlignment.CENTER);

            table.AddHeaderCell(cell);
            
            for (int i = 0; i < 2; i++)
            {
                Cell[] headerFooter =
                {
                    new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("#")),
                    new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Key")),
                    new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Value"))
                };

                foreach (Cell hfCell in headerFooter)
                {
                    if (i == 0)
                    {
                        table.AddHeaderCell(hfCell);
                    }
                    else
                    {
                        table.AddFooterCell(hfCell);
                    }
                }
            }

            for (int counter = 0; counter < 100; counter++)
            {
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph((counter + 1).ToString())));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph("key " + (counter + 1))));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph("value " + (counter + 1))));
            }

            doc.Add(table);

            doc.Close();
        }
    }
}


fullpagetable

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.colors.DeviceRgb;
import com.itextpdf.kernel.geom.PageSize;
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.TextAlignment;

import java.io.File;

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

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

        new FullPageTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(595, 842));
        doc.setMargins(0, 0, 0, 0);

        Table table = new Table(new float[10]).useAllAvailableWidth();
        table.setMarginTop(0);
        table.setMarginBottom(0);

        // first row
        Cell cell = new Cell(1, 10).add(new Paragraph("DateRange"));
        cell.setTextAlignment(TextAlignment.CENTER);
        cell.setPadding(5);
        cell.setBackgroundColor(new DeviceRgb(140, 221, 8));
        table.addCell(cell);

        table.addCell("Calldate");
        table.addCell("Calltime");
        table.addCell("Source");
        table.addCell("DialedNo");
        table.addCell("Extension");
        table.addCell("Trunk");
        table.addCell("Duration");
        table.addCell("Calltype");
        table.addCell("Callcost");
        table.addCell("Site");

        for (int i = 0; i < 100; i++) {
            table.addCell("date" + i);
            table.addCell("time" + i);
            table.addCell("source" + i);
            table.addCell("destination" + i);
            table.addCell("extension" + i);
            table.addCell("trunk" + i);
            table.addCell("dur" + i);
            table.addCell("toc" + i);
            table.addCell("callcost" + i);
            table.addCell("Site" + i);
        }

        doc.add(table);

        doc.close();
    }
}

C#

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

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

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

            new FullPageTable().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(595, 842));
            doc.SetMargins(0, 0, 0, 0);

            Table table = new Table(new float[10]).UseAllAvailableWidth();
            table.SetMarginTop(0);
            table.SetMarginBottom(0);

            // first row
            Cell cell = new Cell(1, 10).Add(new Paragraph("DateRange"));
            cell.SetTextAlignment(TextAlignment.CENTER);
            cell.SetPadding(5);
            cell.SetBackgroundColor(new DeviceRgb(140, 221, 8));
            table.AddCell(cell);

            table.AddCell("Calldate");
            table.AddCell("Calltime");
            table.AddCell("Source");
            table.AddCell("DialedNo");
            table.AddCell("Extension");
            table.AddCell("Trunk");
            table.AddCell("Duration");
            table.AddCell("Calltype");
            table.AddCell("Callcost");
            table.AddCell("Site");

            for (int i = 0; i < 100; i++)
            {
                table.AddCell("date" + i);
                table.AddCell("time" + i);
                table.AddCell("source" + i);
                table.AddCell("destination" + i);
                table.AddCell("extension" + i);
                table.AddCell("trunk" + i);
                table.AddCell("dur" + i);
                table.AddCell("toc" + i);
                table.AddCell("callcost" + i);
                table.AddCell("Site" + i);
            }

            doc.Add(table);

            doc.Close();
        }
    }
}


rightcornertable

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.colors.ColorConstants;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.properties.HorizontalAlignment;
import com.itextpdf.layout.borders.SolidBorder;
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 java.io.File;

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

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

        new RightCornerTable().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, new PageSize(300, 300));
        doc.setMargins(0, 0, 0, 0);

        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        table.setHorizontalAlignment(HorizontalAlignment.RIGHT);
        table.setWidth(90);

        Cell cell = new Cell().add(new Paragraph(" Date").setFontColor(ColorConstants.WHITE));
        cell.setBackgroundColor(ColorConstants.BLACK);
        cell.setBorder(new SolidBorder(ColorConstants.GRAY, 2));
        table.addCell(cell);

        Cell cellTwo = new Cell().add(new Paragraph("10/01/2015"));
        cellTwo.setBorder(new SolidBorder(2));
        table.addCell(cellTwo);

        doc.add(table);

        doc.close();
    }
}

C#

C#
using System;
using System.IO;
using iText.Kernel.Colors;
using iText.Kernel.Geom;
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 RightCornerTable
    {
        public static readonly string DEST = "results/sandbox/tables/right_corner_table.pdf";

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

            new RightCornerTable().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, new PageSize(300, 300));
            doc.SetMargins(0, 0, 0, 0);

            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
            table.SetHorizontalAlignment(HorizontalAlignment.RIGHT);
            table.SetWidth(90);

            Cell cell = new Cell().Add(new Paragraph(" Date").SetFontColor(ColorConstants.WHITE));
            cell.SetBackgroundColor(ColorConstants.BLACK);
            cell.SetBorder(new SolidBorder(ColorConstants.GRAY, 2));
            table.AddCell(cell);
            
            Cell cellTwo = new Cell().Add(new Paragraph("10/01/2015"));
            cellTwo.SetBorder(new SolidBorder(2));
            table.AddCell(cellTwo);

            doc.Add(table);

            doc.Close();
        }
    }
}


simpletable3

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.geom.PageSize;
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 java.io.File;

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

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

        new SimpleTable3().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc, PageSize.A3.rotate());

        Table table = new Table(UnitValue.createPercentArray(35)).useAllAvailableWidth().setFixedLayout();
        table.setWidth(pdfDoc.getDefaultPageSize().getWidth() - 80);

        Cell contractor = new Cell(1, 5).add(new Paragraph("XXXXXXXXXXXXX"));
        table.addCell(contractor);

        Cell workType = new Cell(1, 5).add(new Paragraph("Refractory Works"));
        table.addCell(workType);

        Cell supervisor = new Cell(1, 4).add(new Paragraph("XXXXXXXXXXXXXX"));
        table.addCell(supervisor);

        Cell paySlipHead = new Cell(1, 10).add(new Paragraph("XXXXXXXXXXXXXXXX"));
        table.addCell(paySlipHead);

        Cell paySlipMonth = new Cell(1, 2).add(new Paragraph("XXXXXXX"));
        table.addCell(paySlipMonth);

        Cell blank = new Cell(1, 9).add(new Paragraph(""));
        table.addCell(blank);

        doc.add(table);

        doc.close();
    }
}

C#

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

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

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

            new SimpleTable3().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc, PageSize.A3.Rotate());

            Table table = new Table(UnitValue.CreatePercentArray(35)).UseAllAvailableWidth().SetFixedLayout();
            table.SetWidth(pdfDoc.GetDefaultPageSize().GetWidth() - 80);

            Cell contractor = new Cell(1, 5).Add(new Paragraph("XXXXXXXXXXXXX"));
            table.AddCell(contractor);

            Cell workType = new Cell(1, 5).Add(new Paragraph("Refractory Works"));
            table.AddCell(workType);

            Cell supervisor = new Cell(1, 4).Add(new Paragraph("XXXXXXXXXXXXXX"));
            table.AddCell(supervisor);

            Cell paySlipHead = new Cell(1, 10).Add(new Paragraph("XXXXXXXXXXXXXXXX"));
            table.AddCell(paySlipHead);

            Cell paySlipMonth = new Cell(1, 2).Add(new Paragraph("XXXXXXX"));
            table.AddCell(paySlipMonth);

            Cell blank = new Cell(1, 9).Add(new Paragraph(""));
            table.AddCell(blank);

            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.