Compare Word Documents in .NET Word
18 Aug 202614 minutes to read
Comparing two Word documents allows you to identify the changes between two versions of a document. The .NET Word library (DocIO) enables you to compare two Word documents and highlight the following changes as tracked changes.
- Insertions
- Deletions
- Formatting
NOTE
- DocIO performs word level comparison while comparing two Word documents. In this scenario, if a single character in a word is changed, the entire word will be highlighted as changed.
- Comparing two Word documents supported in DOCX format only.
To quickly start comparing two Word documents, please check out this video:
Assemblies and NuGet packages required
Refer to the following links for assemblies and NuGet packages required based on platforms for comparing Word documents using the .NET Word Library (DocIO).
Compare two Word documents
Compare the existing Word documents or documents created from scratch using the Compare method in the .NET Word library (DocIO).
The following code example illustrates how to compare two Word documents.
NOTE
Refer to the appropriate tabs in the code snippets section: C# [Cross-platform] for ASP.NET Core, Blazor, Xamarin, UWP, .NET MAUI, and WinUI; C# [Windows-specific] for WinForms and WPF; VB.NET [Windows-specific] for VB.NET applications.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (FileStream originalDocumentStreamPath = new FileStream("Data/OriginalDocument.docx", FileMode.Open, FileAccess.Read))
{
using (WordDocument originalDocument = new WordDocument(originalDocumentStreamPath, FormatType.Docx))
{
// Load the revised document.
using (FileStream revisedDocumentStreamPath = new FileStream("Data/RevisedDocument.docx", FileMode.Open, FileAccess.Read))
{
using (WordDocument revisedDocument = new WordDocument(revisedDocumentStreamPath, FormatType.Docx))
{
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument);
// Save the Word document to MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
originalDocument.Save(stream, FormatType.Docx);
// Save the stream to a file.
File.WriteAllBytes("Result.docx", stream.ToArray());
}
}
}
}
}using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (WordDocument originalDocument = new WordDocument("Data/OriginalDocument.docx", FormatType.Docx))
{
// Load the revised document.
using (WordDocument revisedDocument = new WordDocument("Data/RevisedDocument.docx", FormatType.Docx))
{
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument);
// Save the Word document.
originalDocument.Save("Result.docx");
}
}Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
' Load the original document.
Using originalDocument As New WordDocument("Data/OriginalDocument.docx", FormatType.Docx)
' Load the revised document.
Using revisedDocument As New WordDocument("Data/RevisedDocument.docx", FormatType.Docx)
' Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument)
' Save the Word document.
originalDocument.Save("Result.docx")
End Using
End Using
You can download a complete working sample from GitHub.
Set Author and Date
Compare the two Word documents by setting the author and date for revisions to identify the changes. In DocIO, the default author is “Author” and the default date/time is the current time. The following sample uses an overload of the Compare method that accepts an author and date.
The following code example shows how to set the author and date for revision while comparing two Word documents.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (FileStream originalDocumentStreamPath = new FileStream("Data/OriginalDocument.docx", FileMode.Open, FileAccess.Read))
{
using (WordDocument originalDocument = new WordDocument(originalDocumentStreamPath, FormatType.Docx))
{
// Load the revised document.
using (FileStream revisedDocumentStreamPath = new FileStream("Data/RevisedDocument.docx", FileMode.Open, FileAccess.Read))
{
using (WordDocument revisedDocument = new WordDocument(revisedDocumentStreamPath, FormatType.Docx))
{
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Nancy Davolio", DateTime.Now.AddDays(-1));
// Save the Word document to MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
originalDocument.Save(stream, FormatType.Docx);
// Save the stream to a file.
File.WriteAllBytes("Result.docx", stream.ToArray());
}
}
}
}
}using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (WordDocument originalDocument = new WordDocument("Data/OriginalDocument.docx", FormatType.Docx))
{
// Load the revised document.
using (WordDocument revisedDocument = new WordDocument("Data/RevisedDocument.docx", FormatType.Docx))
{
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Nancy Davolio", DateTime.Now.AddDays(-1));
// Save the Word document.
originalDocument.Save("Result.docx");
}
}Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
' Load the original document.
Using originalDocument As New WordDocument("Data/OriginalDocument.docx", FormatType.Docx)
' Load the revised document.
Using revisedDocument As New WordDocument("Data/RevisedDocument.docx", FormatType.Docx)
' Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Nancy Davolio", DateTime.Now.AddDays(-1))
' Save the Word document.
originalDocument.Save("Result.docx")
End Using
End Using
You can download a complete working sample from GitHub.
Comparison options
You can customize the Word comparison using our ComparisonOptions in the DocIO.
Ignore format changes
In the .NET Word library (DocIO), document comparison includes formatting changes by default. However, you can configure DocIO to ignore formatting differences using the DetectFormatChanges API to concentrate solely on content modifications.
The following code example illustrates how to compare two Word documents by ignoring the format changes.
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (FileStream originalDocumentStreamPath = new FileStream("Data/OriginalDocument.docx", FileMode.Open, FileAccess.Read))
{
using (WordDocument originalDocument = new WordDocument(originalDocumentStreamPath, FormatType.Docx))
{
// Load the revised document.
using (FileStream revisedDocumentStreamPath = new FileStream("Data/RevisedDocument.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (WordDocument revisedDocument = new WordDocument(revisedDocumentStreamPath, FormatType.Docx))
{
// Set whether to detect format changes while comparing two Word documents.
ComparisonOptions compareOptions = new ComparisonOptions();
compareOptions.DetectFormatChanges = false;
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Syncfusion", DateTime.Now, compareOptions);
// Save the Word document to MemoryStream.
using (MemoryStream stream = new MemoryStream())
{
originalDocument.Save(stream, FormatType.Docx);
// Save the stream to a file.
File.WriteAllBytes("Result.docx", stream.ToArray());
}
}
}
}
}using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
// Load the original document.
using (WordDocument originalDocument = new WordDocument("Data/OriginalDocument.docx", FormatType.Docx))
{
// Load the revised document.
using (WordDocument revisedDocument = new WordDocument("Data/RevisedDocument.docx", FormatType.Docx))
{
// Set whether to detect format changes while comparing two Word documents.
ComparisonOptions compareOptions = new ComparisonOptions();
compareOptions.DetectFormatChanges = false;
// Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Syncfusion", DateTime.Now, compareOptions);
// Save the Word document.
originalDocument.Save("Result.docx");
}
}Imports Syncfusion.DocIO
Imports Syncfusion.DocIO.DLS
' Load the original document.
Using originalDocument As New WordDocument("Data/OriginalDocument.docx", FormatType.Docx)
' Load the revised document.
Using revisedDocument As New WordDocument("Data/RevisedDocument.docx", FormatType.Docx)
' Set whether to detect format changes while comparing two Word documents.
Dim compareOptions As New ComparisonOptions()
compareOptions.DetectFormatChanges = False
' Compare the original and revised Word documents.
originalDocument.Compare(revisedDocument, "Syncfusion", DateTime.Now, compareOptions)
' Save the Word document.
originalDocument.Save("Result.docx")
End Using
End Using
You can download a complete working sample from GitHub.
Online Demo
- Explore how to compare Word documents using the .NET Word Library (DocIO) in a live demo here.