Skip to main content
Skip table of contents

Cell heights

These examples were written in answer to questions such as:


cellheights

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.Property;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

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

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

        new CellHeights().manipulatePdf(DEST);
    }

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

        // 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(2)).useAllAvailableWidth();

        // a long phrase with newlines
        Paragraph p = new Paragraph("Dr. iText or:\nHow I Learned to Stop Worrying\nand Love PDF.");
        Cell cell = new Cell().add(p);

        // the phrase fits the fixed height
        table.addCell("set height (more than sufficient)");
        cell.setHeight(172);

        // In iText a cell is meant to be used only once in the table.
        // If you want to reuse it, please clone it (either including the content or not)
        table.addCell(cell.clone(true));

        // the phrase doesn't fit the fixed height
        table.addCell("set height (not sufficient)");
        cell.setHeight(36);
        table.addCell(cell.clone(true));

        // The minimum height is exceeded
        table.addCell("minimum height");
        cell = new Cell().add(new Paragraph("Dr. iText"));
        cell.setMinHeight(70);
        table.addCell(cell.clone(true));

        // the last cell that should be extended
        table.addCell("extend last row");
        cell.deleteOwnProperty(Property.MIN_HEIGHT);
        table.addCell(cell.clone(true));

        table.setExtendBottomRow(true);

        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 CellHeights
    {
        public static readonly string DEST = "results/sandbox/tables/cell_heights.pdf";

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

            new CellHeights().ManipulatePdf(DEST);
        }

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

            // 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(2)).UseAllAvailableWidth();

            // A long phrase with newlines
            Paragraph p = new Paragraph("Dr. iText or:\nHow I Learned to Stop Worrying\nand Love PDF.");
            Cell cell = new Cell().Add(p);

            // The phrase fits the fixed height
            table.AddCell("set height (more than sufficient)");
            cell.SetHeight(172);

            // In iText7 a cell is meant to be used only once in the table.
            // If you want to reuse it, please clone it (either including the content or not)
            table.AddCell(cell.Clone(true));

            // the phrase doesn't fit the fixed height
            table.AddCell("set height (not sufficient)");
            cell.SetHeight(36);
            table.AddCell(cell.Clone(true));

            // The minimum height is exceeded
            table.AddCell("minimum height");
            cell = new Cell().Add(new Paragraph("Dr. iText"));
            cell.SetMinHeight(70);
            table.AddCell(cell.Clone(true));

            // The last cell that should be extended
            table.AddCell("extend last row");
            cell.DeleteOwnProperty(Property.MIN_HEIGHT);
            table.AddCell(cell.Clone(true));

            table.SetExtendBottomRow(true);

            doc.Add(table);
            
            doc.Close();
        }
    }
}



fittableonpage

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.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.layout.LayoutArea;
import com.itextpdf.layout.layout.LayoutContext;
import com.itextpdf.layout.layout.LayoutResult;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.renderer.IRenderer;

import java.io.File;

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

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

        new FitTableOnPage().manipulatePdf(DEST);
    }

    protected void manipulatePdf(String dest) throws Exception {
        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();

        for (int i = 0; i < 10; i++) {
            Cell cell;

            if (i == 9) {
                cell = new Cell().add(new Paragraph("Two\nLines"));
            } else {
                cell = new Cell().add(new Paragraph(Integer.toString(i)));
            }

            table.addCell(cell);
        }

        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);

        // Fictitiously layout the element to find out, how much space it takes
        IRenderer tableRenderer = table.createRendererSubTree().setParent(doc.getRenderer());
        LayoutResult tableLayoutResult = tableRenderer.layout(new LayoutContext(
                new LayoutArea(0, new Rectangle(550 + 72, 1000))));

        pdfDoc.setDefaultPageSize(new PageSize(550 + 72,
                tableLayoutResult.getOccupiedArea().getBBox().getHeight() + 72));

        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.Layout;
using iText.Layout.Properties;
using iText.Layout.Renderer;

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

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

            new FitTableOnPage().ManipulatePdf(DEST);
        }

        private void ManipulatePdf(string dest)
        {
            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();

            for (int i = 0; i < 10; i++)
            {
                Cell cell;

                if (i == 9)
                {
                    cell = new Cell().Add(new Paragraph("Two\nLines"));
                }
                else
                {
                    cell = new Cell().Add(new Paragraph(i.ToString()));
                }

                table.AddCell(cell);
            }

            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);

            // Fictitiously layout the element to find out, how much space it takes
            IRenderer tableRenderer = table.CreateRendererSubTree().SetParent(doc.GetRenderer());
            LayoutResult tableLayoutResult = tableRenderer.Layout(new LayoutContext(
                new LayoutArea(0, new Rectangle(550 + 72, 1000))));

            pdfDoc.SetDefaultPageSize(new PageSize(550 + 72,
                tableLayoutResult.GetOccupiedArea().GetBBox().GetHeight() + 72));

            doc.Add(table);

            doc.Close();
        }
    }
}


fixedheightcell

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.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 FixedHeightCell {
    public static final String DEST = "./target/sandbox/tables/fixed_height_cell.pdf";

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

        new FixedHeightCell().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(5)).useAllAvailableWidth();

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

                if (r == 'D') {
                    cell.setHeight(60);
                }
                if (r == 'E') {
                    cell.setHeight(60);
                    if (c == 4) {
                        cell.setHeight(120);
                    }
                }
                if (r == 'F') {
                    cell.setMinHeight(60);

                    cell.setHeight(60);
                    if (c == 2) {
                        cell.add(new Paragraph("This cell has more content than the other cells"));
                    }
                }

                table.addCell(cell);
            }
        }

        doc.add(table);

        doc.close();
    }
}

C#

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

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

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

            new FixedHeightCell().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(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).ToString() + c));
                    
                    if (r == 'D')
                    {
                        cell.SetHeight(60);
                    }
                    if (r == 'E')
                    {
                        cell.SetHeight(60);
                        if (c == 4)
                        {
                            cell.SetHeight(120);
                        }
                    }
                    if (r == 'F')
                    {
                        cell.SetMinHeight(60);
                        cell.SetHeight(60);
                        if (c == 2)
                        {
                            cell.Add(new Paragraph("This cell has more content than the other cells"));
                        }
                    }

                    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.