Repository navigation
Styling and custom drawing
Use decorators for backgrounds and borders on normal elements. Use Canvas when
you need to draw a shape or work directly with a PDFBox content stream.
Save as StyledPdf.java:
import inspired.pdf.unbox.Border;
import inspired.pdf.unbox.Bounds;
import inspired.pdf.unbox.Document;
import inspired.pdf.unbox.Margin;
import inspired.pdf.unbox.Padding;
import inspired.pdf.unbox.elements.Canvas;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import java.awt.Color;
import java.io.IOException;
import java.nio.file.Path;
import static inspired.pdf.unbox.Unbox.background;
import static inspired.pdf.unbox.Unbox.paragraph;
import static inspired.pdf.unbox.decorators.BorderDecorator.border;
import static inspired.pdf.unbox.internal.SimpleFont.helvetica_bold;
/** Combines a rounded card with a canvas that draws a simple progress bar. */
public class StyledPdf {
public static void main(String[] args) {
Color accent = new Color(30, 90, 140);
try (Document document = new Document()) {
document.render(paragraph("Capacity used: 82 %", helvetica_bold(14))
.with(Padding.of(12))
.with(Margin.bottom(12))
.with(background(new Color(235, 242, 248), 6))
.with(border(Border.of(0.75f).withRadius(6), accent)));
document.render(new Canvas(24) {
@Override
public void paint(PDPageContentStream stream, Bounds bounds) throws IOException {
stream.saveGraphicsState();
try {
float y = bounds.bottom() + 6;
stream.setNonStrokingColor(new Color(225, 230, 235));
stream.addRect(bounds.left(), y, bounds.width(), 12);
stream.fill();
stream.setNonStrokingColor(accent);
stream.addRect(bounds.left(), y, bounds.width() * 0.82f, 12);
stream.fill();
} finally {
stream.restoreGraphicsState();
}
}
});
document.finishTo(Path.of("styled.pdf"));
}
}
}background(color) draws a plain background; background(color, radius) draws
rounded corners. border(thickness, color) draws an outline. To match rounded
corners, use the same radius for the background and Border.withRadius(...).
A rounded border requires uniform thickness on all sides.
Borders do not enlarge the element. Use padding to keep content away from the
outline and margin to separate the element from its neighbors. For an individual
edge, use a border such as Border.bottom(0.5f) with the border(Border, Color)
factory.
Paragraphs and tables order their stored decorators by drawing level; containers use insertion order. When decorating a container, add its background before its border so the background does not cover the outline.
new Canvas(24) allocates 24 points of height. paint(...) receives the canvas
bounds in PDF page coordinates and the document's current content stream. Draw
inside those bounds and let the document advance after the canvas.
PDFBox rectangles start at their lower-left corner, while Unbox bounds are
anchored at the top. Use bounds.bottom() as shown, or subtract from bounds.top()
when positioning something a measured distance below the top edge.
Save and restore graphics state around custom changes, and leave the supplied stream open. Canvas drawing does not wrap or split itself across pages.
For drawn shapes aligned with text, see the DrawnContentOnTextLine sample, which uses text metrics and baseline placement.
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