Skip to main content

How to Compare Word Documents in C#

 Comparing Word documents is a common requirement when working with reports, contracts, or any content that undergoes frequent revisions. In C#, you can automate this process to detect changes, highlight differences, and generate a consolidated comparison document. This article walks through several ways to compare Word documents using C#.

Compare Word Documents in C#

Installing the Required Library

Before you start, install the Free Spire.Doc library via NuGet. In your Visual Studio Package Manager Console, run:

Install-Package FreeSpire.Doc

This library provides APIs to load, edit, and compare Word documents in C#. Once installed, import the following namespaces in your class:

using Spire.Doc;
using Spire.Doc.Documents.Comparison;

1. Basic Word Document Comparison in Csharp

The simplest scenario is to compare two documents and mark the differences with tracked changes. This approach uses the Document.Compare method to highlight changes under a specified author name.

// Create a new Document object for the first document 
Document doc1 = new Document();
doc1.LoadFromFile(@"SupportDocumentCompare1.docx");

// Create a new Document object for the second document
Document doc2 = new Document();
doc2.LoadFromFile(@"SupportDocumentCompare2.docx");

// Compare the contents of the two documents
doc1.Compare(doc2, "AuthorName");

// Save the result to a new file
string result = "CompareDocuments_result.docx";
doc1.SaveToFile(result, FileFormat.Docx2013);

// Release resources
doc1.Dispose();
doc2.Dispose();

This example demonstrates a direct comparison with default settings, capturing all differences, including formatting and structural changes.

2. Using Custom Options to Compare Word Documents in Csharp

You can customize the comparison behavior using the CompareOptions class. For example, you might want to ignore formatting differences, focus only on text, or exclude specific elements like tables or headers and footers.

2.1 Ignore Formatting Changes

Formatting changes can be ignored by setting the IgnoreFormatting property to true.

Document doc1 = new Document();
doc1.LoadFromFile(@"SupportDocumentCompare1.docx");

Document doc2 = new Document();
doc2.LoadFromFile(@"SupportDocumentCompare2.docx");

// Create CompareOptions and set IgnoreFormatting to true
CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreFormatting = true;

// Compare documents with the specified options
doc1.Compare(doc2, "AuthorName", DateTime.Now, compareOptions);

doc1.SaveToFile("CompareDocumentsWithOptions_result.docx", FileFormat.Docx2013);

doc1.Dispose();
doc2.Dispose();

This example ensures that only textual content differences are highlighted while font styles, colors, and other formatting changes are ignored.

2.2 Ignoring Specific Elements

You can also ignore certain parts of the document such as headers, footers, and tables:

  • Ignore Headers and Footers
compareOptions.IgnoreHeadersAndFooters = true;
  • Ignore Tables
compareOptions.IgnoreTable = true;

These options help you focus on the content that is most important for your comparison.

3. Controlling Text Comparison Level in Word Documents with Csharp

The granularity of text comparison can be controlled using the TextCompareLevel property in CompareOptions. You can choose to compare documents word by word or character by character, depending on how precise you want the changes to be tracked.

CompareOptions compareOptions = new CompareOptions();
compareOptions.TextCompareLevel = TextDiffMode.Word; // or TextDiffMode.Char

Explanation:

  • TextDiffMode.Word – compares text word by word.

  • TextDiffMode.Char – compares text character by character.

Choosing Word is usually sufficient for most documents, while Char provides more precise tracking for minor edits.

Tips for Comparing Word Documents in Csharp

Here are some practical tips to ensure your document comparisons in C# are accurate and meaningful:

  • Choose what to ignore wisely – Ignoring tables, headers, footers, or formatting can make the comparison cleaner, but only if these elements are not important for your use case.

  • Select the right text comparison level – Use Word for general comparisons and Char for detecting very fine changes.

  • Manage output files clearly – Name your comparison results to reflect the versions compared to avoid confusion when comparing multiple files.

  • Review the result visually – Even with automation, check the resulting document to ensure changes are captured as expected.

Summary

Comparing Word documents in C# can be simple or highly customizable, depending on your needs. You can start with a basic comparison, apply options to ignore formatting or specific elements, adjust the text comparison level, and handle large or complex documents efficiently. Automating document comparisons helps maintain consistency, track revisions effectively, and save time when working with multiple versions of a document.

Comments

Popular posts from this blog

3 Ways to Generate Word Documents from Templates in Java

A template is a document with pre-applied formatting like styles, tabs, line spacing and so on. You can quickly generate a batch of documents with the same structure based on the template. In this article, I am going to show you the different ways to generate Word documents from templates programmatically in Java using Free Spire.Doc for Java library. Prerequisite First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file. <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>...

Insert and Extract OLE objects in Word in Java

You can use OLE (Object Linking and Embedding) to include content from other programs, such as another Word document, an Excel or PowerPoint document to an existing Word document. This article demonstrates how to insert and extract embedded OLE objects in a Word document in Java by using Free Spire.Doc for Java API.   Add dependencies First of all, you need to add needed dependencies for including Free Spire.Doc for Java into your Java project. There are two ways to do that. If you use maven, you need to add the following code to your project’s pom.xml file.     <repositories>               <repository>                   <id>com.e-iceblue</id>                   <name>e-iceblue</name>    ...

Simple Java Code to Convert Excel to PDF in Java

This article demonstrates a simple solution to convert an Excel file to PDF in Java by using free Excel API – Free Spire.XLS for Java . The following examples illustrate two possibilities to convert Excel to PDF:      Convert the whole Excel file to PDF     Convert a particular Excel Worksheet to PDF Before start with coding, you need to Download Free Spire.XLS for Java package , unzip it and import Spire.Xls.jar file from the lib folder in your project as a denpendency. 1. Convert the whole Excel file to PDF Spire.XLS for Java provides saveToFile method in Workbook class that enables us to easily save a whole Excel file to PDF. import com.spire.xls.FileFormat; import com.spire.xls.Workbook; public class ExcelToPDF {     public static void main(String[] args){         //Create a Workbook         Workbook workbook = new Workbook();   ...