Skip to content

Annotations

huzpsb edited this page Oct 25, 2026 · 1 revision

Annotations

List and remove page annotations — highlights, links, stamps, widgets, ink, redaction prep and the other PDF annotation types.

Command line

List

pdfcpu2 annotations list in.pdf                 # all pages
pdfcpu2 annotations list in.pdf --pages 1-2
pdfcpu2 annotations list --json in.pdf          # JSON

Output identifies each annotation by obj# and id — the handles used for surgical removal.

Remove

pdfcpu2 annotations remove in.pdf out.pdf              # remove all
pdfcpu2 annotations remove in.pdf --pages 1-10         # first 10 pages
pdfcpu2 annotations remove in.pdf 37 38                # by obj# (from list)
pdfcpu2 annotations remove in.pdf out.pdf Widget       # by type
pdfcpu2 annotations remove in.pdf Ink Widget --pages 3 # types + pages
pdfcpu2 annotations remove in.pdf out.pdf Link 30 Text someId   # type + obj# + id

Annotation types: Text, Link, FreeText, Line, Square, Circle, Polygon, PolyLine, HighLight, Underline, Squiggly, StrikeOut, Stamp, Caret, Ink, Popup, FileAttachment, Sound, Movie, Widget, Screen, PrinterMark, TrapNet, Watermark, 3D, Redact.

Redaction recipe — remove link annotations before publishing:

cat review.pdf | pdfcpu2 annotations remove - - Link > review-no-links.pdf

Go API

package main

import (
	"context"
	"fmt"
	"os"

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

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

	f, err := os.Open("in.pdf")
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// Inspect: map of page number -> annotations
	m, err := api.Annotations(ctx, f, nil, conf)
	if err != nil {
		panic(err)
	}
	for page, pgAnnots := range m {
		for _, a := range pgAnnots.Annots {
			// a.SubType, a.Contents, a.NM, a.Rect, ...
			fmt.Printf("page %d: %v\n", page, a.SubType)
		}
	}

	// Remove by type on selected pages
	err = api.RemoveAnnotationsFile(ctx, "in.pdf", "out.pdf", []string{"1-10"},
		[]string{"Link"}, nil, conf, false)
	if err != nil {
		panic(err)
	}
}

model.AnnotationRenderer implementations (model.NewTextAnnotation, model.NewLinkAnnotation, model.NewHighlightAnnotation, …) let you add annotations programmatically via api.AddAnnotationsFile(ctx, inFile, outFile, selectedPages, ann, conf, incr); incr=true writes an incremental update instead of a full rewrite.

Clone this wiki locally