Skip to Content
NexPDF

Compare and optimise

Compare two PDF versions page by page, make files smaller by pruning objects and subsetting fonts, save for Fast Web View and control memory with NexPDF for Go.

English

License module

Comparing documents, linearized saving and the memory calls work without a key. Optimize and SubsetFonts need the editor module (Startup tier and above).

Compare two versions

CompareJSON(other, dpi) compares two documents page by page: whether the text changed and how much of the rendered page differs. CompareOverlayPNG(other, pageIndex, dpi) draws the differences of one page as a PNG.

type Diff struct {
	PagesA int `json:"pages_a"`
	PagesB int `json:"pages_b"`
	Pages  []struct {
		Page        int     `json:"page"`
		TextChanged bool    `json:"text_changed"`
		VisualDiff  float64 `json:"visual_diff"`
	} `json:"pages"`
}

a, err := nexpdf.Open("v1.pdf")
if err != nil {
	log.Fatal(err)
}
defer a.Close()
b, err := nexpdf.Open("v2.pdf")
if err != nil {
	log.Fatal(err)
}
defer b.Close()

s, err := a.CompareJSON(b, 72)
if err != nil {
	log.Fatal(err)
}
var diff Diff
json.Unmarshal([]byte(s), &diff)
fmt.Println(diff.PagesA, "pages before,", diff.PagesB, "after")

for _, p := range diff.Pages {
	if p.TextChanged || p.VisualDiff > 0.001 {
		fmt.Printf("page %d: %.2f%% of the pixels changed\n", p.Page+1, p.VisualDiff*100)
		overlay, _ := a.CompareOverlayPNG(b, p.Page, 110)
		os.WriteFile(fmt.Sprintf("diff-%d.png", p.Page+1), overlay, 0o644)
	}
}

To see which words changed (inserted, deleted, replaced and moved text, and an annotated PDF of the changes), use Word-level comparison.

VisualDiff is the share of differing pixels, from 0 to 1. A low resolution (72 dpi) is enough to find changed pages; the overlay resolution is clamped to 18-300 dpi.

Make files smaller

Optimize() compresses uncompressed streams and removes objects nothing refers to. SubsetFonts() replaces embedded TrueType, OpenType and CFF fonts with subsets of the glyphs the document uses. Both return a JSON report; save afterwards.

doc, err := nexpdf.Open("large.pdf")
if err != nil {
	log.Fatal(err)
}
defer doc.Close()

var opt struct {
	StreamsCompressed int `json:"streams_compressed"`
	ObjectsPruned     int `json:"objects_pruned"`
	BytesSaved        int `json:"bytes_saved"`
}
s, err := doc.Optimize()
if err != nil {
	log.Fatal(err)
}
json.Unmarshal([]byte(s), &opt)
fmt.Println(opt.StreamsCompressed, "streams compressed,", opt.ObjectsPruned, "objects pruned,",
	opt.BytesSaved, "bytes saved")

var fonts struct {
	FontsSubset int `json:"fonts_subset"`
	BytesBefore int `json:"bytes_before"`
	BytesAfter  int `json:"bytes_after"`
}
s, err = doc.SubsetFonts()
if err != nil {
	log.Fatal(err)
}
json.Unmarshal([]byte(s), &fonts)
fmt.Printf("%d fonts subset: %d -> %d bytes\n", fonts.FontsSubset, fonts.BytesBefore, fonts.BytesAfter)

doc.Save("small.pdf", false)

Fonts that may still be needed for new text are left whole and counted in fonts_skipped.

Fast Web View

Linearized files let a browser show the first page before the rest downloads:

doc.Save("web.pdf", true)

Memory

MemoryUsageBytes() estimates the memory a document uses. TrimMemory() compacts its internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. Call it in long-running services after large edits or batch work:

fmt.Println(doc.MemoryUsageBytes()/1024, "KiB")
fmt.Println(doc.TrimMemory(), "bytes reclaimed")

The document stays fully usable. Close frees everything. See Platform notes for goroutines and memory.

On this page