Skip to content

Bookmarks

huzpsb edited this page Oct 25, 2026 · 1 revision

Bookmarks

Manage the document outline (bookmarks): list, export/import as JSON, remove.

Command line

pdfcpu2 bookmarks list in.pdf                    # plain
pdfcpu2 bookmarks export in.pdf bookmarks.json   # JSON, editable
pdfcpu2 bookmarks import in.pdf bookmarks.json out.pdf
pdfcpu2 bookmarks import --replace in.pdf bookmarks.json out.pdf
pdfcpu2 bookmarks remove in.pdf out.pdf

The export JSON is a tree of bookmarks — title, destination (page, x/y/zoom or named), color, style and children. Edit it by hand or generate it programmatically, then import. Workflow:

cat product.pdf | pdfcpu2 bookmarks export - bookmarks.json
# ...edit bookmarks.json...
pdfcpu2 bookmarks import --replace product.pdf bookmarks.json product2.pdf

Tip: merge creates wrapper bookmarks automatically (-b=false to skip, --bookmark-mode preserve to keep input trees) — see Merge.

Go API

package main

import (
	"context"
	"os"

	"github.com/huzpsb/pdfcpu2/pkg/api"
	"github.com/huzpsb/pdfcpu2/pkg/pdfcpu"
	"github.com/huzpsb/pdfcpu2/pkg/pdfcpu/model"
)

func main() {
	ctx := context.Background()
	conf := model.NewDefaultConfiguration()

	// Build a bookmark tree in code (PageFrom is 1-based)
	bms := []pdfcpu.Bookmark{
		{
			Title:    "Chapter 1",
			PageFrom: 1,
			Kids: []pdfcpu.Bookmark{
				{Title: "1.1 Intro", PageFrom: 2},
			},
		},
	}

	err := api.AddBookmarksFile(ctx, "in.pdf", "out.pdf", bms, false, conf)
	if err != nil {
		panic(err)
	}

	// Export to JSON
	f, _ := os.Create("bookmarks.json")
	defer f.Close()
	in, _ := os.Open("in.pdf")
	defer in.Close()
	_ = api.ExportBookmarksJSON(ctx, in, f, "", conf)
}

Also available: api.Bookmarks (returns the parsed tree), api.ListBookmarksFile, api.ImportBookmarksFile(inPDF, inJSON, outPDF, replace, conf), api.RemoveBookmarksFile.

Clone this wiki locally