Accept or Reject detected changes
GroupDocs.Comparison provides an ability to apply or discard specific changes between source and target documents and save resultant document with (or without) selected changes.
The following are the steps to apply/reject changes to resultant document.
- Instantiate Comparer object with source document path or stream;
- Call Add method and specify path target document path or stream;
- Call Compare method;
- Call GetChanges method and obtain detected changes list;
- Set ComparisonAction of needed change object to ComparisonAction.Accept or ComparisonAction.Reject value;
- Call ApplyChanges method and pass collection of changes to it.
ApplyChangeOptions class:
- Changes - List of changes that must be applied (or not) to the resulting document;
- SaveOriginalState - Keep the original state of the compared result after applying changes.
The following code sample shows how to accept/reject detected changes.
Accept or Reject changes for documents stored at local disk
using (Comparer comparer = new Comparer("source.docx"))
{
comparer.Add("target.docx");
comparer.Compare();
ChangeInfo[] changes = comparer.GetChanges();
changes[0].ComparisonAction = ComparisonAction.Reject;
comparer.ApplyChanges(File.Create("result.docx"), new SaveOptions(), new ApplyChangeOptions() { Changes = changes });
}
Accept or Reject changes for documents provided as a stream
using (Comparer comparer = new Comparer(File.OpenRead("source.docx")))
{
comparer.Add(File.OpenRead("target.docx"));
comparer.Compare(new SaveOptions(), new CompareOptions());
ChangeInfo[] changes = comparer.GetChanges(new GetChangeOptions());
changes[0].ComparisonAction = ComparisonAction.Reject;
comparer.ApplyChanges(File.Create("result.docx"), new SaveOptions(), new ApplyChangeOptions() { Changes = changes });
}
The following code sample shows how to accept/reject detected changes using SaveOriginalState option
using (Comparer comparer = new Comparer("source.docx"))
{
comparer.Add("target.docx");
comparer.Compare();
ChangeInfo[] changes = comparer.GetChanges();
changes[0].ComparisonAction = ComparisonAction.Reject;
comparer.ApplyChanges("resultWithRejectedChange.docx", new ApplyChangeOptions() { Changes = changes, SaveOriginalState = true });
changes = comparer.GetChanges();
changes[0].ComparisonAction = ComparisonAction.Accept;
comparer.ApplyChanges("resultWithAcceptedChange.docx", new ApplyChangeOptions() { Changes = changes });
}
More resources
GitHub Examples
You may easily run the code above and see the feature in action in our GitHub examples:
- GroupDocs.Comparison for .NET examples, plugins, and showcase
- GroupDocs.Comparison for Java examples, plugins, and showcase
- Document Comparison for .NET MVC UI Example
- Document Comparison for .NET App WebForms UI Modern Example
- Document Comparison for Java App Dropwizard UI Modern Example
- Document Comparison for Java Spring UI Example
Free Online App
Along with full-featured .NET library we provide simple, but powerful free Apps.
You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSG and other documents with free to use online GroupDocs Comparison App.