Skip to content

Text layout

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

Text layout

Paragraph wraps text to its available width. You can configure its font, alignment, spacing, and overflow policy, or append runs with different fonts.

Paragraphs and styled runs

Save as TextLayoutPdf.java:

import inspired.pdf.unbox.Align;
import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Margin;
import inspired.pdf.unbox.Overflow;
import inspired.pdf.unbox.Padding;
import inspired.pdf.unbox.VAlign;

import java.nio.file.Path;

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

/** Demonstrates alignment, mixed text styles, and explicit truncation. */
public class TextLayoutPdf {
    public static void main(String[] args) {
        try (Document document = new Document()) {
            document.render(paragraph("Quarterly report", helvetica_bold(18))
                    .align(Align.CENTER)
                    .with(Margin.bottom(12)));
            document.render(paragraph("82 %", helvetica_bold(14))
                    .add(" of rated capacity is in use.", helvetica(10))
                    .with(Margin.bottom(10)));
            document.render(paragraph("First line\nSecond line\nThird line", helvetica(10))
                    .limit(2)
                    .with(Overflow.ELLIPSIS)
                    .with(Margin.bottom(10)));
            document.render(paragraph("Centered in a 50-point box", helvetica(10))
                    .withInnerHeight(50)
                    .with(Padding.of(6))
                    .align(Align.CENTER, VAlign.MIDDLE));
            document.finishTo(Path.of("text-layout.pdf"));
        }
    }
}

The third paragraph shows two lines; the second ends in an ellipsis because the third line was omitted.

Wrapping and alignment

Paragraphs wrap at whitespace and can split words that are too wide for a line. Use \n for explicit line breaks. Use margins or separate paragraphs for vertical spacing rather than relying on repeated blank lines.

Align.LEFT, CENTER, and RIGHT control horizontal placement. VAlign.TOP, MIDDLE, and BOTTOM position the text within the available text box. Vertical alignment is most visible with an explicit height, as above. withInnerHeight(...) sets the paragraph box height excluding margin; padding is applied inside that box, reducing the space for text.

add(text, font) appends a styled run to the same paragraph. Runs wrap together, share a baseline on each line, and use the tallest font on that line to determine line height. add(text) uses the paragraph's original font. Supply spaces at run boundaries where you want them: they are not inserted automatically.

Decide what happens to excess text

Policy Behavior
Overflow.CLIP Default. Omits lines beyond the line limit or available height.
Overflow.ELLIPSIS Omits excess lines and adds … to the last visible line when text was omitted.
Overflow.OVERFLOW Draws all wrapped lines, even outside the allocated height or line limit.

limit(n) requires a positive number. For clipping and ellipsis, both the line limit and the number of complete lines fitting the text box constrain output. If no complete line fits, no text is drawn.

Overflow does not reserve extra space or continue a paragraph onto another page; it can overlap following content. Prefer a suitable limit, a larger box, or several smaller paragraphs for report text.

See Fonts and encoding for missing characters and Rows and columns for controlling the width of a text block.

Clone this wiki locally