Effortlessly Compare Word Documents Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)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  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)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