Work with the comparison result

GroupDocs.Comparison gives you two things after a comparison: the result document and the list of detected changes.

To work with the comparison result, 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. It returns the java.nio.file.Path of the result document.
  4. Call the getChanges() method to get the detected changes as an array of ChangeInfo objects.
Note
Use the Path returned by compare() rather than the path you passed in: in some situations the library changes the extension of the result file.

The following code snippets show how to get the changes and how to modify the result document with Aspose.Words for Java:

Get the result document and the list of changes

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

try (Comparer comparer = new Comparer("source.docx")) {
    comparer.add("target.docx");
    final Path resultPath = comparer.compare("result.docx");
    System.out.println("Result document: " + resultPath);

    ChangeInfo[] changes = comparer.getChanges();
    for (ChangeInfo change : changes) {
        System.out.println("Source text: " + change.getSourceText());
        System.out.println("Target text: " + change.getTargetText() + "\n");
    }
}

Modify the result document with Aspose.Words

The result is a regular document of the compared format, so you can open it with any library that supports the format. The example below opens a Word result with Aspose.Words for Java and appends some text to it. Aspose.Words for Java is a separate product — add it to your project as its own dependency.

import com.groupdocs.comparison.Comparer;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import java.nio.file.Path;
// ...

try (Comparer comparer = new Comparer("source.docx")) {
    // Add target document and save comparison result
    comparer.add("target.docx");
    final Path resultPath = comparer.compare("result.docx");

    // Open the result document with Aspose.Words
    Document asposeDocument = new Document(resultPath.toString());

    // Access the Document's builder to add content.
    DocumentBuilder builder = new DocumentBuilder(asposeDocument);

    // Add some text to the document.
    builder.writeln("Hello, World!");
    builder.write("This is an example of using Aspose.Words.");

    // Save the document to a file.
    asposeDocument.save("output.docx");
}

See also

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.