GroupDocs.Comparison provides the compare options collection to set up the appropriate comparison speed and quality.
The following code snippets show how to compare documents with specific options.
Compare documents from a local disk with custom change styles
The following example compares two DOCX files from disk and applies custom styles to inserted, deleted, and changed content in the result.
'use strict';// Import the GroupDocs.Comparison for Node.js via Java SDK
constgroupdocs=require('@groupdocs/groupdocs.comparison');// Define file paths for source, target, and output documents
constsourceFile='sample-files/source.docx';consttargetFile='sample-files/target.docx';constresultFile='output/result.docx';// Initialize the comparer with the source document and add the target
constcomparer=newgroupdocs.Comparer(sourceFile);comparer.add(targetFile);// Create comparison options to configure visual styling of changes
constcompareOptions=newgroupdocs.CompareOptions();// Configure style for inserted items (text added in the target document)
constinsertedStyleSettings=newgroupdocs.StyleSettings();insertedStyleSettings.setUnderline(true);insertedStyleSettings.setBold(true);insertedStyleSettings.setStrikethrough(true);insertedStyleSettings.setItalic(true);compareOptions.setInsertedItemStyle(insertedStyleSettings);// Configure style for deleted items (text removed from the source document)
constdeletedStyleSettings=newgroupdocs.StyleSettings();deletedStyleSettings.setUnderline(true);deletedStyleSettings.setBold(true);deletedStyleSettings.setStrikethrough(true);deletedStyleSettings.setItalic(true);compareOptions.setDeletedItemStyle(deletedStyleSettings);// Configure style for changed items (text modified between documents)
constchangedStyleSettings=newgroupdocs.StyleSettings();changedStyleSettings.setUnderline(true);changedStyleSettings.setBold(true);changedStyleSettings.setStrikethrough(true);changedStyleSettings.setItalic(true);compareOptions.setChangedItemStyle(changedStyleSettings);// Run the comparison using the configured styling options
comparer.compare(resultFile,compareOptions);// Terminate the process with a success exit code
process.exit(0);
This example creates a Comparer instance with the source document, adds the target document, and creates a CompareOptions object. It then creates separate StyleSettings objects for inserted, deleted, and changed items, configuring each with underline, bold, strikethrough, and italic formatting. These style settings are applied to the comparison options, and when the comparison is performed, all detected changes will be styled according to these custom settings in the result document.
The result is as follows:
Compare documents from a stream with custom change styles
The following example performs the same comparison using document streams and applies color-based styles for each type of change.
'use strict';// Import GroupDocs.Comparison for Node.js via Java and Java interop utilities
constgroupdocs=require('@groupdocs/groupdocs.comparison');//----------------------------------------------------
//----------------------------------------------------
constjava=require('java');letInputStream=java.import('java.io.FileInputStream');letColor=java.import('java.awt.Color');//----------------------------------------------------
// Create input streams for the source and target documents
constsourceInputStream=newInputStream('sample-files/source.docx');consttargetInputStream=newInputStream('sample-files/target.docx');//----------------------------------------------------
// Initialize the comparer with the source document stream
constcomparer=newgroupdocs.Comparer(sourceInputStream);//----------------------------------------------------
// Add the target document stream to the comparer
comparer.add(targetInputStream);//----------------------------------------------------
// Prepare comparison options to customize visual styles
constcompareOptions=newgroupdocs.CompareOptions();//----------------------------------------------------
// Configure style for inserted items (e.g., text added in the target)
constinsertedStyleSettings=newgroupdocs.StyleSettings();insertedStyleSettings.setHighlightColor(Color.RED);// Highlight inserted text in red
insertedStyleSettings.setFontColor(Color.GREEN);// Set font color to green
insertedStyleSettings.setUnderline(true);// Underline inserted text
insertedStyleSettings.setBold(true);// Make inserted text bold
insertedStyleSettings.setStrikethrough(true);// Apply strikethrough (optional visual cue)
insertedStyleSettings.setItalic(true);// Italicize inserted text
compareOptions.setInsertedItemStyle(insertedStyleSettings);// Apply inserted style to options
//----------------------------------------------------
// Configure style for deleted items (e.g., text removed from the source)
constdeletedStyleSettings=newgroupdocs.StyleSettings();deletedStyleSettings.setHighlightColor(Color.PINK);// Highlight deleted text in pink
deletedStyleSettings.setFontColor(Color.CYAN);// Set font color to cyan
deletedStyleSettings.setUnderline(true);// Underline deleted text
deletedStyleSettings.setBold(true);// Make deleted text bold
deletedStyleSettings.setStrikethrough(true);// Strike through deleted text
deletedStyleSettings.setItalic(true);// Italicize deleted text
compareOptions.setDeletedItemStyle(deletedStyleSettings);// Apply deleted style to options
//----------------------------------------------------
// Configure style for changed items (e.g., text modified between documents)
constchangedStyleSettings=newgroupdocs.StyleSettings();changedStyleSettings.setHighlightColor(Color.LIGHT_GRAY);// Highlight changes in light gray
changedStyleSettings.setFontColor(Color.GRAY);// Set font color to gray
changedStyleSettings.setUnderline(true);// Underline changed text
changedStyleSettings.setBold(true);// Make changed text bold
changedStyleSettings.setStrikethrough(true);// Strikethrough changed text
changedStyleSettings.setItalic(true);// Italicize changed text
compareOptions.setChangedItemStyle(changedStyleSettings);// Apply changed style to options
//----------------------------------------------------
// Define the output path for the comparison result document
constresultPath='result.docx';//----------------------------------------------------
// Execute the comparison and save the result using the defined options
comparer.compare(resultPath,compareOptions);// Terminate the process with a success exit code
process.exit(0);
This example demonstrates the same workflow using Java input streams instead of file paths. It creates InputStream objects for both documents, initializes the comparer with the source stream, adds the target stream, and creates CompareOptions with custom StyleSettings. In this case, the styles include color settings: inserted items are highlighted in red with green font, deleted items are highlighted in pink with cyan font, and changed items are highlighted in light gray with gray font. All items also have underline, bold, strikethrough, and italic formatting applied.
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.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.