Get source and target text from files

GroupDocs.Comparison for Java allows to getting source and target texts of specific changes in result file.

The following are the steps to get a list of changed source and target texts:

  • Instantiate Comparer object with source document path or stream;
  • Call add method and specify target document path or stream;
  • Call compare method;
  • Call getChanges method.

The following code example demonstrates how to get specified texts from a file.

Get target text from local disk

try (Comparer comparer = new Comparer(sourceDocumentPath)) {
    comparer.add(targetDocumentPath);
    final Path resultPath = comparer.compare(outputPath);
    ChangeInfo[] changes = comparer.getChanges();
    for (ChangeInfo change : changes) {
        System.out.println();
        System.out.println("Source text: " + change.getSourceText());
        System.out.println("Target text: " + change.getTargetText());
    }
}

Get target text from stream

try (InputStream inputStream = new FileInputStream(sourceDocumentPath);
        Comparer comparer = new Comparer(inputStream)) {
        comparer.add(targetDocumentPath);
        final Path resultPath = comparer.compare(outputPath);
        ChangeInfo[] changes = comparer.getChanges();
        for (ChangeInfo change : changes) {
        System.out.println();
        System.out.println("Source text: " + change.getSourceText());
        System.out.println("Target text: " + change.getTargetText());
    }
}

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured Java library we provide simple, but powerful free Apps.
You are welcome to compare your DOC or DOCX, XLS or XLSX, PPT or PPTX, PDF, EML, EMLX, MSG and other documents with free to use online GroupDocs Comparison App.