Compare Word Documents using C#

Word processing documents are the most common way to draft contracts, agreements, papers, and many other official documents. If you need to compare and merge two Word documents, just like the track changes option of Microsoft Word, we can programmatically do it within our .NET applications. In this article, we will discuss how to compare two Word documents and highlight the identified differences using C#. Additionally, we will look at how to compare password-protected documents, accept and reject changes, and compare more than two documents with C# examples.

Compare Word Documents to find differences using .NET API

The following topics are discussed here:

.NET API to Compare Word Documents

GroupDocs.Comparison provides a .NET API that allows comparing and then merging various documents of multiple file-formats within the .NET application. I will use its .NET API i.e. GroupDocs.Comparison for .NET to compare Word documents.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Comparison

Compare Word Documents using C#

If you have two versions of a document, you can compare the documents to find their differences (additions, deletions) and create a new document that shows all the changes. The following are the steps to compare any two Word documents and highlight the differences.

  • Load the first Word document using Comparer class.
  • Add the second file to Comparer using Add() method.
  • Compare the get changes summary by just calling Compare() method.

The following C# code shows how to compare Word documents and get the changes in the resultant document.

Accept or Reject Identified Changes of Word Documents using C#

Similar to the track changes option of MS Word, you can accept or reject each identified change. The following are the steps to compare and then accept or reject the identified changes within the Word documents.

  • Load the source document and add target Word document(s) using Comparer class.
  • Make the comparison of loaded documents using Compare() method.
  • Get the identified changes using GetChanges() method.
  • Now you can traverse the changes and set ComparisonAction of each change.
    • For each change you can select Accept, or Reject.
  • When done with changes, call the ApplyChanges() method to get the resultant document having the applied changes.

The following C# source code compares two Word documents and then accepts an identified change and then rejects another one.

Compare More than Two Documents using C#

Similarly, more than two documents can be compared in one go. The following are the steps to compare multiple Word documents for differences and highlight the identified changes.

  • Load the first Word document using Comparer class.
  • Keep adding the other document(s) to Comparer using the Add() method.
  • Call the Compare() method to get the changes & summary of changes.

The following C# code shows how to compare more than two Word documents and get the changes in the resultant document.

Compare Password Protected Word Documents using C#

If your documents are password-protected, just provide their password while loading the documents. The following steps show how we can compare the content of password-protected documents using C#.

  • Prepare the loading options for source and target documents by providing the password.
  • Load the source document using Comparer class.
  • Add the target document to Comparer using the prepared loading options.
  • Get the differences summary by calling the Compare() method.

The following C# code example compares two password-protected Word files and generates the resultant document that highlights the differences.

Get a Free API License

You can get a free temporary license to use the API without the evaluation limitations.

Conclusion

To conclude, we learned to compare two or more Word documents using C#. Further, after highlighting the differences, we learned to programmatically accept and reject the identified changes. In the end, we also discussed how could we compare the password-protected Word documents within .NET applications.

There are many other customizations to control the comparison results, like setting the comparison sensitivity, showing only the summary page, ignoring gaps, and much more. Learn more about GroupDocs.Comparison for .NET. Visit its documentation to start building your own document comparison applications for various supported document formats. For queries, contact us via the forum.

See Also