Use Word Processing Document API to Compare Word Documents
- 3 minutes to read
The Word Processing Document API ships with the RichEditDocumentServerExtensions.Compare method that allows you to compare two Word documents in any format. The method returns a new file with revisions that show the difference between two documents. The resulting document is loaded to a new SubDocument instance. You can save the result in one of the following formats:
- DOCX/DOCM
- DOTX/DOTM
- DOC
- RTF
- WordML
- HTML
Important
The RichEditDocumentServerExtensions
class is defined in the DevExpress.Docs.v25.1.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package to your project to use the RichEditDocumentServerExtensions
members. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.
The following code snippet compares two documents loaded into two RichEditDocumentServer
instances:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
var wordProcessor = new RichEditDocumentServer();
var wordProcessorRevised = new RichEditDocumentServer();
wordProcessor.LoadDocument("C:\\Documents\\sample text original.docx");
wordProcessorRevised.LoadDocument("C:\\Documents\\sample text revised.docx");
Document document = wordProcessor.Document.Compare(wordProcessorRevised.Document);
document.SaveDocument("comparison.docx", DocumentFormat.Docx);
Note
The input documents must not include document revisions. Otherwise, the Compare
method throws an exception.
Change the Location of the Comparison Result
The RichEditDocumentServerExtensions.Compare(Document, Document) method returns a new SubDocument instance that contains a file with revisions. To change this behavior, pass one of the ComparisonTargetType enumeration values as the Compare
method parameter. This enumeration allows you to specify whether the original or revised document should be replaced with the document with revisions.
The following code snippet replaces the revised document with the comparison result:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
var wordProcessor = new RichEditDocumentServer();
var wordProcessorRevised = new RichEditDocumentServer();
wordProcessor.LoadDocument("C:\\Documents\\sample text original.docx");
wordProcessorRevised.LoadDocument("C:\\Documents\\sample text revised.docx");
wordProcessor.Document.Compare(wordProcessorRevised.Document, ComparisonTargetType.Revised);
wordProcessorRevised.SaveDocument("comparison.docx", DocumentFormat.Docx);
Customize Comparison Options
Use the CompareDocumentOptions properties to adjust comparison sensitivity. You can also specify the author and the date of the revisions.
The following code snippet specifies comparison settings and passes the CompareDocumentOptions
instance as the Compare
method parameter:
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
var wordProcessor = new RichEditDocumentServer();
var wordProcessorRevised = new RichEditDocumentServer();
wordProcessor.LoadDocument("C:\\Documents\\sample text original.docx");
wordProcessorRevised.LoadDocument("C:\\Documents\\sample text revised.docx");
CompareDocumentOptions options = new CompareDocumentOptions();
options.CompareFormatting = false;
options.CompareCaseChanges = false;
options.ComparisonLevel = ComparisonLevel.Word;
options.Author = "Nancy Doe";
options.DateTime = DateTime.Now;
Document document = wordProcessor.Document.Compare(wordProcessorRevised.Document, options);
document.SaveDocument("comparison.docx", DocumentFormat.Docx);