Skip to content
huzpsb edited this page Oct 25, 2026 · 1 revision

Trim

Generate a trimmed version of a PDF containing only the selected pages. (For building custom page sequences — reorder, repeat — see Collect.)

Command line

pdfcpu2 trim -p pageSelection inFile [outFile]

-p is required. Omit outFile to overwrite inFile (needs --force if it exists).

pdfcpu2 trim -p 1-12 in.pdf out.pdf
pdfcpu2 trim -p 1,3-5,l in.pdf out.pdf
cat filing.pdf | pdfcpu2 trim -p 1-12 - - > filing-trimmed.pdf

Go API

package main

import (
	"context"

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

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

	err := api.TrimFile(ctx, "in.pdf", "out.pdf", []string{"1-12"}, conf)
	if err != nil {
		panic(err)
	}
}

Reader/writer variant: api.Trim(ctx, rs, w, selectedPages, conf) streams the result to any io.Writer.

Clone this wiki locally