Skip to main content
Skip table of contents

Alternatives to using a PdfPTable

Examples written in answer to the question Click How to format a String resulting in a two-column display?


simpletable13

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.Border;
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 SimpleTable13 {
    public static final String DEST = "./target/sandbox/tables/simple_table13.pdf";
    public static final String[][] DATA = {
            {"John Edward Jr.", "AAA"},
            {"Pascal Einstein W. Alfi", "BBB"},
            {"St. John", "CCC"}
    };

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

        new SimpleTable13().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[] {5, 1}));
        table.setWidth(UnitValue.createPercentValue(50));
        table.setTextAlignment(TextAlignment.LEFT);

        table.addCell(new Cell().add(new Paragraph("Name: " + DATA[0][0])).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph(DATA[0][1])).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph("Surname: " + DATA[1][0])).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph(DATA[1][1])).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph("School: " + DATA[2][0])).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(new Paragraph(DATA[1][1])).setBorder(Border.NO_BORDER));

        doc.add(table);

        doc.close();
    }
}

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

        public static readonly String[][] DATA =
        {
            new[]
            {
                "John Edward Jr.", "AAA"
            },
            new[]
            {
                "Pascal Einstein W. Alfi", "BBB"
            },
            new[]
            {
                "St. John", "CCC"
            }
        };

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

            new SimpleTable13().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[] {5, 1}));
            table.SetWidth(UnitValue.CreatePercentValue(50));
            table.SetTextAlignment(TextAlignment.LEFT);
            
            table.AddCell(new Cell().Add(new Paragraph("Name: " + DATA[0][0])).SetBorder(Border.NO_BORDER));
            table.AddCell(new Cell().Add(new Paragraph(DATA[0][1])).SetBorder(Border.NO_BORDER));
            table.AddCell(new Cell().Add(new Paragraph("Surname: " + DATA[1][0])).SetBorder(Border.NO_BORDER));
            table.AddCell(new Cell().Add(new Paragraph(DATA[1][1])).SetBorder(Border.NO_BORDER));
            table.AddCell(new Cell().Add(new Paragraph("School: " + DATA[2][0])).SetBorder(Border.NO_BORDER));
            table.AddCell(new Cell().Add(new Paragraph(DATA[1][1])).SetBorder(Border.NO_BORDER));

            doc.Add(table);

            doc.Close();
        }
    }
}


tablespace

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.objects;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TableSpace {
    public static final String DEST = "./target/sandbox/objects/table_space.pdf";
    public static final String FONT = "./src/main/resources/font/PTM55FT.ttf";
    public static final String[][] DATA = {
            {"John Edward Jr.", "AAA"},
            {"Pascal Einstein W. Alfi", "BBB"},
            {"St. John", "CCC"}
    };

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

    protected void manipulatePdf(String dest) throws IOException {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
        PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.CP1250, EmbeddingStrategy.PREFER_EMBEDDED);

        doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
        doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
        doc.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));

        doc.close();
    }

    private static Paragraph createParagraphWithSpaces(PdfFont font, String value1, String value2) {
        Paragraph p = new Paragraph();
        p.setFont(font);
        p.add(String.format("%-35s", value1));
        p.add(value2);
        return p;
    }
}

C#

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

namespace iText.Samples.Sandbox.Objects
{
    public class TableSpace
    {
        public static readonly string DEST = "results/sandbox/objects/table_space.pdf";
        public static readonly string FONT = "../../../resources/font/PTM55FT.ttf";
        public static readonly string[][] DATA =
        {
            new string[] {"John Edward Jr.", "AAA"},
            new string[] {"Pascal Einstein W. Alfi", "BBB"},
            new string[] {"St. John", "CCC"}
        };

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new TableSpace().ManipulatePdf(DEST);
        }

        protected void ManipulatePdf(string dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
            Document doc = new Document(pdfDoc);
            PdfFont font = PdfFontFactory.CreateFont(FONT, PdfEncodings.CP1250, 
                PdfFontFactory.EmbeddingStrategy.PREFER_EMBEDDED);

            doc.Add(CreateParagraphWithSpaces(font, string.Format("{0}: {1}", "Name", DATA[0][0]), DATA[0][1]));
            doc.Add(CreateParagraphWithSpaces(font, string.Format("{0}: {1}", "Surname", DATA[1][0]), DATA[1][1]));
            doc.Add(CreateParagraphWithSpaces(font, string.Format("{0}: {1}", "School", DATA[2][0]), DATA[2][1]));

            doc.Close();
        }

        private static Paragraph CreateParagraphWithSpaces(PdfFont font, string value1, string value2)
        {
            Paragraph p = new Paragraph();
            p.SetFont(font);
            p.Add(string.Format("{0, -35}", value1));
            p.Add(value2);
            return p;
        }
    }
}


tabletab

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.objects;

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.properties.TabAlignment;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class TableTab {
    public static final String DEST = "./target/sandbox/objects/table_tab.pdf";
    public static final String[][] DATA = {
            {"John Edward Jr.", "AAA"},
            {"Pascal Einstein W. Alfi", "BBB"},
            {"St. John", "CCC"}
    };

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

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

        doc.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
        doc.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
        doc.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));

        doc.close();
    }

    private static Paragraph createParagraphWithTab(String key, String value1, String value2) {
        Paragraph p = new Paragraph();
        p.addTabStops(new TabStop(200f, TabAlignment.LEFT));
        p.add(key);
        p.add(value1);
        p.add(new Tab());
        p.add(value2);
        return p;
    }
}

C#

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

namespace iText.Samples.Sandbox.Objects
{
    public class TableTab
    {
        public static readonly string DEST = "results/sandbox/objects/table_tab.pdf";
        public static readonly string[][] DATA =
        {
            new string[] {"John Edward Jr.", "AAA"},
            new string[] {"Pascal Einstein W. Alfi", "BBB"},
            new string[] {"St. John", "CCC"}
        };

        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST);
            file.Directory.Create();
            new TableTab().ManipulatePdf(DEST);
        }

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

            doc.Add(CreateParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
            doc.Add(CreateParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
            doc.Add(CreateParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));

            doc.Close();
        }

        private static Paragraph CreateParagraphWithTab(string key, string value1, string value2)
        {
            Paragraph p = new Paragraph();
            p.AddTabStops(new TabStop(200f, TabAlignment.LEFT));
            p.Add(key);
            p.Add(value1);
            p.Add(new Tab());
            p.Add(value2);
            return p;
        }
    }
}
JavaScript errors detected

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

If this problem persists, please contact our support.