Comparing Word documents is a common requirement when working with reports, contracts, or any content that undergoes frequent revisions. In C#, you can automate this process to detect changes, highlight differences, and generate a consolidated comparison document. This article walks through several ways to compare Word documents using C#.
Installing the Required Library
Before you start, install the Free Spire.Doc library via NuGet. In your Visual Studio Package Manager Console, run:
Install-Package FreeSpire.Doc
This library provides APIs to load, edit, and compare Word documents in C#. Once installed, import the following namespaces in your class:
using Spire.Doc;
using Spire.Doc.Documents.Comparison;
1. Basic Word Document Comparison in Csharp
The simplest scenario is to compare two documents and mark the differences with tracked changes. This approach uses the Document.Compare method to highlight changes under a specified author name.
// Create a new Document object for the first document
Document doc1 = new Document();
doc1.LoadFromFile(@"SupportDocumentCompare1.docx");
// Create a new Document object for the second document
Document doc2 = new Document();
doc2.LoadFromFile(@"SupportDocumentCompare2.docx");
// Compare the contents of the two documents
doc1.Compare(doc2, "AuthorName");
// Save the result to a new file
string result = "CompareDocuments_result.docx";
doc1.SaveToFile(result, FileFormat.Docx2013);
// Release resources
doc1.Dispose();
doc2.Dispose();
This example demonstrates a direct comparison with default settings, capturing all differences, including formatting and structural changes.
2. Using Custom Options to Compare Word Documents in Csharp
You can customize the comparison behavior using the CompareOptions class. For example, you might want to ignore formatting differences, focus only on text, or exclude specific elements like tables or headers and footers.
2.1 Ignore Formatting Changes
Formatting changes can be ignored by setting the IgnoreFormatting property to true.
Document doc1 = new Document();
doc1.LoadFromFile(@"SupportDocumentCompare1.docx");
Document doc2 = new Document();
doc2.LoadFromFile(@"SupportDocumentCompare2.docx");
// Create CompareOptions and set IgnoreFormatting to true
CompareOptions compareOptions = new CompareOptions();
compareOptions.IgnoreFormatting = true;
// Compare documents with the specified options
doc1.Compare(doc2, "AuthorName", DateTime.Now, compareOptions);
doc1.SaveToFile("CompareDocumentsWithOptions_result.docx", FileFormat.Docx2013);
doc1.Dispose();
doc2.Dispose();
This example ensures that only textual content differences are highlighted while font styles, colors, and other formatting changes are ignored.
2.2 Ignoring Specific Elements
You can also ignore certain parts of the document such as headers, footers, and tables:
- Ignore Headers and Footers
compareOptions.IgnoreHeadersAndFooters = true;
- Ignore Tables
compareOptions.IgnoreTable = true;
These options help you focus on the content that is most important for your comparison.
3. Controlling Text Comparison Level in Word Documents with Csharp
The granularity of text comparison can be controlled using the TextCompareLevel property in CompareOptions. You can choose to compare documents word by word or character by character, depending on how precise you want the changes to be tracked.
CompareOptions compareOptions = new CompareOptions();
compareOptions.TextCompareLevel = TextDiffMode.Word; // or TextDiffMode.Char
Explanation:
TextDiffMode.Word– compares text word by word.TextDiffMode.Char– compares text character by character.
Choosing Word is usually sufficient for most documents, while Char provides more precise tracking for minor edits.
Tips for Comparing Word Documents in Csharp
Here are some practical tips to ensure your document comparisons in C# are accurate and meaningful:
Choose what to ignore wisely – Ignoring tables, headers, footers, or formatting can make the comparison cleaner, but only if these elements are not important for your use case.
Select the right text comparison level – Use Word for general comparisons and Char for detecting very fine changes.
Manage output files clearly – Name your comparison results to reflect the versions compared to avoid confusion when comparing multiple files.
Review the result visually – Even with automation, check the resulting document to ensure changes are captured as expected.
Summary
Comparing Word documents in C# can be simple or highly customizable, depending on your needs. You can start with a basic comparison, apply options to ignore formatting or specific elements, adjust the text comparison level, and handle large or complex documents efficiently. Automating document comparisons helps maintain consistency, track revisions effectively, and save time when working with multiple versions of a document.
Comments
Post a Comment