Effortlessly Compare Word Documents Using C#
Tags: fileformats, csharp, word, whatsnew
Published: 2023-10-29 14:52:23

---
title: Effortlessly Compare Word Documents Using C#
published: true
date: 2023-10-18 14:39:36 UTC
tags: fileformats, csharp, word, whatsnew
canonical_url: https://www.syncfusion.com/blogs/post/compare-word-documents-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yv43j3p0kln01fbfjmi5.png
---
In real-life scenarios, you often need to compare two Word documents to identify the differences between them.

For example, if you have an original Word document and a revised version, finding the differences between them can be time-consuming. The solution to this challenge is to utilize the Syncfusion [.NET Word Library](https://www.syncfusion.com/document-processing/word-framework/net/word-library ".NET Word Library") (DocIO).

From the 2023 Volume 3 onwards, Syncfusion .NET Word Library supports programmatically comparing two Word documents with just a few lines of code in C#. This library does not need Microsoft Word or any other interop dependencies. With this functionality, you can easily identify the changes between two versions of a Word document.

**Note:** If you are new to our Word Library, following our [Getting Started guide](https://help.syncfusion.com/file-formats/docio/getting-started "Getting started with simple word document") is highly recommended.

## Getting started

**Step 1: ** First, [create a new C# .NET Core Console App ](https://help.syncfusion.com/file-formats/docio/create-word-document-in-asp-net-core "Getting Started with the Creation of a Word Document in ASP.NET Core")in [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio"). ![Select Console App in Visual Studio](https://www.syncfusion.com/blogs/wp-content/uploads/2023/10/Select-Console-App-in-Visual-Studio.png)

**Step 2:**  Then, install the [Syncfusion.DocIO.Net.Core](https://www.nuget.org/packages/Syncfusion.DocIO.Net.Core "Syncfusion.DocIO.Net.Core NuGet package") NuGet package as a reference to the app from the [NuGet Gallery](https://www.nuget.org/ "NuGet Gallery"). ![Installing Syncfusion.DocIO.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2023/10/Installing-Syncfusion.DocIO_.Net_.Core-NuGet-package.png)

**Step 3:** Include the following namespaces in the **Program.cs** file.

```csharp
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
```

Now, the project is ready!

## Compare two Word documents

Imagine you’ve sent a Word document to someone, and you need to identify the changes they made for review purposes quickly. By comparing two versions of the Word document programmatically, you can quickly identify changes such as insertions, deletions, and format changes.

Refer to the following code example to compare two Word documents using C#.

```csharp
//Open the file as Stream. 
using (FileStream originalDocumentFileStream = new FileStream(Path.GetFullPath(@"OriginalDocument.docx"), FileMode.Open))
{
    //Load the original Word document.
    using (WordDocument originalDocument = new WordDocument(originalDocumentFileStream, FormatType.Docx))
    {
        //Open the file as Stream. 
        using (FileStream revisedDocumentFileStream = new FileStream(Path.GetFullPath(@"RevisedDocument.docx"), FileMode.Open))
        {
//Load the revised Word document.
using (WordDocument revisedDocument = new WordDocument(revisedDocumentFileStream, FormatType.Docx))
{
    //Compare the original document with revised document
    originalDocument.Compare(revisedDocument, "Nancy Davolio", DateTime.Now.AddDays(-1));
    //Create the output file stream.
    using (FileStream fileStreamOutput = File.Create(Path.GetFullPath(@"Output.docx")))
    {
        //Save the document.
        originalDocument.Save(fileStreamOutput, FormatType.Docx);
    }
}
        }
    }
}
```

After executing this code example, we’ll get output like in the following image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/10/Comparing-Word-documents-with-tracked-changes.png" alt="Comparing Word documents with tracked changes" style="width:100%">

  <figcaption>Comparing Word documents with tracked changes</figcaption>

</figure>

## Ignore format changes

Consider a scenario where you’re reviewing the changes in a Word document. Some changes might involve formatting adjustments like font or style modifications, which aren’t substantial. By disabling the **DetectFormatChanges** flag in your code example, you can focus exclusively on the critical changes, such as content added or removed.

Refer to the following code example to compare two Word documents, but ignore the formatting changes.

```csharp
//Open the file as Stream. 
using (FileStream originalDocumentFileStream = new FileStream(Path.GetFullPath(@"OriginalDocument.docx"), FileMode.Open))
{
    //Load the original Word document.
    using (WordDocument originalDocument = new WordDocument(originalDocumentFileStream, FormatType.Docx))
    {
        //Open the file as Stream. 
        using (FileStream revisedDocumentFileStream = new FileStream(Path.GetFullPath(@"RevisedDocument.docx"), FileMode.Open))
        {
//Load the revised Word document.
using (WordDocument revisedDocument = new WordDocument(revisedDocumentFileStream, FormatType.Docx))
{
    //Disable the flag to ignore the formatting changes while comparing the documents.
    ComparisonOptions comparisonOptions = new ComparisonOptions();
    comparisonOptions.DetectFormatChanges = false;
    //Compare the original document with revised document
    originalDocument.Compare(revisedDocument, "Nancy Davolio", DateTime.Now.AddDays(-1), comparisonOptions);
    //Create the output file stream.
    using (FileStream fileStreamOutput = File.Create(Path.GetFullPath(@"Output.docx")))
    {
        //Save the document.
        originalDocument.Save(fileStreamOutput, FormatType.Docx);
    }
}
        }
    }
}
```

After executing this code example, we’ll get output like in the following image.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/10/Comparing-Word-documents-by-ignoring-formatting-changes.png" alt="Comparing Word documents by ignoring formatting changes" style="width:100%">

  <figcaption>Comparing Word documents by ignoring formatting changes</figcaption>

</figure>

## GitHub reference

You can find all the examples for [comparing Word documents using C#](https://github.com/SyncfusionExamples/compare-word-documents-c-sharp "Comparing Word documents using C# GitHub demo") in the GitHub repository.

## Conclusion

Thanks for reading. In this blog, we’ve seen how to compare two Word documents and highlight the difference between them using the Syncfusion .NET Core Word Library (DocIO). Take a moment to peruse its [documentation](https://help.syncfusion.com/file-formats/docio/word-document/compare-word-documents "Getting Started with Comparing Word documents in C#"), where you’ll find other options and features, all with accompanying code examples.

Apart from this comparison functionality, our Syncfusion .NET Word Library has the following significant functionalities:

- Create, read, and edit Word documents programmatically.
- Create complex reports by merging data into a Word template from various data sources through [mail merge](https://help.syncfusion.com/file-formats/docio/working-with-mail-merge "Begin Your Journey with Mail Merge").
- [Merge](https://help.syncfusion.com/file-formats/docio/word-document/merging-word-documents "Get Started with Merging Word documents"), [split](https://help.syncfusion.com/file-formats/docio/word-document/split-word-documents "Getting Started with Splitting the Word documents"), and organize Word documents.
- Convert Word documents into [HTML](https://help.syncfusion.com/file-formats/docio/html "Getting Started with Converting Word to HTML and HTML to Word"), [RTF](https://help.syncfusion.com/file-formats/docio/rtf "Get Started with the RTF Conversions in Word Library"), [PDF](https://help.syncfusion.com/file-formats/docio/word-to-pdf "Getting Started with Word document to PDF Conversion"), [images](https://help.syncfusion.com/file-formats/docio/word-to-image "Getting Started with Rendering / Converting Word document to Image in Word Library"), and other formats.

You can find more Word Library examples at this [GitHub location](https://github.com/SyncfusionExamples/DocIO-Examples "Syncfusion DocIO GitHub examples").

Are you already a Syncfusion user? You can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/ "Essential Studio License and Downloads page"). If you’re not yet a Syncfusion user, you can download a [30-day free trial](https://www.syncfusion.com/downloads/fileformats/confirm "Get free evaluation of the Essential Studio products").

If you have questions, contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/react "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [Explore the Possibilities of the What-If Analysis Scenario Manager in Excel Using C#](https://www.syncfusion.com/blogs/post/what-if-analysis-scenario-manager-excel-csharp.aspx "Blog: Explore the Possibilities of the What-If Analysis Scenario Manager in Excel Using C#")
- [How to Add Comments to Excel Documents Using C#](https://www.syncfusion.com/blogs/post/add-comments-in-excel-csharp.aspx "Blog: How to Add Comments to Excel Documents Using C#")
- [What’s New 2023 Volume 3: File-Format Libraries](https://www.syncfusion.com/blogs/post/whats-new-file-format-libraries.aspx "Blog: What’s New 2023 Volume 3: File-Format Libraries")
- [Creating a .NET MAUI Smart PDF Viewer App with Auto-Summary Generation Using OpenAI’s ChatGPT](https://www.syncfusion.com/blogs/post/dotnet-maui-smart-pdf-viewer-using-openai.aspx "Blog: Creating a .NET MAUI Smart PDF Viewer App with Auto-Summary Generation Using OpenAI’s ChatGPT")