Logo

dev-resources.site

for different kinds of informations.

Effortlessly Compare Word Documents Using C#

Published at
10/29/2023
Categories
fileformats
csharp
word
whatsnew
Author
jollenmoyani
Categories
4 categories in total
fileformats
open
csharp
open
word
open
whatsnew
open
Author
12 person written this
jollenmoyani
open
Effortlessly Compare Word Documents Using C#

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 (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 is highly recommended.

Getting started

*Step 1: * First, create a new C# .NET Core Console App in Visual Studio. Select Console App in Visual Studio

Step 2: Then, install the Syncfusion.DocIO.Net.Core NuGet package as a reference to the app from the NuGet Gallery. Installing Syncfusion.DocIO.Net.Core NuGet package

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

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
Enter fullscreen mode Exit fullscreen mode

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#.

//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);
    }
}
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Comparing Word documents with tracked changes

Comparing Word documents with tracked changes

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.

//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);
    }
}
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Comparing Word documents by ignoring formatting changes

Comparing Word documents by ignoring formatting changes

GitHub reference

You can find all the examples for comparing Word documents using C# 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, 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.
  • Merge, split, and organize Word documents.
  • Convert Word documents into HTML, RTF, PDF, images, and other formats.

You can find more Word Library examples at this GitHub location.

Are you already a Syncfusion user? You can download the product setup here. If you’re not yet a Syncfusion user, you can download a 30-day free trial.

If you have questions, contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

fileformats Article's
30 articles in total
Favicon
Introducing the Syncfusion® Document Viewer Extension for Visual Studio Code
Favicon
What’s New in Document Processing Libraries: 2024 Volume 4
Favicon
How to Effectively Use Formulas in Excel Using C#
Favicon
Easily Create Dynamic Charts in Excel Using C#
Favicon
6 Effective Ways to Merge PDF Files Using C#
Favicon
What’s New in Document Processing Libraries: 2024 Volume 3
Favicon
3 Easy Steps to Add Watermarks to Your Excel Document Using C#
Favicon
Easily Create PDF Tables with Advanced Customization in C#
Favicon
Easily Convert Your PDF to Images in C#
Favicon
4 Simple Ways to Split Word Documents Using C#
Favicon
What’s New in 2024 Volume 2: Document Processing Libraries
Favicon
Create Excel Table in Just 3 Steps Using C#
Favicon
Easily Create an Excel Pivot Table in Just 3 Steps Using C#
Favicon
What’s New in 2024 Volume 1: Document Processing Libraries
Favicon
What’s New in 2023 Volume 4: File Format Libraries
Favicon
Converting XLS to XLSX Format in Just 3 Steps Using C#
Favicon
Print Excel Documents in Just 4 Steps Using C#
Favicon
3 Simple Steps to Split an Excel File into Multiple Excel Files in C#
Favicon
An Ultimate Solution to Convert EML to PST Format
Favicon
Easily Convert Organizational Chart Diagrams to PowerPoint Presentations
Favicon
Merge Multiple Excel Files into One in Just 3 Steps Using C#
Favicon
Automate Email Distribution of Income Tax Reports from Excel using C#
Favicon
Top 10 Must-Have Features in C# PDF Library
Favicon
How to Convert OST to MBOX Format: 2 Effective Techniques
Favicon
Optical Character Recognition (OCR) Made Easy with the .NET PDF Library in C#
Favicon
Secure Your PDF Documents Like a Pro Using C#
Favicon
Effortlessly Compare Word Documents Using C#
Favicon
Explore the Possibilities of the What-If Analysis Scenario Manager in Excel Using C#
Favicon
How to Add Comments to Excel Documents Using C#
Favicon
Convert Word Documents to Markdown and Vice Versa in C#

Featured ones: