Skip to main content
Skip table of contents

Colspan and rowspan

Some table examples involving rowspan and/or colspan.


colspanrowspan

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

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

        new ColspanRowspan().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(4)).useAllAvailableWidth();
        Cell cell = new Cell().add(new Paragraph(" 1,1 "));
        table.addCell(cell);

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

        Cell cell23 = new Cell(2, 2).add(new Paragraph("multi 1,3 and 1,4"));
        table.addCell(cell23);

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

        cell = new Cell().add(new Paragraph(" 2,2 "));
        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 ColspanRowspan
    {
        public static readonly string DEST = "results/sandbox/tables/colspan_rowspan.pdf";

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

            new ColspanRowspan().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(4)).UseAllAvailableWidth();
            Cell cell = new Cell().Add(new Paragraph(" 1,1 "));
            table.AddCell(cell);

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

            Cell cell23 = new Cell(2, 2).Add(new Paragraph("multi 1,3 and 1,4"));
            table.AddCell(cell23);

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

            cell = new Cell().Add(new Paragraph(" 2,2 "));
            table.AddCell(cell);

            doc.Add(table);

            doc.Close();
        }
    }
}


rowspanabsoluteposition

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

    public static final String IMG = "./src/main/resources/img/berlin2013.jpg";

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

        new RowspanAbsolutePosition().manipulatePdf(DEST);
    }

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

        Table table1 = new Table(new float[] {150, 200, 200});

        Cell cell = new Cell(1, 2).add(new Paragraph("{Month}"));
        cell.setHorizontalAlignment(HorizontalAlignment.LEFT);

        Image img = new Image(ImageDataFactory.create(IMG));
        img.setWidth(UnitValue.createPercentValue(100));
        img.setAutoScale(true);

        Cell cell2 = new Cell(2, 1).add(img);
        Cell cell3 = new Cell(1, 2).add(new Paragraph("Mr Fname Lname"));
        cell3.setHorizontalAlignment(HorizontalAlignment.LEFT);

        table1.addCell(cell);
        table1.addCell(cell2);
        table1.addCell(cell3);

        doc.add(table1);

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

        public static readonly String IMG = "../../../resources/img/berlin2013.jpg";

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

            new RowspanAbsolutePosition().ManipulatePdf(DEST);
        }

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

            Table table1 = new Table(new float[] {150, 200, 200});

            Cell cell = new Cell(1, 2).Add(new Paragraph("{Month}"));
            cell.SetHorizontalAlignment(HorizontalAlignment.LEFT);

            Image img = new Image(ImageDataFactory.Create(IMG));
            img.SetWidth(UnitValue.CreatePercentValue(100));
            img.SetAutoScale(true);

            Cell cell2 = new Cell(2, 1).Add(img);
            Cell cell3 = new Cell(1, 2).Add(new Paragraph("Mr Fname Lname"));
            cell3.SetHorizontalAlignment(HorizontalAlignment.LEFT);

            table1.AddCell(cell);
            table1.AddCell(cell2);
            table1.AddCell(cell3);

            doc.Add(table1);

            doc.Close();
        }
    }
}


simplerowcolspan

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

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

        new SimpleRowColspan().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, 2, 2, 1}));

        Cell cell = new Cell(2, 1).add(new Paragraph("S/N"));
        table.addCell(cell);

        cell = new Cell(1, 3).add(new Paragraph("Name"));
        table.addCell(cell);

        cell = new Cell(2, 1).add(new Paragraph("Age"));
        table.addCell(cell);

        table.addCell("SURNAME");
        table.addCell("FIRST NAME");
        table.addCell("MIDDLE NAME");
        table.addCell("1");
        table.addCell("James");
        table.addCell("Fish");
        table.addCell("Stone");
        table.addCell("17");

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

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

            new SimpleRowColspan().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, 2, 2, 1}));

            Cell cell = new Cell(2, 1).Add(new Paragraph("S/N"));
            table.AddCell(cell);

            cell = new Cell(1, 3).Add(new Paragraph("Name"));
            table.AddCell(cell);

            cell = new Cell(2, 1).Add(new Paragraph("Age"));
            table.AddCell(cell);

            table.AddCell("SURNAME");
            table.AddCell("FIRST NAME");
            table.AddCell("MIDDLE NAME");
            table.AddCell("1");
            table.AddCell("James");
            table.AddCell("Fish");
            table.AddCell("Stone");
            table.AddCell("17");

            doc.Add(table);

            doc.Close();
        }
    }
}


