Skip to main content
Skip table of contents

Alignment, indentation, leading and spacing in cells

Examples written in answer to questions such as:


centeredtextincell

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.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
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.properties.VerticalAlignment;

import java.io.File;

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

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

        new CenteredTextInCell().manipulatePdf(DEST);
    }

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

        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
        Paragraph para = new Paragraph("Test").setFont(font);
        para.setFixedLeading(0);
        para.setMultipliedLeading(1);

        Table table = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth();
        Cell cell = new Cell();
        cell.setMinHeight(50);
        cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
        cell.add(para);
        table.addCell(cell);

        doc.add(table);

        doc.close();
    }
}

C#

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

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

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

            new CenteredTextInCell().ManipulatePdf(DEST);
        }

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

            PdfFont font = PdfFontFactory.CreateFont(StandardFonts.HELVETICA_BOLD);
            Paragraph para = new Paragraph("Test").SetFont(font);
            para.SetFixedLeading(0);
            para.SetMultipliedLeading(1);

            Table table = new Table(UnitValue.CreatePercentArray(1)).UseAllAvailableWidth();
            Cell cell = new Cell();
            cell.SetMinHeight(50);
            cell.SetVerticalAlignment(VerticalAlignment.MIDDLE);
            cell.Add(para);
            table.AddCell(cell);

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



indenttable

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.kernel.pdf.canvas.PdfCanvas;
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.HorizontalAlignment;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

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

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

        new IndentTable().manipulatePdf(DEST);
    }

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

        PdfCanvas cb = new PdfCanvas(pdfDoc.addNewPage());
        cb.moveTo(36, 842);
        cb.lineTo(36, 0);
        cb.stroke();

        Table table = new Table(UnitValue.createPercentArray(8));
        table.setHorizontalAlignment(HorizontalAlignment.LEFT);
        table.setWidth(150);

        for (int aw = 0; aw < 16; aw++) {
            table.addCell(new Cell().add(new Paragraph("hi")));
        }

        table.setMarginLeft(25);

        doc.add(table);

        doc.close();
    }
}

C#

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

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

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

            new IndentTable().ManipulatePdf(DEST);
        }

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

            PdfCanvas cb = new PdfCanvas(pdfDoc.AddNewPage());
            cb.MoveTo(36, 842);
            cb.LineTo(36, 0);
            cb.Stroke();

            Table table = new Table(UnitValue.CreatePercentArray(8));
            table.SetHorizontalAlignment(HorizontalAlignment.LEFT);
            table.SetWidth(150);

            for (int aw = 0; aw < 16; aw++)
            {
                table.AddCell(new Cell().Add(new Paragraph("hi")));
            }

            table.SetMarginLeft(25);

            doc.Add(table);

            doc.Close();
        }
    }
}


indentationincell

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

import java.io.File;

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

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

        new IndentationInCell().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(1)).useAllAvailableWidth();

        Cell cell = new Cell().add(new Paragraph("TO:\n\n   name"));
        table.addCell(cell);

        cell = new Cell().add(new Paragraph("TO:\n\n\u00a0\u00a0\u00a0name"));
        table.addCell(cell);

        cell = new Cell();
        cell.add(new Paragraph("TO:"));
        Paragraph p = new Paragraph("name");
        p.setMarginLeft(10);
        cell.add(p);
        table.addCell(cell);

        cell = new Cell();
        cell.add(new Paragraph("TO:"));
        p = new Paragraph("name");
        p.setTextAlignment(TextAlignment.RIGHT);
        cell.add(p);
        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 IndentationInCell
    {
        public static readonly string DEST = "results/sandbox/tables/indentation_in_cell.pdf";

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

            new IndentationInCell().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(1)).UseAllAvailableWidth();
            
            Cell cell = new Cell().Add(new Paragraph("TO:\n\n   name"));
            table.AddCell(cell);

            cell = new Cell().Add(new Paragraph("TO:\n\n\u00a0\u00a0\u00a0name"));
            table.AddCell(cell);

            cell = new Cell();
            cell.Add(new Paragraph("TO:"));
            Paragraph p = new Paragraph("name");
            p.SetMarginLeft(10);
            cell.Add(p);
            table.AddCell(cell);

            cell = new Cell();
            cell.Add(new Paragraph("TO:"));
            p = new Paragraph("name");
            p.SetTextAlignment(TextAlignment.RIGHT);
            cell.Add(p);
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}


