Accept or Reject detected changes
Leave feedback
On this page
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:
- Instantiate the Comparer object. Specify the source document path or stream.
- Call the add() method. Specify the target document path or stream.
- Call the compare() method.
- Call the getChanges() method to get changes list.
- Call the setComparisonAction() method of the appropriate change object. Specify the ComparisonAction.ACCEPT or the ComparisonAction.REJECT value.
- 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:
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 changes | Rejected changes |
---|---|
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));
}
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));
}
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.