Skip to content

Collect

huzpsb edited this page Oct 25, 2026 · 1 revision

Collect

Build a new PDF from a custom page sequence — reorder, repeat, mirror. Unlike Trim (which only filters), collect honors duplicates and order.

Command line

pdfcpu2 collect -p pageSelection inFile [outFile]

-p is required and may repeat pages:

pdfcpu2 collect -p 1,3,5-7 in.pdf out.pdf     # exact sequence
pdfcpu2 collect -p l-1 in.pdf out.pdf         # reversed document
pdfcpu2 collect -p 1,1,2 in.pdf out.pdf       # repeats allowed

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.CollectFile(ctx, "in.pdf", "out.pdf", []string{"1", "3", "5-7"}, conf)
	if err != nil {
		panic(err)
	}
}

Stream variant: api.Collect(ctx, rs, w, selectedPages, conf).

To resolve a selection into page numbers without writing anything:

pages, err := api.PagesForPageCollection(pageCount, []string{"l-1"})

Clone this wiki locally