Alternatives to using a PdfPTable

Tags: tablestabiText 7

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

Files: 
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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.border.Border;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
 
import java.io.File;
 
import org.junit.experimental.categories.Category;
 
@Category(SampleTest.class)
public class SimpleTable13 extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    @Override
    protected void manipulatePdf(String dest) throws Exception {
        PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
        Document doc = new Document(pdfDoc);
 
        Table table = new Table(new float[]{5, 1});
        table.setWidthPercent(50);
        table.setTextAlignment(TextAlignment.LEFT);
        table.addCell(new Cell().add("Name: " + DATA[0][0]).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(DATA[0][1]).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("Surname: " + DATA[1][0]).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(DATA[1][1]).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add("School: " + DATA[2][0]).setBorder(Border.NO_BORDER));
        table.addCell(new Cell().add(DATA[1][1]).setBorder(Border.NO_BORDER));
        doc.add(table);
        doc.close();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TableSpace extends GenericTest {
    public static final String DEST = "./target/test/resources/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);
    }
 
    public 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;
    }
 
    public 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();
    }
}
/*
 
    This file is part of the iText (R) project.
    Copyright (c) 1998-2016 iText Group NV
 
*/
 
/**
 * 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 com.itextpdf.samples.GenericTest;
import com.itextpdf.test.annotations.type.SampleTest;
import org.junit.experimental.categories.Category;
 
import java.io.File;
import java.io.IOException;
 
@Category(SampleTest.class)
public class TableTab extends GenericTest {
    public static final String DEST = "./target/test/resources/sandbox/objects/table_tab.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 TableTab().manipulatePdf(DEST);
    }
 
    public 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;
    }
 
    public 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();
    }
}
File nameRaw URLUpdated
SimpleTable13.javaSimpleTable13.java2016-08-03 2:26 pm
TableSpace.javaTableSpace.java2016-08-03 2:36 pm
TableTab.javaTableTab.java2016-08-03 2:36 pm
Resources: 
File nameRaw URLUpdated
PTM55FT.ttfPTM55FT.ttf2016-08-03 2:42 pm
Results: 
File nameRaw URLUpdated
cmp_simple_table13.pdfcmp_simple_table13.pdf2016-08-03 2:45 pm
cmp_table_space.pdfcmp_table_space.pdf2016-08-03 2:45 pm
cmp_table_tab.pdfcmp_table_tab.pdf2016-08-03 2:45 pm