Effortlessly Compare Word Documents Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (182).NET Core  (27).NET MAUI  (233)Angular  (117)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (42)Black Friday Deal  (1)Blazor  (244)BoldSign  (15)DocIO  (24)Essential JS 2  (109)Essential Studio  (200)File Formats  (74)Flutter  (140)JavaScript  (227)Microsoft  (121)PDF  (86)Python  (1)React  (107)Streamlit  (1)Succinctly series  (131)Syncfusion  (991)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)Word  (9)WPF  (165)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (11)Business intelligence  (55)Button  (4)C#  (164)Chart  (154)Chart of the week  (62)Cloud  (16)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (74)Development  (694)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (31)File Manager  (8)Gantt  (23)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (530)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (48)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (426)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (648)What's new  (354)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Effortlessly Compare Word Documents Using C#

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.

Mastering Word documents is a breeze with the Syncfusion Word Library, simplifying every aspect of document creation and management!

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;

Now, the project is ready!

Get insights into the Syncfusion’s Word Library and its stunning array of features with its extensive documentation.

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);
                }
            }
        }
    }
}

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

Unearth the endless potential firsthand through demos highlighting the features of the Syncfusion Word Library.

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);
                }
            }
        }
    }
}

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.

The Syncfusion Word Library provides cross-platform compatibility and can be tailored to your unique needs.

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.
  • Mergesplit, and organize Word documents.
  • Convert Word documents into HTMLRTFPDFimages, 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 forumsupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed