Accept or Reject detected changes

GroupDocs.Comparison allows you to apply or discard specific changes between source and target documents and save output document with (or without) selected changes.

To apply/reject changes to output document, follow these steps:

  1. Instantiate the Comparer object. Specify the source document path or stream.
  2. Call the Add method. Specify the target document path or stream.
  3. Call the Compare method.
  4. Call the GetChanges method to get changes list.
  5. Set the ComparisonAction of the appropriate change object to the ComparisonAction.Accept or ComparisonAction.Reject value.
  6. Call the ApplyChanges method. Specify the collection of changes.

ApplyChangeOptions class includes the following properties:

  • Changes is a list of changes that must be applied (or not) to the output document
  • SaveOriginalState is an option to reep the original state of the compared result after applying changes

The following code snippets show how to accept/reject 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 });
}

The result is as follows:

Accepted changesRejected 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 });
}

Accept or 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 });
}