Skip to content

Getting started

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

Getting started

Create a PDF by opening a Document, rendering elements in order, and saving the finished result. Unbox is a Java library that you call from your application.

Add the dependency

Use Java 17 or newer. Add this dependency to your Maven project; Maven also brings in the required PDFBox dependencies:

<dependency>
    <groupId>consulting.inspired</groupId>
    <artifactId>unbox-pdf</artifactId>
    <version>0.10.1</version>
</dependency>

Create your first PDF

Save this as FirstPdf.java and run its main method from your IDE with the Maven dependency on the classpath. It creates hello.pdf in the working directory.

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

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;

/** Creates a small PDF with a heading and a paragraph. */
public class FirstPdf {
    public static void main(String[] args) {
        try (Document document = new Document()) {
            document.render(paragraph("Hello, Unbox PDF", helvetica_bold(18))
                    .with(Margin.bottom(12)));
            document.render(paragraph(
                    "This report is built from ordinary Java objects.", helvetica(11)));
            document.finishTo(Path.of("hello.pdf"));
        }
    }
}

paragraph(...) is a convenience factory from Unbox. Its result can be configured with fluent methods such as with(...), align(...), and limit(...). Each document.render(...) writes content and advances the document's position.

Save and close

Method Result and ownership
finishTo(Path) Writes a file and closes the document. Create parent directories first if needed.
finishToBytes() Returns a byte[] and closes the document. Useful for an application response or storage.
finishTo(OutputStream) Writes and closes the document. PDFBox also closes the supplied stream.
finish() Returns the finished PDDocument for lower-level access. Save it before closing the Unbox document.

Use one finishing method once. close() alone releases resources without saving or running finish listeners. The try-with-resources block also cleans up when rendering fails; closing again after finishTo(...) is harmless.

Run the repository samples

From a checkout of the main repository:

./mvnw -B -ntp -Dgpg.skip install
./mvnw -B -ntp -f samples/pom.xml package
mkdir -p samples/out

Open the separate samples/ Maven project in your IDE and run a sample's main method with the repository root as its working directory. The samples depend on the matching locally installed library and write to samples/out/.

Continue with page layout or text layout.

Clone this wiki locally