Word-level comparison
Compare the text of two PDF versions word by word in a .NET MAUI app, list inserted, deleted, replaced and moved text, and show an annotated PDF of the changes in the PdfView.
License module
Comparison works without a key (viewer module).
Compare and optimise shows which pages changed and by how many pixels. Word-level comparison tells you what text changed: words inserted, deleted, replaced and moved, with their positions on both versions.
List the changes
old.CompareTextJson(newer, optionsJson) compares the document it is called on (the old version)
with newer. Use documents of your own, off the UI thread:
using System.Text.Json.Nodes;
using Nexdevit.NexPdf;
var diff = await Task.Run(() =>
{
using var old = NexPdfDocument.OpenBytes(v1Bytes);
using var newer = NexPdfDocument.OpenBytes(v2Bytes);
return JsonNode.Parse(old.CompareTextJson(newer))!;
});
var s = diff["summary"]!;
SummaryLabel.Text = $"{s["similarity"]!.GetValue<double>():P0} similar, {s["words_inserted"]} words added, {s["words_deleted"]} removed";Each change has type (inserted, deleted, replaced or moved), old_text, new_text,
old_page, new_page, and old_rects / new_rects to highlight it. Words hyphenated across
lines are rejoined, and compound hyphens are kept (self-driving, long-term).
Options
ignore_case, ignore_punctuation, detect_moves (default true), min_move_words (default
3), align (document or index) and page_map. See the
.NET word-level comparison guide.
An annotated PDF of the changes
old.CompareAnnotate(newer, optionsJson) returns a new PDF with highlight annotations: deleted
words in red on the old pages, inserted words in green on the new pages, moved text in blue. Show it
in the view:
var pdf = await Task.Run(() =>
{
using var old = NexPdfDocument.OpenBytes(v1Bytes);
using var newer = NexPdfDocument.OpenBytes(v2Bytes);
return old.CompareAnnotate(newer, """{"output":"side_by_side","author":"Review"}""");
});
await view.LoadAsync(pdf);output is both (old pages then new pages, the default), old, new or side_by_side (one wide
page per pair). author and deleted_color, inserted_color and moved_color (#RRGGBB) are
also accepted.