Skip to content

Rows and columns

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

Rows and columns

Use containers to arrange small groups of elements. A row places its children side by side; a column stacks its children vertically. Containers can be nested.

Two panels with different widths

Save as ColumnsPdf.java. The left panel receives two thirds of the available row width, and the right panel receives one third.

import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Margin;
import inspired.pdf.unbox.Padding;

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

import static inspired.pdf.unbox.Unbox.background;
import static inspired.pdf.unbox.Unbox.column;
import static inspired.pdf.unbox.Unbox.paragraph;
import static inspired.pdf.unbox.Unbox.rowStretch;
import static inspired.pdf.unbox.internal.SimpleFont.helvetica;
import static inspired.pdf.unbox.internal.SimpleFont.helvetica_bold;

/** Arranges two panels with proportional widths and matching heights. */
public class ColumnsPdf {
    public static void main(String[] args) {
        try (Document document = new Document()) {
            document.render(rowStretch(2f, 1f)
                    .add(column()
                            .with(Padding.of(10))
                            .with(background(new Color(235, 242, 248)))
                            .add(paragraph("Customer", helvetica_bold(12)))
                            .add(paragraph("Example Company\nMain Street 12\nBerlin", helvetica(10))))
                    .add(column()
                            .with(Padding.of(10))
                            .with(background(new Color(245, 245, 245)))
                            .add(paragraph("Invoice", helvetica_bold(12)))
                            .add(paragraph("No. 2026-001", helvetica(10))))
                    .with(Margin.bottom(12)));
            document.render(paragraph("Content continues below both panels.", helvetica(10)));
            document.finishTo(Path.of("columns.pdf"));
        }
    }
}

Choose the container

Factory Arrangement
row() Side-by-side children of equal width.
row(2f, 1f) Side-by-side children using proportional widths.
column() Children stacked top to bottom.
rowStretch(...) A row that supplies extra padding to shorter children so their allocated heights match.
columnStretch() A column that supplies extra padding to its last child; useful as a panel inside a stretched row.

Import these factories from inspired.pdf.unbox.Unbox. With explicit row widths, provide at least as many widths as children. Use a margin on a child when you want a visible gap between neighboring panels.

Stretching adjusts layout padding, not font sizes. Whether a custom element stretches depends on whether it honors the supplied rendering hints. For a panel whose last item should fill the added space, use columnStretch() as that panel.

Keep groups small

A container is one element for ordinary pagination. It does not split its children across pages. Use containers for addresses, summary panels, or short groups; render long sequences directly on the document or use a table.

Rendering can change layout hints. Build fresh child objects for independent layouts instead of sharing a mutable element between containers.

See Styling and custom drawing for panel borders and Page layout and units for spacing.

Clone this wiki locally