Compare and optimise
Compare two PDF versions page by page, make files smaller before sharing them, and control the memory a document uses in a .NET MAUI app.
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).
These calls render or rewrite whole documents. Run them off the UI thread with Task.Run, on
documents of your own rather than on view.Document while the view is rendering.
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 you can show in an Image:
using System.Text.Json.Nodes;
using Nexdevit.NexPdf;
var (changed, overlay) = await Task.Run(() =>
{
using var a = NexPdfDocument.OpenBytes(v1Bytes);
using var b = NexPdfDocument.OpenBytes(v2Bytes);
var diff = JsonNode.Parse(a.CompareJson(b, 72))!;
var pages = diff["pages"]!.AsArray()
.Where(p => p!["text_changed"]!.GetValue<bool>() || p["visual_diff"]!.GetValue<double>() > 0.001)
.Select(p => p!["page"]!.GetValue<int>())
.ToList();
var png = pages.Count > 0 ? a.CompareOverlayPng(b, pages[0], 110) : null;
return (pages, png);
});
ChangesLabel.Text = $"{changed.Count} pages changed";
if (overlay is not null)
DiffImage.Source = ImageSource.FromStream(() => new MemoryStream(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.
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. Run them before you share or upload a file:
var bytes = await Task.Run(() =>
{
using var doc = NexPdfDocument.OpenBytes(original);
var r = JsonNode.Parse(doc.Optimize())!;
System.Diagnostics.Debug.WriteLine($"{r["bytes_saved"]} bytes saved");
doc.SubsetFonts();
return doc.SaveBytes();
});
var path = Path.Combine(FileSystem.CacheDirectory, "small.pdf");
await File.WriteAllBytesAsync(path, bytes);The reports also have streams_compressed and objects_pruned (Optimize), and fonts_subset,
bytes_before, bytes_after and fonts_skipped (SubsetFonts). Fonts that may still be needed
for new text are left whole and counted in fonts_skipped.
Fast Web View
Before uploading a PDF that a browser will show, save it linearized so the first page appears before the rest downloads:
var web = doc.SaveLinearized();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. On phones,
call it after large edits to a document your app keeps open:
// doc: a NexPdfDocument your app keeps open between edits
System.Diagnostics.Debug.WriteLine($"{doc.MemoryUsageBytes() / 1024} KiB");
var freed = await Task.Run(() => doc.TrimMemory());The document stays fully usable. Disposing it frees everything. See Platform notes for threading and memory.