Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare Two or More Word Documents and Highlight Differences using C#
You can easily compare two or more Word documents and highlight the differences programmatically. You may need to compare multiple versions of the same Word file or different files for the differences and similarities in your Java application. Here, you will learn how to compare two or more Word documents and highlight differences using C#.
The following topics are discussed/covered here:
1. Compare Word Documents using C#
2. Compare Word Documents using Stream in C#
3. Get Text of the Changes using C#
4. Documents Properties Comparison using C#
5. Compare Password Protected Word Documents using C#
6. Compare Bookmarks in Word Documents using C#
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file to compare
comparer.Add("C:\\Files\\target.docx");
// compare and save differences
comparer.Compare("C:\\Files\\result.docx");
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file to compare
comparer.Add("C:\\Files\\target.docx");
// define compare options
CompareOptions compareOptions = new CompareOptions();
compareOptions.CompareBookmarks = true; // compare bookmarks
// compare
comparer.Compare("C:\\Files\\result.docx", compareOptions);
// define load options for source file
LoadOptions sourceLoadOptions = new LoadOptions() { Password = "1234" };
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx", sourceLoadOptions);
// add target file to compare
comparer.Add("C:\\Files\\target.docx", new LoadOptions() { Password = "5678" });
// compare
comparer.Compare("C:\\Files\\result.docx");
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file to compare
comparer.Add("C:\\Files\\target.docx");
// define compare options
CompareOptions options = new CompareOptions();
options.CompareVariableProperty = true; // activate the comparison of variable properties
options.CompareDocumentProperty = true; // activate the comparison of built and custom properties
// compare
comparer.Compare("C:\\Files\\result.docx", options);
// read source and target files
using (Stream sourceStream = File.OpenRead("C:\\Files\\source.docx"))
using (Stream targetStream = File.OpenRead("C:\\Files\\target.docx"))
{
// initialize comparer
using (Comparer comparer = new Comparer(sourceStream))
{
// add target filestream to compare
comparer.Add(targetStream);
// compare and save differences
comparer.Compare(File.Create("C:\\Files\\result.docx"));
}
}
// initialize comparer
Comparer comparer = new Comparer("C:\\Files\\source.docx");
// add target file to compare
comparer.Add("C:\\Files\\target.docx");
// compare
comparer.Compare();
// get changes
ChangeInfo[] changes = comparer.GetChanges();
Console.WriteLine("Count of changes: " + changes.Length);
// show changes
foreach (ChangeInfo change in changes)
{
Console.WriteLine("Change Type: " + change.Type + ", Text: " + change.Text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment