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

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.ApplyChangeOptions;
import com.groupdocs.comparison.options.save.SaveOptions;
import com.groupdocs.comparison.result.ChangeInfo;
import com.groupdocs.comparison.result.ComparisonAction;
import java.nio.file.Path;
// ...

try (Comparer comparer = new Comparer("source.docx")) {
    comparer.add("target.docx");
    final Path resultPath = comparer.compare();
    ChangeInfo[] changes = comparer.getChanges();
    changes[0].setComparisonAction(ComparisonAction.REJECT);
    comparer.applyChanges("result.docx", new SaveOptions(), new ApplyChangeOptions(changes));
}

The result is as follows:

Accepted changesRejected changes

Accept or reject changes for documents provided as a stream

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.ApplyChangeOptions;
import com.groupdocs.comparison.options.CompareOptions;
import com.groupdocs.comparison.options.save.SaveOptions;
import com.groupdocs.comparison.result.ChangeInfo;
import com.groupdocs.comparison.result.ComparisonAction;
import java.nio.file.Path;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
// ...

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

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.options.ApplyChangeOptions;
import com.groupdocs.comparison.result.ChangeInfo;
import com.groupdocs.comparison.result.ComparisonAction;
import java.nio.file.Path;
// ...

try (Comparer comparer = new Comparer("source.docx")) {
    comparer.add("target.docx");
    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));
}