Skip to content

Collect

huzpsb edited this page Nov 8, 2026 · 2 revisions

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

hspdf collect -p pageSelection inFile [outFile]

-p is required and may repeat pages:

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

Go API

package main

import (
	"context"

	"github.com/huzpsb/hspdf/pkg/api"
	"github.com/huzpsb/hspdf/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