Skip to content

Instantly share code, notes, and snippets.

@conholdate-gists
Last active February 19, 2024 09:19
Show Gist options
  • Save conholdate-gists/0820fa9bb316a147466d99f751724789 to your computer and use it in GitHub Desktop.
Save conholdate-gists/0820fa9bb316a147466d99f751724789 to your computer and use it in GitHub Desktop.
Compare Word Documents and Highlight Differences using Java
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file
comparer.add("C:\\Files\\target.docx");
// define compare options
CompareOptions compareOptions = new CompareOptions();
compareOptions.setCompareBookmarks(true);
// compare and save compare results
comparer.compare("C:\\Files\\result.docx", compareOptions);
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file
comparer.add("C:\\Files\\target.docx");
// compare and save compare results
comparer.compare("C:\\Files\\result.docx");
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file
comparer.add("C:\\Files\\target.docx");
// compare
final Path resultPath = comparer.compare();
// get changes
ChangeInfo[] changes = comparer.getChanges();
System.out.println("Count of changes: " + changes.length);
for (ChangeInfo change : changes) {
System.out.printf("Change Type: %d, Text: %s%n", change.getType(), change.getText());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment