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. Call the setComparisonAction() method of the appropriate change object. Specify the ComparisonAction.ACCEPT or the ComparisonAction.REJECT value.
  6. Call the applyChanges() method. Specify the collection of changes.

The ApplyChangeOptions class includes the following properties:

  • getChanges is a list of changes that must be applied (or not) to the output document
  • isSaveOriginalState 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

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    final Path resultPath = comparer.compare();
    ChangeInfo[] changes = comparer.getChanges();
    changes[0].setComparisonAction(ComparisonAction.REJECT);
    comparer.applyChanges(resultFile, new SaveOptions(), new ApplyChangeOptions(changes));
}

The result is as follows:

Accepted changesRejected changes

Accept or reject changes for documents provided as a stream

try (Comparer comparer = new Comparer(sourceInputStream)) {
    comparer.add(targetInputStream);
    final Path resultPath = comparer.compare(new SaveOptions(), new CompareOptions());
    ChangeInfo[] changes = comparer.getChanges();
    changes[0].setComparisonAction(ComparisonAction.REJECT);
    comparer.applyChanges(resultOutputStream, new ApplyChangeOptions(changes));
}

Accept or reject detected changes using SaveOriginalState option

try (Comparer comparer = new Comparer(sourceFile)) {
	comparer.add(targetFile);
    final Path resultPath = comparer.compare();
    ChangeInfo[] changes = comparer.getChanges();
    changes[0].setComparisonAction(ComparisonAction.REJECT);
    ApplyChangeOptions changeOptions = new ApplyChangeOptions();
    changeOptions.setChanges(changes);
    changeOptions.setSaveOriginalState(true);
    comparer.applyChanges(resultFileWithRejectedChange, changeOptions);
    changes = comparer.getChanges();
    changes[0].setComparisonAction(ComparisonAction.ACCEPT);
    comparer.applyChanges(resultFileWithAcceptedChange, new ApplyChangeOptions(changes));
}