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 Android.
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
NexPdfJni.compareJson(a, b, dpi) compares two documents page by page: whether the text changed
and how much of the rendered page differs. compareOverlayPng(a, b, pageIndex, dpi) draws the
differences of one page as a PNG. Both take two document handles.
import com.nexdevit.nexpdf.NexPdfDocument
import com.nexdevit.nexpdf.NexPdfJni
import org.json.JSONObject
NexPdfDocument.openBytes(v1Bytes).use { a ->
NexPdfDocument.openBytes(v2Bytes).use { b ->
val diff = JSONObject(NexPdfJni.compareJson(a.handle(), b.handle(), 72.0))
Log.i("NexPDF", "${diff.getInt("pages_a")} pages before, ${diff.getInt("pages_b")} after")
val pages = diff.getJSONArray("pages")
for (i in 0 until pages.length()) {
val page = pages.getJSONObject(i)
val changed = page.getDouble("visual_diff")
if (page.getBoolean("text_changed") || changed > 0.001) {
val n = page.getInt("page")
Log.i("NexPDF", "page ${n + 1}: ${"%.2f".format(changed * 100)} % of the pixels changed")
val overlay = NexPdfJni.compareOverlayPng(a.handle(), b.handle(), n, 110.0)
File(cacheDir, "diff-${n + 1}.png").writeBytes(overlay)
}
}
}
}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
NexPdfJni.optimize(handle) compresses uncompressed streams and removes objects nothing refers to,
and returns a JSON report. Save afterwards.
NexPdfDocument.openBytes(largeBytes).use { doc ->
val r = JSONObject(NexPdfJni.optimize(doc.handle()))
Log.i("NexPDF", "${r.optInt("streams_compressed")} streams compressed, " +
"${r.optInt("objects_pruned")} objects pruned, ${r.optLong("bytes_saved")} bytes saved")
val small = doc.saveBytes()
}NexPdfJni.subsetFonts(handle) replaces embedded TrueType, OpenType and CFF fonts with subsets of
the glyphs the document uses. It returns a JSON report, or an empty string on failure:
val f = JSONObject(NexPdfJni.subsetFonts(doc.handle()))
Log.i("NexPDF", "${f.optInt("fonts_subset")} fonts subset: ${f.optLong("bytes_before")} -> ${f.optLong("bytes_after")} bytes")
val smaller = doc.saveBytes()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:
File(filesDir, "web.pdf").writeBytes(doc.saveLinearized())Memory
NexPdfJni.memoryUsageBytes(handle) estimates the memory a document uses.
NexPdfJni.trimMemory(handle) compacts its internal buffers and drops objects nothing refers to any
more, and returns the bytes reclaimed. Call it after large edits, and from onTrimMemory:
Log.i("NexPDF", "${NexPdfJni.memoryUsageBytes(doc.handle()) / 1024} KiB")
val freed = NexPdfJni.trimMemory(doc.handle())The document stays fully usable. Closing it frees everything. See Platform notes for threading and memory.