Adding nested and automatically numbered bookmarks, in a PDF/UA document.
Example_48.pdf — the PDF this example creates.
/*
* Example_48.java
*
* Copyright (c) 2026 PDFjet Software
* Licensed under the MIT License. See LICENSE file in the project root.
*/
package examples;
import java.io.*;
import com.pdfjet.*;
import com.pdfjet.fonts.*;
/**
* Example_48.java
*/
public class Example_48 {
public Example_48() throws Exception {
PDF pdf = new PDF(
new BufferedOutputStream(new FileOutputStream("Example_48.pdf")));
pdf.setCompliance(Compliance.PDF_UA_1);
pdf.setTitle("The structure of a PDF file");
Font f1 = new Font(pdf, IBMPlexSans.Regular);
f1.setSize(14f);
Page page = new Page(pdf, Letter.PORTRAIT);
Bookmark toc = new Bookmark(pdf);
Title title = null;
float x = 70f;
float y = 50f;
float offset = 50f;
y += 30f;
title = new Title(f1, "This is a test!", x, y);
toc.addBookmark(page, title);
title.drawOn(page);
y += 30f;
title = new Title(f1, "General", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "File Header", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "File Body", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "Cross-Reference Table", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
page = new Page(pdf, Letter.PORTRAIT);
y = 50f;
title = new Title(f1, "File Trailer", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "Incremental Updates", x, y).setOffset(offset);
Bookmark bm = toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "Hello", x, y).setOffset(offset);
bm = bm.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "World", x, y).setOffset(offset);
bm = bm.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "Yahoo!!", x, y).setOffset(offset);
bm.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "Test Test Test ...", x, y).setOffset(offset);
bm.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
bm = bm.getParent();
title = new Title(f1, "Let's see ...", x, y).setOffset(offset);
bm.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "One more item.", x, y).setOffset(offset);
toc.addBookmark(page, title).autoNumber(title.getPrefix());
title.drawOn(page);
y += 30f;
title = new Title(f1, "The End :)", x, y);
toc.addBookmark(page, title);
title.drawOn(page);
pdf.complete();
}
public static void main(String[] args) throws Exception {
long time0 = System.currentTimeMillis();
new Example_48();
long time1 = System.currentTimeMillis();
System.out.printf("Example_48 => %4d ms%n", time1 - time0);
}
} // End of Example_48.java
©2026 PDFjet Software.