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 Java.
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.
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nexdevit.nexpdf.NexPdfDocument;
import java.nio.file.Files;
import java.nio.file.Path;
ObjectMapper json = new ObjectMapper();
try (NexPdfDocument a = NexPdfDocument.open(Path.of("v1.pdf"));
NexPdfDocument b = NexPdfDocument.open(Path.of("v2.pdf"))) {
JsonNode diff = json.readTree(a.compareJson(b, 72));
System.out.println(diff.get("pages_a") + " pages before, " + diff.get("pages_b") + " after");
for (JsonNode page : diff.get("pages")) {
int p = page.get("page").asInt();
double changed = page.get("visual_diff").asDouble();
if (page.get("text_changed").asBoolean() || changed > 0.001) {
System.out.printf("page %d: %.2f%% of the pixels changed%n", p + 1, changed * 100);
Files.write(Path.of("diff-" + (p + 1) + ".png"), a.compareOverlayPng(b, p, 110));
}
}
}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.
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.
try (NexPdfDocument doc = NexPdfDocument.open(Path.of("large.pdf"))) {
JsonNode r = json.readTree(doc.optimize());
System.out.println(r.get("streams_compressed") + " streams compressed, "
+ r.get("objects_pruned") + " objects pruned, " + r.get("bytes_saved") + " bytes saved");
JsonNode f = json.readTree(doc.subsetFonts());
System.out.println(f.get("fonts_subset") + " fonts subset: "
+ f.get("bytes_before") + " -> " + f.get("bytes_after") + " bytes");
doc.save(Path.of("small.pdf"));
}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(Path.of("web.pdf"), true);Memory
memoryUsageBytes() estimates the native memory a document uses. trimMemory() compacts its
internal buffers and drops objects nothing refers to any more, and returns the bytes reclaimed. Call
it on long-running servers after large edits or batch work:
System.out.println(doc.memoryUsageBytes() / 1024 + " KiB");
long freed = doc.trimMemory();The document stays fully usable. Closing it frees everything. See Platform notes for threading and memory.