leadingincell

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

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

        new LeadingInCell().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(1));
        table.setWidth(UnitValue.createPercentValue(60));

        Cell cell = new Cell();

        Paragraph p = new Paragraph("paragraph 1: leading 16. Text to force a wrap and check the leading. Ha-ha")
                .setFixedLeading(16);
        cell.add(p);

        p = new Paragraph("paragraph 2: leading 32. Text to force a wrap and check the leading. Ha-ha")
                .setFixedLeading(32);
        cell.add(p);

        p = new Paragraph("paragraph 3: leading 10. Text to force a wrap and check the leading. Ha-ha")
                .setFixedLeading(10);
        cell.add(p);

        p = new Paragraph("paragraph 4: leading 18. Text to force a wrap and check the leading. Ha-ha")
                .setFixedLeading(18);
        cell.add(p);

        p = new Paragraph("paragraph 5: leading 40. Text to force a wrap and check the leading. Ha-ha")
                .setFixedLeading(40);
        cell.add(p);

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

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

            new LeadingInCell().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(1));
            table.SetWidth(UnitValue.CreatePercentValue(60));

            Cell cell = new Cell();
            
            Paragraph p = new Paragraph("paragraph 1: leading 16. Text to force a wrap and check the leading. Ha-ha")
                .SetFixedLeading(16);
            cell.Add(p);

            p = new Paragraph("paragraph 2: leading 32. Text to force a wrap and check the leading. Ha-ha")
                .SetFixedLeading(32);
            cell.Add(p);

            p = new Paragraph("paragraph 3: leading 10. Text to force a wrap and check the leading. Ha-ha")
                .SetFixedLeading(10);
            cell.Add(p);

            p = new Paragraph("paragraph 4: leading 18. Text to force a wrap and check the leading. Ha-ha")
                .SetFixedLeading(18);
            cell.Add(p);

            p = new Paragraph("paragraph 5: leading 40. Text to force a wrap and check the leading. Ha-ha")
                .SetFixedLeading(40);
            cell.Add(p);

            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}


simpletable4

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

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

        new SimpleTable4().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(1)).useAllAvailableWidth();

        Paragraph right = new Paragraph("This is right, because we create a paragraph with an indentation to the " +
                "left and as we are adding the paragraph in composite mode, all the properties of the paragraph are preserved.");

        right.setMarginLeft(20);

        Cell rightCell = new Cell().add(right);
        table.addCell(rightCell);

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

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

            new SimpleTable4().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(1)).UseAllAvailableWidth();
           
            Paragraph right = new Paragraph(
                "This is right, because we create a paragraph with an indentation to the left and as we are adding" +
                " the paragraph in composite mode, all the properties of the paragraph are preserved.");

            right.SetMarginLeft(20);

            Cell rightCell = new Cell().Add(right);
            table.AddCell(rightCell);

            doc.Add(table);

            doc.Close();
        }
    }
}


tablewithtab

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.Paragraph;
import com.itextpdf.layout.element.Tab;
import com.itextpdf.layout.element.TabStop;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.TabAlignment;
import com.itextpdf.layout.properties.UnitValue;

import java.io.File;

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

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

        new TableWithTab().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(1)).useAllAvailableWidth();
        Paragraph p = new Paragraph();
        p.add("Left");
        p.addTabStops(new TabStop(1000, TabAlignment.RIGHT));
        p.add(new Tab());
        p.add("Right");
        table.addCell(p);

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

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

            new TableWithTab().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(1)).UseAllAvailableWidth();
            Paragraph p = new Paragraph();
            p.Add("Left");
            p.AddTabStops(new TabStop(1000, TabAlignment.RIGHT));
            p.Add(new Tab());
            p.Add("Right");
            table.AddCell(p);

            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.