This is a slightly more advanced example of using the Apache PDFBox library. It demonstrates how to add tables to PDFs using the Boxable library. Also shown is how to customize cell contents by changing cell size, font type and size, text color, line spacing, text rotation, border color and stlye, and horizontal and vertical alignment.

If you're new to PDFBox, start with the PdfBox example rather than this one.

import java.awt.Color;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import be.quodlibet.boxable.*;
import be.quodlibet.boxable.line.LineStyle;

public class SimpleTable {

    public static void main (String[] args) throws Exception {
        String outputFileName = "SimpleTable.pdf";
        if (args.length > 0)
            outputFileName = args[0];

        // Create a new font object selecting one of the PDF base fonts
        PDFont fontPlain = PDType1Font.HELVETICA;
        PDFont fontBold = PDType1Font.HELVETICA_BOLD;
        PDFont fontItalic = PDType1Font.HELVETICA_OBLIQUE;
        PDFont fontMono = PDType1Font.COURIER;

        // Create a document and add a page to it
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDRectangle.A4);
        // PDRectangle.LETTER and others are also possible
        PDRectangle rect = page.getMediaBox();
        // rect can be used to get the page width and height

        // Start a new content stream which will "hold" the to be created content
        PDPageContentStream cos = new PDPageContentStream(document, page);

        //Dummy Table
        float margin = 50;
        // starting y position is whole page height subtracted by top and bottom margin
        float yStartNewPage = page.getMediaBox().getHeight() - (2 * margin);
        // we want table across whole page width (subtracted by left and right margin ofcourse)
        float tableWidth = page.getMediaBox().getWidth() - (2 * margin);

        boolean drawContent = true;
        float yStart = yStartNewPage;
        float bottomMargin = 70;
        // y position is your coordinate of top left corner of the table
        float yPosition = 550;

        BaseTable table = new BaseTable(yPosition, yStartNewPage,
            bottomMargin, tableWidth, margin, document, page, true, drawContent);

        // the parameter is the row height
        Row<PDPage> headerRow = table.createRow(50);
        // the first parameter is the cell width
        Cell<PDPage> cell = headerRow.createCell(100"Header");
        // vertical alignment
        // border style
        cell.setTopBorderStyle(new LineStyle(Color.BLACK, 10));

        Row<PDPage> row = table.createRow(20);
        cell = row.createCell(30"black left plain");
        cell = row.createCell(70"black left bold");

        row = table.createRow(20);
        cell = row.createCell(50"red right mono");
        // horizontal alignment
        cell.setBottomBorderStyle(new LineStyle(Color.RED, 5));
        cell = row.createCell(50"green centered italic");
        cell.setBottomBorderStyle(new LineStyle(Color.GREEN, 5));

        row = table.createRow(20);
        cell = row.createCell(40"rotated");
        // rotate the text
        // long text that wraps
        cell = row.createCell(30"long text long text long text long text long text long text long text");
        // long text that wraps, with more line spacing
        cell = row.createCell(30"long text long text long text long text long text long text long text");


        float tableHeight = table.getHeaderAndDataHeight();
        System.out.println("tableHeight = "+tableHeight);

        // close the content stream 

        // Save the results and ensure that the document is properly closed:;

Boxable has a few dependencies other than PDFBox, so you're better off using a build system like Gradle. This build file does the trick with gradle run.

apply plugin: 'java'
apply plugin: 'application'

repositories {

sourceCompatibility = 1.8
targetCompatibility = 1.8

application {
    mainClassName = "SimpleTable"

dependencies {
    implementation 'com.github.dhorions:boxable:1.6'