Repository navigation
Page layout and units
A Document places content inside an A4 page. Choose portrait or landscape when
creating it, and use document margins and padding to define the usable regions.
The page is narrowed in this order:
Page bounds → document margin → content bounds → document padding → body
Document margin is the outer page whitespace. Document padding reserves space inside it. In particular, top and bottom padding provide the header and footer regions; header and footer listeners do not enlarge them automatically.
For a paragraph or container, margin is the spacing around the element, while padding is the spacing inside it. Horizontal spacing reduces the width available for text wrapping.
Save as PageLayoutPdf.java. This creates two landscape pages with a 15 mm margin.
import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Length;
import inspired.pdf.unbox.Margin;
import inspired.pdf.unbox.Orientation;
import inspired.pdf.unbox.Padding;
import inspired.pdf.unbox.Unit;
import java.nio.file.Path;
import static inspired.pdf.unbox.Unbox.paragraph;
import static inspired.pdf.unbox.internal.SimpleFont.helvetica;
/** Demonstrates page regions, physical units, and an explicit page break. */
public class PageLayoutPdf {
public static void main(String[] args) {
try (Document document = new Document(
Orientation.LANDSCAPE,
Margin.of(Length.of(15, Unit.MM)),
Padding.of(30, 0, 24, 0))) {
document.render(paragraph("First section", helvetica(12))
.with(Padding.of(Length.of(2, Unit.MM)))
.with(Margin.bottom(Length.of(4, Unit.MM))));
document.render(paragraph("More content on the first page.", helvetica(10)));
document.addPage();
document.render(paragraph("Second section", helvetica(12)));
document.finishTo(Path.of("page-layout.pdf"));
}
}
}Four-value Margin.of(...) and Padding.of(...) arguments are in top, right,
bottom, left order. Two-value factories take vertical, horizontal spacing;
one value applies to every side.
Float-based geometry values are PDF points: 72 points equal one inch. Length
supports POINT, PICA, INCH, CM, and MM. Margins, padding, and borders have
unit-aware factories; for an API accepting only a float, convert explicitly with
Length.of(5, Unit.MM).points().
For custom drawing, Bounds(left, top, width, height) is anchored at its top edge.
PDF Y coordinates increase upward: bottom() = top() - height(), and moving down
decreases Y.
- Unbox moves an ordinary element to a new page when it does not fit the remaining body space. It does not split that element across pages.
-
document.addPage()starts a page deliberately. Calling it after finishing your content can leave a trailing blank page, so add pages before their content. -
document.getSpaceLeftOnPage()reports the body space below the current position.document.forward(points)advances that position without drawing. - Tables have their own row-based pagination; see Tables.
Keep individual paragraphs, containers, and table rows within a page's usable height. A page break does not make an oversized element smaller.
To inspect placement, add document.add(Ruler.mm()) before rendering and before
other page listeners; import inspired.pdf.unbox.Ruler. This optional millimetre
grid is drawn into the PDF, so leave it out of the final report unless wanted.
Unbox PDF wiki · Source · Examples target Unbox PDF 0.10.1 and Java 17+.
Unbox PDF
Start here
Build your content
Finish your report
Design
Project