iText pdf library
Website search

Alternatives to using a PdfPTable

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

SimpleTable13.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34480476
 */
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.property.TextAlignment;
import com.itextpdf.layout.property.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();
    }
}
TableSpace.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34480476
 */
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.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.IOException;
 
public class TableSpace {
    public static final String DEST = "./target/sandbox/objects/table_space.pdf";
    public static final String FONT = "./src/test/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, true);
 
        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;
    }
}
TableTab.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
    This file is part of the iText (R) project.
    Copyright (c) 1998-2020 iText Group NV
    Authors: iText Software.
 
    For more information, please contact iText Software at this address:
    sales@itextpdf.com
 */
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34480476
 */
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.property.TabAlignment;
 
import java.io.File;
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;
    }
}
SimpleTable13.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2020 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2020 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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, true);
 
            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.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2020 iText Group NV
Authors: iText Software.
 
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
 
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;
        }
    }
}

Click the following link to see the legacy example for iText 5. Except for security fixes, iText 5 is no longer being developed.


Tags
tables tab

Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now