officefileapi-405437-word-processing-document-api-compare-documents.md
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:
Important
The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.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);
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Private wordProcessor = New RichEditDocumentServer()
Private wordProcessorRevised = New RichEditDocumentServer()
wordProcessor.LoadDocument("C:\Documents\sample text original.docx")
wordProcessorRevised.LoadDocument("C:\Documents\sample text revised.docx")
Dim document As 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.
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);
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Private wordProcessor = New RichEditDocumentServer()
Private wordProcessorRevised = New RichEditDocumentServer()
wordProcessor.LoadDocument("C:\Documents\sample text original.docx")
wordProcessorRevised.LoadDocument("C:\Documents\sample text revised.docx")
Dim document As Document = wordProcessor.Document.Compare(wordProcessorRevised.Document, ComparisonTargetType.Revised)
wordProcessorRevised.SaveDocument("comparison.docx", DocumentFormat.Docx)
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);
Imports DevExpress.XtraRichEdit
Imports DevExpress.XtraRichEdit.API.Native
Private wordProcessor = New RichEditDocumentServer()
Private wordProcessorRevised = New RichEditDocumentServer()
wordProcessor.LoadDocument("C:\Documents\sample text original.docx")
wordProcessorRevised.LoadDocument("C:\Documents\sample text revised.docx")
Dim options As New CompareDocumentOptions()
options.CompareFormatting = False
options.CompareCaseChanges = False
options.ComparisonLevel = ComparisonLevel.Word
options.Author = "Nancy Doe"
options.DateTime = DateTime.Now
Dim document As Document = wordProcessor.Document.Compare(wordProcessorRevised.Document, options)
document.SaveDocument("comparison.docx", DocumentFormat.Docx)
The following document elements are ignored when Word Processing Document API compares two documents: