Skip to content
Oliver Tigges edited this page Sep 13, 2026 · 1 revision

Tables

Use FixedColumnsTable when every row uses the same columns. FlexTable lets each row define its own column model. Tables handle page breaks between rows and repeat their header rows by default.

A table with a header and horizontal rules

Save as TablePdf.java:

import inspired.pdf.unbox.Align;
import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Padding;
import inspired.pdf.unbox.Stroke;
import inspired.pdf.unbox.base.TableModel;
import inspired.pdf.unbox.elements.FixedColumnsTable;

import java.awt.Color;
import java.nio.file.Path;

import static inspired.pdf.unbox.Unbox.background;
import static inspired.pdf.unbox.decorators.BorderDecorator.border;
import static inspired.pdf.unbox.internal.SimpleFont.helvetica_bold;

/** Creates a table with proportional columns and a repeated header. */
public class TablePdf {
    public static void main(String[] args) {
        try (Document document = new Document()) {
            TableModel model = new TableModel()
                    .add("Item", 3f)
                    .add("Quantity", 1f, Align.RIGHT)
                    .add("Amount", 1f, Align.RIGHT);
            FixedColumnsTable table = new FixedColumnsTable(model)
                    .withHeader(helvetica_bold(10), background(new Color(235, 242, 248)))
                    .withRowStroke(new Stroke(new Color(200, 210, 220), 0.5f))
                    .withColumnStroke(Stroke.none())
                    .with(border(0.5f, Color.GRAY));
            table.withCellPadding(Padding.of(6));
            table.addRow().withCells("Consulting", "2", "240.00 EUR");
            table.addRow().withCells("Support", "1", "80.00 EUR");
            document.render(table);
            document.finishTo(Path.of("table.pdf"));
        }
    }
}

withHeader(...) takes column titles from the model. withCells(...) builds text cells and applies the column alignment. Format amounts and dates in your application before adding them; Unbox does not apply currency or locale rules.

Column widths

Widths are scaled proportionally to the available table width after margins. In the example the first column is three times as wide as either numeric column.

You can also calculate widths in points. For example, TableModel.of(120f, 240f, 120f).scaleToSize(480f) leaves those widths unchanged because their sum already equals the target. If the table has a different available width, all columns scale again. Point values do not disable scaling. Leave enough room for cell padding and text; wide tables may need landscape pages or fewer columns.

Values and custom cells

  • table.addRow("Consulting", 2, 240) uses value-based default cells: numbers are right-aligned, other values use text cells, and null produces an empty cell. These defaults are distinct from the column-aware addCell/withCells path.
  • table.addRow().addCell("Total", Align.RIGHT, font) sets text alignment and font explicitly for a cell.
  • TextCell supports padding, alignment through withAlign(...), and decorators.
  • ContainerCell holds a row or column container when a cell needs several elements. See the container guide.

For predictable reuse, create a fresh model when tables need different default cell padding or custom cell prototypes; those prototypes can hold mutable state.

Lines and page breaks

withRowStroke(...) sets horizontal rules. withColumnStroke(...) sets internal vertical dividers. Stroke.none() turns an axis off. with(Stroke) sets both axes and overrides earlier axis-specific settings. An outer frame is a border decorator, as in the example; it is applied to each page's table segment.

Tables repeat headers after page breaks. Call table.repeatHeader(false) to show the header only at the start. When possible, the initial header stays with the first body row even with repetition disabled.

Each row stays on one page. An oversized body row can overflow, and repeated headers must fit the usable page height. Render a long table directly with document.render(table) so it can manage pagination; do not rely on a container to paginate a table. A table's pre-render innerHeight(...) is not its total height.

Rows with different structures

For a flexible table, the following fragment runs inside an open Document:

import inspired.pdf.unbox.base.TableModel;
import inspired.pdf.unbox.elements.FlexTable;
import inspired.pdf.unbox.elements.TableRow;

FlexTable table = new FlexTable();
table.addRow(new TableRow(TableModel.of(1f)).withCells("Section heading"));
table.addRow(new TableRow(TableModel.of(2f, 1f)).withCells("Detail", "Value"));
document.render(table);

Different row models produce different divider positions; they do not declare merged cells in a fixed grid. For a larger example, see FlexTableReport.

Clone this wiki locally