Alternatives to using a PdfPTable

Tags: tablestabiText 5

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

Files: 
/**
 * Example written by Bruno Lowagie in answer to the following question:
 * http://stackoverflow.com/questions/34480476
 */
package sandbox.tables;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import sandbox.WrapToTest;
 
@WrapToTest
public class SimpleTable13 {
    public static final String DEST = "results/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 IOException,
            DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new SimpleTable13().createPdf(DEST);
    }
    public void createPdf(String dest) throws IOException, DocumentException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream(dest));
        document.open();
        PdfPTable table = new PdfPTable(2);
        table.setWidthPercentage(50);
        table.setHorizontalAlignment(Element.ALIGN_LEFT);
        table.setWidths(new int[]{5, 1});
        table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
        table.addCell("Name: " + DATA[0][0]);
        table.addCell(DATA[0][1]);
        table.addCell("Surname: " + DATA[1][0]);
        table.addCell(DATA[1][1]);
        table.addCell("School: " + DATA[2][0]);
        table.addCell(DATA[1][1]);
        document.add(table);
        document.close();
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34480476
 */
package sandbox.objects;
 
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.TabSettings;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class TableTab {
 
    public static final String DEST = "results/objects/tab_table.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, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableTab().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws FileNotFoundException, DocumentException {
        Document document = new Document();
 
        PdfWriter.getInstance(document, new FileOutputStream(dest));
 
        document.open();
 
        document.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
        document.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
        document.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));
 
        document.close();
    }
 
    public Paragraph createParagraphWithTab(String key, String value1, String value2) {
        Paragraph p = new Paragraph();
        p.setTabSettings(new TabSettings(200f));
        p.add(key);
        p.add(value1);
        p.add(Chunk.TABBING);
        p.add(value2);
        return p;
    }
}
/**
 * Example written by Bruno Lowagie in answer to:
 * http://stackoverflow.com/questions/34480476
 */
package sandbox.objects;
 
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
import sandbox.WrapToTest;
 
@WrapToTest
public class TableSpace {
 
    public static final String DEST = "results/objects/spaces_table.pdf";
    public static final String FONT = "resources/fonts/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, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TableSpace().createPdf(DEST);
    }
 
    public void createPdf(String dest) throws DocumentException, IOException {
        Document document = new Document();
 
        PdfWriter.getInstance(document, new FileOutputStream(dest));
 
        document.open();
 
        BaseFont bf = BaseFont.createFont(FONT, BaseFont.CP1250, BaseFont.EMBEDDED);
        Font font = new Font(bf, 12);
 
        document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
        document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
        document.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));
 
        document.close();
    }
 
    public Paragraph createParagraphWithSpaces(Font font, String value1, String value2) {
        Paragraph p = new Paragraph();
        p.setFont(font);
        p.add(String.format("%-35s", value1));
        p.add(value2);
        return p;
    }
}
File nameRaw URLUpdated
SimpleTable13.javaSimpleTable13.java2015-12-28 6:49 pm
TableTab.javaTableTab.java2015-12-28 6:50 pm
TableSpace.javaTableSpace.java2015-12-28 6:50 pm
Resources: 
File nameRaw URLUpdated
PTM55FT.ttfPTM55FT.ttf2015-12-28 6:51 pm
Results: 
File nameRaw URLUpdated
cmp_simple_table13.pdfcmp_simple_table13.pdf2015-12-28 6:52 pm
cmp_tab_table.pdfcmp_tab_table.pdf2015-12-28 6:53 pm
cmp_spaces_table.pdfcmp_spaces_table.pdf2015-12-28 6:53 pm