Skip to content

Instantly share code, notes, and snippets.

@GroupDocsGists
Last active April 18, 2023 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GroupDocsGists/2bc5a8376473a179e59310fd3dc23a9f to your computer and use it in GitHub Desktop.
Save GroupDocsGists/2bc5a8376473a179e59310fd3dc23a9f to your computer and use it in GitHub Desktop.
Compare Word Documents, Find Differences, and Accept or Reject Changes using C#
/*
* Accept and reject identified changes by comparing Word documents using C#
*/
using (Comparer comparer = new Comparer("path/document-1.docx"))
{
comparer.Add("path/document-2.docx");
comparer.Compare();
ChangeInfo[] changes = comparer.GetChanges();
// Rejecting the first identified change and it will not be added to result document
changes[0].ComparisonAction = ComparisonAction.Reject;
comparer.ApplyChanges("path/rejected-change-result.docx", new ApplyChangeOptions { Changes = changes, SaveOriginalState = true });
changes = comparer.GetChanges();
changes[0].ComparisonAction = ComparisonAction.Accept;
comparer.ApplyChanges("path/accepted-change-result.docx", new ApplyChangeOptions { Changes = changes });
}
/*
* Compare Multiple Word Documents using C#
*/
using (Comparer comparer = new Comparer("path/document-1.docx"))
{
comparer.Add("path/document-2.docx");
comparer.Add("path/document-3.docx");
comparer.Add("path/document-4.docx");
comparer.Compare("path/compare-result.docx");
}
/*
* Compare Password Protected Word Documents using C#
*/
using (Comparer comparer = new Comparer("path/protected-document-1.docx", new LoadOptions(){ Password = "SourceFilePassword" }))
{
comparer.Add("path/protected-document-2.docx", new LoadOptions() { Password = "TargetFilePassword" });
comparer.Compare("path/compared-protected-docs-result.docx");
}
/*
* Compare Two Word Documents and Highlight Changes using C#
*/
using (Comparer comparer = new Comparer("path/document.docx"))
{
comparer.Add("path/document-ver2.docx");
comparer.Compare("path/compared-result.docx");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment