Skip to content

Extract Content

huzpsb edited this page Oct 25, 2026 · 1 revision

Extract Content

Export the raw page content streams of a PDF — the drawing operators (Tj, cm, Do, …) behind every page. The substrate for content-level analysis, custom text mining and debugging.

Command line

pdfcpu2 extract -m content in.pdf outDir
pdfcpu2 extract -m content -p 2 in.pdf outDir
cat in.pdf | pdfcpu2 extract -m content - outDir

Writes outDir/<inBase>_content_<page>.txt.

Go API

package main

import (
	"context"
	"io"
	"os"

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

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

	f, _ := os.Open("in.pdf")
	defer f.Close()

	err := api.ExtractContent(ctx, f, nil, func(r io.Reader, pageNr int) error {
		b, _ := io.ReadAll(r)
		fmt.Printf("--- page %d ---\n%s\n", pageNr, b)
		return nil
	}, conf)
	if err != nil {
		panic(err)
	}

	// Or to disk
	_ = api.ExtractContentFile(ctx, "in.pdf", "./content", nil, conf)
}

For plain text (not operators) use Text Extraction; for page-level PDFs use Extract Pages.

Clone this wiki locally