Accept or Reject detected changes

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

To apply/reject changes to the 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 the 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 keep 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 on a local disk

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
comparer.compare();
let changes = comparer.getChanges();
changes[0].setComparisonAction(groupdocs.comparison.ComparisonAction.REJECT);
comparer.applyChanges(resultFile, new groupdocs.comparison.SaveOptions(), new groupdocs.comparison.ApplyChangeOptions(changes));

The result is as follows:

Accepted changesRejected changes

Accept or reject changes for documents provided as a stream

const comparer = new groupdocs.comparison.Comparer(sourceInputStream);
comparer.add(targetInputStream);
comparer.compare(new groupdocs.comparison.SaveOptions(), new CompareOptions());
let changes = comparer.getChanges();
changes[0].setComparisonAction(groupdocs.comparison.ComparisonAction.REJECT);
comparer.applyChanges(resultOutputStream, new groupdocs.comparison.ApplyChangeOptions(changes));

Accept or reject detected changes using the SaveOriginalState option

const comparer = new groupdocs.comparison.Comparer(sourceFile);
comparer.add(targetFile);
comparer.compare();
let changes = comparer.getChanges();
changes[0].setComparisonAction(groupdocs.comparison.ComparisonAction.REJECT);
let changeOptions = new groupdocs.comparison.ApplyChangeOptions();
changeOptions.setChanges(changes);
changeOptions.setSaveOriginalState(true);
comparer.applyChanges(resultFileWithRejectedChange, changeOptions);
changes = comparer.getChanges();
changes[0].setComparisonAction(groupdocs.comparison.ComparisonAction.ACCEPT);
comparer.applyChanges(resultFileWithAcceptedChange, new groupdocs.comparison.ApplyChangeOptions(changes));