simpletable11

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

import java.io.File;

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

    private String[][] data = {
            {"ABC123", "The descriptive text may be more than one line and the text should wrap automatically",
                    "$5.00", "10", "$50.00"},
            {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
            {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
    };

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

        new SimpleTable11().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, 1, 1, 1}));

        table.addCell(createCell("SKU", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Description", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Unit Price", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Quantity", 2, 1, TextAlignment.LEFT));
        table.addCell(createCell("Extension", 2, 1, TextAlignment.LEFT));

        for (String[] row : data) {
            table.addCell(createCell(row[0], 1, 1, TextAlignment.LEFT));
            table.addCell(createCell(row[1], 1, 1, TextAlignment.LEFT));
            table.addCell(createCell(row[2], 1, 1, TextAlignment.RIGHT));
            table.addCell(createCell(row[3], 1, 1, TextAlignment.RIGHT));
            table.addCell(createCell(row[4], 1, 1, TextAlignment.RIGHT));
        }

        table.addCell(createCell("Totals", 2, 4, TextAlignment.LEFT));
        table.addCell(createCell("$1,552.00", 2, 1, TextAlignment.RIGHT));

        doc.add(table);

        doc.close();
    }

    private static Cell createCell(String content, float borderWidth, int colspan, TextAlignment alignment) {
        Cell cell = new Cell(1, colspan).add(new Paragraph(content));
        cell.setTextAlignment(alignment);
        cell.setBorder(new SolidBorder(borderWidth));
        return cell;
    }
}

C#

C#
using System;
using System.IO;
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 SimpleTable11
    {
        public static readonly string DEST = "results/sandbox/tables/simple_table11.pdf";

        private String [][] data =
        {
            new[]
            {
                "ABC123", "The descriptive text may be more than one line and the text should wrap automatically",
                "$5.00", "10", "$50.00"
            },
            new[] {"QRS557", "Another description", "$100.00", "15", "$1,500.00"},
            new[] {"XYZ999", "Some stuff", "$1.00", "2", "$2.00"}
        };
        
        public static void Main(String[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();

            new SimpleTable11().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, 1, 1, 1}));
           
            table.AddCell(CreateCell("SKU", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Description", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Unit Price", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Quantity", 2, 1, TextAlignment.LEFT));
            table.AddCell(CreateCell("Extension", 2, 1, TextAlignment.LEFT));
            
            foreach (String[] row in data)
            {
                table.AddCell(CreateCell(row[0], 1, 1, TextAlignment.LEFT));
                table.AddCell(CreateCell(row[1], 1, 1, TextAlignment.LEFT));
                table.AddCell(CreateCell(row[2], 1, 1, TextAlignment.RIGHT));
                table.AddCell(CreateCell(row[3], 1, 1, TextAlignment.RIGHT));
                table.AddCell(CreateCell(row[4], 1, 1, TextAlignment.RIGHT));
            }

            table.AddCell(CreateCell("Totals", 2, 4, TextAlignment.LEFT));
            table.AddCell(CreateCell("$1,552.00", 2, 1, TextAlignment.RIGHT));

            doc.Add(table);

            doc.Close();
        }

        private static Cell CreateCell(String content, float borderWidth, int colspan, TextAlignment? alignment)
        {
            Cell cell = new Cell(1, colspan).Add(new Paragraph(content));
            cell.SetTextAlignment(alignment);
            cell.SetBorder(new SolidBorder(borderWidth));
            return cell;
        }
    }
}


simpletable12

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

