Word-level comparison
Compare the text of two PDF versions word by word, list inserted, deleted, replaced and moved text, and produce an annotated PDF of the changes with NexPDF for Go.
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(new, optionsJson) compares the document it is called on (the old version)
with new:
type TextDiff struct {
Summary struct {
WordsInserted int `json:"words_inserted"`
WordsDeleted int `json:"words_deleted"`
Moved int `json:"moved"`
Similarity float64 `json:"similarity"`
} `json:"summary"`
Changes []struct {
Type string `json:"type"`
OldText string `json:"old_text"`
NewText string `json:"new_text"`
OldPage *int `json:"old_page"`
NewPage *int `json:"new_page"`
} `json:"changes"`
}
oldDoc, err := nexpdf.Open("contract-v1.pdf")
if err != nil {
log.Fatal(err)
}
defer oldDoc.Close()
newDoc, err := nexpdf.Open("contract-v2.pdf")
if err != nil {
log.Fatal(err)
}
defer newDoc.Close()
s, err := oldDoc.CompareTextJSON(newDoc, "")
if err != nil {
log.Fatal(err)
}
var diff TextDiff
json.Unmarshal([]byte(s), &diff)
fmt.Printf("%.1f%% similar: +%d -%d words, %d moves\n", diff.Summary.Similarity*100,
diff.Summary.WordsInserted, diff.Summary.WordsDeleted, diff.Summary.Moved)
for _, c := range diff.Changes {
fmt.Printf("%s %q -> %q\n", c.Type, c.OldText, c.NewText)
}Each change has type (inserted, deleted, replaced or moved), old_text, new_text,
old_page, new_page, and old_rects / new_rects ({"page", "rect": [llx, lly, urx, ury]}) to
highlight it. The summary counts words_old, words_new, words_equal, the changes by type, the
words inserted, deleted and moved, similarity (0 to 1) and truncated.
Options
| Option | Default | Meaning |
|---|---|---|
ignore_case | false | Treat upper and lower case as equal |
ignore_punctuation | false | Ignore punctuation differences |
detect_moves | true | Report text that moved as moved instead of deleted plus inserted |
min_move_words | 3 | Shortest run of words reported as a move |
align | document | document compares the whole text; index compares page 1 with page 1, and so on |
page_map | Explicit page pairs, [[old_page, new_page], …] |
Words hyphenated across a line break are rejoined before comparing; compound hyphens are kept
(self-driving, long-term).
An annotated PDF of the changes
old.CompareAnnotate(new, 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.
pdf, err := oldDoc.CompareAnnotate(newDoc, `{"output":"side_by_side","author":"Review bot"}`)
if err != nil {
log.Fatal(err)
}
os.WriteFile("changes.pdf", pdf, 0o644)It takes the options above plus output (both: old pages then new pages, the default; old;
new; or side_by_side: one wide page per pair with the marks drawn in), author, and
deleted_color, inserted_color and moved_color (#RRGGBB). Open the result with
nexpdf.OpenBytes(pdf) to work with it further.