Customize changes styles

GroupDocs.Comparison provides the compare options collection to set up the appropriate comparison speed and quality.

To compare two documents with custom change style settings, 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. Instantiate the CompareOptions object. Specify the appropriate parameters.
  4. Call the compare() method. Specify the CompareOptions object.

The following code snippets show how to compare documents with specific options:

Compare documents from local disk with custom change styles

try (Comparer comparer = new Comparer(sourceFile)) {
    comparer.add(targetFile);
    
    CompareOptions compareOptions = new CompareOptions();

    final StyleSettings insertedStyleSettings = new StyleSettings();
    insertedStyleSettings.setHighlightColor(Color.RED);
    insertedStyleSettings.setFontColor(Color.GREEN);
    insertedStyleSettings.setUnderline(true);
    insertedStyleSettings.setBold(true);
    insertedStyleSettings.setStrikethrough(true);
    insertedStyleSettings.setItalic(true);
    compareOptions.setInsertedItemStyle(insertedStyleSettings);

    final StyleSettings deletedStyleSettings = new StyleSettings();
    deletedStyleSettings.setHighlightColor(Color.PINK);
    deletedStyleSettings.setFontColor(Color.CYAN);
    deletedStyleSettings.setUnderline(true);
    deletedStyleSettings.setBold(true);
    deletedStyleSettings.setStrikethrough(true);
    deletedStyleSettings.setItalic(true);
    compareOptions.setDeletedItemStyle(deletedStyleSettings);

    final StyleSettings changedStyleSettings = new StyleSettings();
    changedStyleSettings.setHighlightColor(Color.LIGHT_GRAY);
    changedStyleSettings.setFontColor(Color.GRAY);
    changedStyleSettings.setUnderline(true);
    changedStyleSettings.setBold(true);
    changedStyleSettings.setStrikethrough(true);
    changedStyleSettings.setItalic(true);
    compareOptions.setChangedItemStyle(changedStyleSettings);

    final Path resultPath = comparer.compare(resultFile, compareOptions);
}

The result is as follows:

Compare documents from stream with custom change styles

try (Comparer comparer = new Comparer(sourceInputStream)) {
    comparer.add(targetInputStream);
    
    CompareOptions compareOptions = new CompareOptions();

    final StyleSettings insertedStyleSettings = new StyleSettings();
    insertedStyleSettings.setHighlightColor(Color.RED);
    insertedStyleSettings.setFontColor(Color.GREEN);
    insertedStyleSettings.setUnderline(true);
    insertedStyleSettings.setBold(true);
    insertedStyleSettings.setStrikethrough(true);
    insertedStyleSettings.setItalic(true);
    compareOptions.setInsertedItemStyle(insertedStyleSettings);

    final StyleSettings deletedStyleSettings = new StyleSettings();
    deletedStyleSettings.setHighlightColor(Color.PINK);
    deletedStyleSettings.setFontColor(Color.CYAN);
    deletedStyleSettings.setUnderline(true);
    deletedStyleSettings.setBold(true);
    deletedStyleSettings.setStrikethrough(true);
    deletedStyleSettings.setItalic(true);
    compareOptions.setDeletedItemStyle(deletedStyleSettings);

    final StyleSettings changedStyleSettings = new StyleSettings();
    changedStyleSettings.setHighlightColor(Color.LIGHT_GRAY);
    changedStyleSettings.setFontColor(Color.GRAY);
    changedStyleSettings.setUnderline(true);
    changedStyleSettings.setBold(true);
    changedStyleSettings.setStrikethrough(true);
    changedStyleSettings.setItalic(true);
    compareOptions.setChangedItemStyle(changedStyleSettings);

    final Path resultPath = comparer.compare(resultInputStream, compareOptions);
}