Compare Two or More Word Documents using C#

Compare Two or More Word Documents using C#
Compare Two or More Word Documents using C#

You can easily compare two or more Word documents or compare multiple versions of the same Word file for the differences and similarities programmatically in your .NET applications. In this article, you will learn how to compare two or more Word documents and highlight differences using C#.

The following topics are discussed/covered in this article:

C# API to Compare DOCX Files

For comparing two or more DOCX files, I will be using GroupDocs.Comparison for .NET API. It compares two or more documents and finds out the changes in the content of the documents for words, paragraphs, and characters. As a result, it produces a comparison document that highlights the differences and lists a summary of the differences. It also enables you to detect changes and differences in text style between similar document formats. The API supports comparing all industry-standard document formats such as PDF, HTML, Word, Excel, PowerPoint, Outlook emails, Visio diagrams, OpenDocument, AutoCAD, and images.

You can either download the DLL of the API or install it using NuGet.

Install-Package GroupDocs.Comparison

Compare Two or More Word Documents using C#

You can compare two or more Word documents programmatically by following the simple steps given below:

  1. Create an instance of the Comparer class with the source DOCX file path
  2. Call the Add() method with the target DOCX file to add to the comparison
  3. Repeat the above step to add more files in comparison
  4. Call the Compare() method with the output file path

The following code sample shows how to compare two or more Word documents and highlights the differences using C#.

Source and Target DOCX Files
Source and Target DOCX Files
Compare Two or More Word Documents using C#
Compare Two or More Word Documents using C#

The resulting document also includes a summary page at the end of the document that shows the summary of all the changes found in the comparison.

The Comparer class is the main class that enables you to control and execute the comparison process. It provides several methods to compare two or more documents. The Add() method of this class, adds a file to the comparison process. You can easily add multiple files to the comparison by using the Add() method as shown below:

comparer.Add("target1.docx");
comparer.Add("target2.docx");
comparer.Add("target3.docx");

The Compare() method of the Comparer class compares the source and target documents. This method highlights the differences and saves results to the file path provided as an input parameter.

Compare Word Documents using Stream in C#

You can compare two or more Word documents using FileStream by following the steps given below:

  1. Read source file in a Stream object
  2. Read target file in another Stream object
  3. Create an instance of the Comparer class with the source Stream object
  4. Call the Add() method with the target Stream object to add to the comparison
  5. Call the Compare() method with the output file path

The following code sample shows how to compare Word documents using FileStream in C#.

Get Text of the Changes using C#

You can get the text of the changes found in the comparison of Word documents programmatically by following the simple steps given below:

  1. Create an instance of the Comparer class with the source DOCX file path
  2. Call the Add() method with the target DOCX file to add to the comparison
  3. Repeat the above step to add more files in comparison
  4. Call the Compare() method
  5. Call the GetChanges() method to get the details of changes
  6. Show the changes

The following code sample shows how to get the text of the changes using C#.

Count of changes: 10
Change Type: Inserted, Text:
Change Type: Inserted, Text:  Company ‼ HYPERLINK "http://www.aspose.com/" ¶Aspose Pty Ltd§ Division GroupDocs
Change Type: Inserted, Text:
Change Type: Inserted, Text: Cool
Change Type: Deleted, Text: test
Change Type: Inserted, Text:
Change Type: Inserted, Text: signatures
Change Type: Inserted, Text:
Change Type: Deleted, Text: Customers
Change Type: Deleted, Text: GroupDocs is used by companies of all sizes across the globe, from large multinational firms to small freelance businesses. They come to us because they have a need for a simple, one-stop-shop, document management solution.

You can get a list of changes between source and target files by calling the GetChanges() method of the Comparer class. It returns a list of ChangeInfo objects. The ChangeInfo class represents information about the changes and provides various properties to get details of the changes such as Text, Type, etc.

Documents Properties Comparison using C#

You can compare built-in, custom properties and variable properties of Word documents programmatically by following the steps given below:

  1. Create an instance of the Comparer class with the source DOCX file path
  2. Call the Add() method with the target DOCX file to add to the comparison
  3. Repeat the above step to add more files in comparison
  4. Create an instance of the CompareOptions
  5. Set the CompareVariableProperty to true
  6. Set the CompareDocumentProperty to true
  7. Call the Compare() method with the output file path and CompareOptions

The following code sample shows how to compare documents properties using C#.

Documents Properties Comparison using C#
Documents Properties Comparison using C#

You can enhance your comparison process by applying various compare options. For this purpose, the CompareOptions class allows setting various compare options to achieve specific results. The CompareDocumentProperty of this class enables you to turn on the comparison of built-in and custom properties in Word format. The CompareVariableProperty allows turning on the comparison of variables properties in Word format.

Compare Password Protected Word Documents using C#

You can compare two or more password-protected Word documents programmatically by following the steps given below:

  1. Create an instance of the LoadOptions class
  2. Provide the password for the source file
  3. Create an instance of the Comparer class with the source DOCX file path and LoadOptions
  4. Call the Add() method with the target DOCX file path and LoadOptions instance with password
  5. Repeat the above step to add more files in comparison
  6. Call the Compare() method with the output file path

The following code sample shows how to compare password-protected Word documents using C#.

The LoadOptions class enables you to specify additional options while loading a document. It provides the following properties to specify:

  • FontDirectories — List of font directories to load.
  • LoadText — Indicates that the strings passed are comparison text, not file paths (for Text Comparison only).
  • Password — Password of document.

Compare Bookmarks in Word Documents using C#

You can compare bookmarks available in Word documents programmatically by following the steps given below:

  1. Create an instance of the Comparer class with the source DOCX file path
  2. Call the Add() method with the target DOCX file to add to the comparison
  3. Repeat the above step to add more files in comparison
  4. Create an instance of the CompareOptions
  5. Set the CompareBookmarks to true
  6. Call the Compare() method with the output file path and CompareOptions

The following code sample shows how to compare bookmarks in Word documents using C#.

Compare Bookmarks in Word Documents using C#
Compare Bookmarks in Word Documents using C#

The CompareBookmarks property allows you to compare the bookmarks available in the source and target documents.

Get a Free License

You can try the API without evaluation limitations by requesting a free temporary license.

Conclusion

In this article, you have learned how to compare two or more Word documents and highlight differences using C#. You have also learned how to get the list of highlighted changes. Moreover, you have learned how to compare bookmarks in Word documents programmatically. Furthermore, you have learned how to compare password-protected Word documents using C#. You can learn more about GroupDocs.Comparison for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also