import java.io.File;

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

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

        new SimpleTable12().manipulatePdf(DEST);
    }

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

        Table table = new Table(UnitValue.createPercentArray(8)).useAllAvailableWidth();

        // The Style instance will be passed by default always until the moment we desire to specify some custom border
        Style style = new Style();

        // Default border width value is 0.5, we set it on 1 for cells borders of the table which will
        // use the Style instance
        style.setBorder(new SolidBorder(1));

        table.addCell(createCell("Examination", 1, 2, style));
        table.addCell(createCell("Board", 1, 2, style));
        table.addCell(createCell("Month and Year of Passing", 1, 2, style));
        table.addCell(createCell("", 1, 1, new Style()
                .setBorder(Border.NO_BORDER)
                .setBorderTop(new SolidBorder(1))
                .setBorderBottom(new SolidBorder(1))
                .setBorderLeft(new SolidBorder(1))));
        table.addCell(createCell("Marks", 2, 1, new Style()
                .setBorder(Border.NO_BORDER)
                .setBorderTop(new SolidBorder(1))
                .setBorderBottom(new SolidBorder(1))
                .setBorderRight(new SolidBorder(1))));
        table.addCell(createCell("Percentage", 1, 2, style));
        table.addCell(createCell("Class / Grade", 1, 2, style));
        table.addCell(createCell("", 1, 1, new Style()));
        table.addCell(createCell("Obtained", 1, 1, style));
        table.addCell(createCell("Out of", 1, 1, style));
        table.addCell(createCell("12th / I.B. Diploma", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));
        table.addCell(createCell("Aggregate (all subjects)", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));
        table.addCell(createCell("", 1, 1, style));

        doc.add(table);

        doc.close();
    }

    private static Cell createCell(String content, int colspan, int rowspan, Style style) {
        Paragraph paragraph = new Paragraph(content)
                .setFontSize(10)
                .setTextAlignment(TextAlignment.CENTER);

        Cell cell = new Cell(rowspan, colspan).add(paragraph);
        cell.addStyle(style);

        return cell;
    }
}

C#

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

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

            new SimpleTable12().ManipulatePdf(DEST);
        }

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

            Table table = new Table(UnitValue.CreatePercentArray(8)).UseAllAvailableWidth();
            
            // The Style instance will be passed by default always until the moment we desire to specify some custom border
            Style style = new Style();
            
            // Default border width value is 0.5, we set it on 1 for cells borders of the table which will
            // use the Style instance
            style.SetBorder(new SolidBorder(1));
            
            table.AddCell(CreateCell("Examination", 1, 2, style));
            table.AddCell(CreateCell("Board", 1, 2, style));
            table.AddCell(CreateCell("Month and Year of Passing", 1, 2, style));
            table.AddCell(CreateCell("", 1, 1, new Style()
                    .SetBorder(Border.NO_BORDER)
                    .SetBorderTop(new SolidBorder(1))
                    .SetBorderBottom(new SolidBorder(1))
                    .SetBorderLeft(new SolidBorder(1))));
            table.AddCell(CreateCell("Marks", 2, 1, new Style()
                    .SetBorder(Border.NO_BORDER)
                    .SetBorderTop(new SolidBorder(1))
                    .SetBorderBottom(new SolidBorder(1))
                    .SetBorderRight(new SolidBorder(1))));
            table.AddCell(CreateCell("Percentage", 1, 2, style));
            table.AddCell(CreateCell("Class / Grade", 1, 2, style));
            table.AddCell(CreateCell("", 1, 1, new Style()));
            table.AddCell(CreateCell("Obtained", 1, 1, style));
            table.AddCell(CreateCell("Out of", 1, 1, style));
            table.AddCell(CreateCell("12th / I.B. Diploma", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));
            table.AddCell(CreateCell("Aggregate (all subjects)", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));
            table.AddCell(CreateCell("", 1, 1, style));

            doc.Add(table);

            doc.Close();
        }

        private static Cell CreateCell(string content, int colspan, int rowspan, Style style) 
        {
            Paragraph paragraph = new Paragraph(content)
                .SetFontSize(10)
                .SetTextAlignment(TextAlignment.CENTER);
            
            Cell cell = new Cell(rowspan, colspan).Add(paragraph);
            cell.AddStyle(style);
            
            return cell;
        }
    }
}


simpletable2

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

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

        new SimpleTable2().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(8)).useAllAvailableWidth();

        Cell cell = new Cell(2, 1).add(new Paragraph("hi"));
        table.addCell(cell);

        for (int i = 0; i < 14; i++) {
            table.addCell("hi");
        }

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

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

            new SimpleTable2().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(8)).UseAllAvailableWidth();
            
            Cell cell = new Cell(2, 1).Add(new Paragraph("hi"));
            table.AddCell(cell);

            for (int i = 0; i < 14; i++)
            {
                table.AddCell("hi");
            }

            doc.Add(table);

            doc.Close();
        }
    }
}


