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

Images

List, extract and update images embedded in a PDF — including inline images.

Command line

List

pdfcpu2 images list in.pdf

Output: one line per image with page, obj#, id, type, dimensions, colorspace, size and filters:

Page Obj# Id   Type  Width Height ColorSpace  Size
1    3    Im0  image 1268  720    DeviceRGB  1.8 MB

Extract

pdfcpu2 images extract in.pdf .          # current dir
pdfcpu2 images extract in.pdf ./images   # gallery_1_Im0.png, ...
cat gallery.pdf | pdfcpu2 images extract - ./images

Update

Replace an embedded image, keeping placement — by object number, by page + id, or auto-detected:

pdfcpu2 images update in.pdf new.png                 # auto (single image)
pdfcpu2 images update in.pdf new.png out.pdf 3       # by obj#
pdfcpu2 images update in.pdf logo.jpg out.pdf 1 Im0  # by page + id
cat gallery.pdf | pdfcpu2 images update - logo.jpg - 1 Im0 > updated.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()

	// Structured image inventory: []map[int]model.Image (page -> objNr -> image)
	ii, err := api.Images(ctx, f, nil, conf)
	if err != nil {
		panic(err)
	}
	for page, m := range ii {
		for objNr, img := range m {
			fmt.Printf("page %d obj#%d: %dx%d\n", page, objNr, img.Width, img.Height)
		}
	}

	// Extract to disk
	err = api.ExtractImagesFile(ctx, "in.pdf", "./images", nil, conf)
	if err != nil {
		panic(err)
	}

	// Update image with objNr 3
	err = api.UpdateImagesFile(ctx, "in.pdf", "new.png", "out.pdf", 3, 0, "", conf)
	_ = err
}

Raw variant: api.ExtractImagesRaw returns image objects without writing files — useful for pipelines that re-encode.

Clone this wiki locally