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 iOS (Swift).
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(b:dpi:) compares two documents page by page: whether the text changed and how much of
the rendered page differs. compareOverlayPng(b:pageIndex:dpi:) draws the differences of one page
as a PNG.
import NexPdf
let v1 = try NexPdfDocument.open(url: v1URL)
let v2 = try NexPdfDocument.open(url: v2URL)
let diff = NexPdf.json(try v1.compareJson(b: v2, dpi: 72)) as? [String: Any] ?? [:]
print(diff["pages_a"] ?? 0, "pages before,", diff["pages_b"] ?? 0, "after")
for page in diff["pages"] as? [[String: Any]] ?? [] {
let index = page["page"] as? Int ?? 0
let changed = page["visual_diff"] as? Double ?? 0
if page["text_changed"] as? Bool == true || changed > 0.001 {
print("page \(index + 1): \(String(format: "%.2f", changed * 100)) % of the pixels changed")
let overlay = try v1.compareOverlayPng(b: v2, pageIndex: index, dpi: 110)
try overlay.write(to: diffsFolder.appendingPathComponent("diff-\(index + 1).png"))
}
}To see which words changed (inserted, deleted, replaced and moved text, and an annotated PDF of the changes), use Word-level comparison.
visual_diff 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. Comparing renders every page of
both documents, so run it off the main thread.
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.
let doc = try NexPdfDocument.open(url: largeURL)
let r = NexPdf.json(try doc.optimize()) as? [String: Any] ?? [:]
print(r["streams_compressed"] ?? 0, "streams compressed,", r["objects_pruned"] ?? 0, "objects pruned,",
r["bytes_saved"] ?? 0, "bytes saved")
let f = NexPdf.json(try doc.subsetFonts()) as? [String: Any] ?? [:]
print("\(f["fonts_subset"] ?? 0) fonts subset: \(f["bytes_before"] ?? 0) -> \(f["bytes_after"] ?? 0) bytes")
try doc.write(to: smallURL)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:
try doc.saveLinearized().write(to: webURL)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 after
large edits, and when the app receives a memory warning:
print(doc.memoryUsageBytes() / 1024, "KiB")
let freed = doc.trimMemory()The document stays fully usable. Releasing the last reference frees everything. See Platform notes for threading and memory warnings.