simpletable9

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

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

        new SimpleTable9().manipulatePdf(DEST);
    }

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

        doc.add(new Paragraph("With 3 columns:"));

        Table table = new Table(UnitValue.createPercentArray(new float[] {10, 10, 80}));
        table.setMarginTop(5);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Col c");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.");
        doc.add(table);

        doc.add(new Paragraph("With 2 columns:"));

        table = new Table(UnitValue.createPercentArray(2)).useAllAvailableWidth();
        table.setMarginTop(5);
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));
        table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.")));
        table.addCell("Col a");
        table.addCell("Col b");
        table.addCell("Value a");
        table.addCell("Value b");
        table.addCell(new Cell(1, 2).add(new Paragraph("Value b")));
        table.addCell(new Cell(1, 2).add(new Paragraph("This is a long description for column c. " +
                "It needs much more space hence we made sure that the third column is wider.")));

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

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

            new SimpleTable9().ManipulatePdf(DEST);
        }

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

            doc.Add(new Paragraph("With 3 columns:"));

            Table table = new Table(UnitValue.CreatePercentArray(new float[] {10, 10, 80}));
            table.SetMarginTop(5);
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Col c");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell("This is a long description for column c. "
                          + "It needs much more space hence we made sure that the third column is wider.");
            doc.Add(table);

            doc.Add(new Paragraph("With 2 columns:"));

            table = new Table(UnitValue.CreatePercentArray(2)).UseAllAvailableWidth();
            table.SetMarginTop(5);
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell(new Cell(1, 2).Add(new Paragraph("Value b")));
            table.AddCell(new Cell(1, 2).Add(new Paragraph("This is a long description for column c. "
                                                           + "It needs much more space hence we made sure that the third column is wider.")));
            table.AddCell("Col a");
            table.AddCell("Col b");
            table.AddCell("Value a");
            table.AddCell("Value b");
            table.AddCell(new Cell(1, 2).Add(new Paragraph("Value b")));
            table.AddCell(new Cell(1, 2).Add(new Paragraph("This is a long description for column c. "
                                                           + "It needs much more space hence we made sure that the third column is wider.")));

            doc.Add(table);

            doc.Close();
        }
    }
}


tablemeasurements

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

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

        new TableMeasurements().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(10));
        table.setWidth(millimetersToPoints(100));
        table.addCell(getCell(10));
        table.addCell(getCell(5));
        table.addCell(getCell(3));
        table.addCell(getCell(2));
        table.addCell(getCell(3));
        table.addCell(getCell(5));
        table.addCell(getCell(1));

        doc.add(table);

        doc.close();
    }

    private static float millimetersToPoints(float value) {
        return (value / 25.4f) * 72f;
    }

    private static Cell getCell(int cm) {
        Cell cell = new Cell(1, cm);
        Paragraph p = new Paragraph(
                String.format("%smm", 10 * cm)).setFontSize(8);
        p.setTextAlignment(TextAlignment.CENTER);
        p.setMultipliedLeading(0.5f);
        p.setMarginTop(0);
        cell.add(p);
        return cell;
    }
}

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

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

            new TableMeasurements().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(10));
            table.SetWidth(MillimetersToPoints(100));
            table.AddCell(GetCell(10));
            table.AddCell(GetCell(5));
            table.AddCell(GetCell(3));
            table.AddCell(GetCell(2));
            table.AddCell(GetCell(3));
            table.AddCell(GetCell(5));
            table.AddCell(GetCell(1));

            doc.Add(table);

            doc.Close();
        }

        private static float MillimetersToPoints(float value)
        {
            return (value / 25.4f) * 72f;
        }

        private static Cell GetCell(int cm)
        {
            Cell cell = new Cell(1, cm);
            Paragraph p = new Paragraph(String.Format("{0}mm", 10 * cm)).SetFontSize(8);
            p.SetTextAlignment(TextAlignment.CENTER);
            p.SetMultipliedLeading(0.5f);
            p.SetMarginTop(0);
            cell.Add(p);
            return cell;
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.