GroupDocs.Comparison allows you to detect changes between the source and target documents and get coordinates of the changes in the document preview images. These coordinates can be useful to highlight changes in the document preview images.
The following code snippet shows how to compare documents and obtain coordinates for each detected change:
'use strict';// Import the GroupDocs Comparison Node.js SDK
constgroupdocs=require('@groupdocs/groupdocs.comparison');// Define file paths for the source, target, and result documents
constsourceFile='sample-files/source.docx';consttargetFile='sample-files/target.docx';constresultFile='result.docx';// Create a comparer instance for the source document
constcomparer=newgroupdocs.Comparer(sourceFile);// Add the target document to the comparer
comparer.add(targetFile);// Initialize comparison options
constcompareOptions=newgroupdocs.CompareOptions();compareOptions.setCalculateCoordinates(true);// Execute the comparison and save the result to the specified file
comparer.compare(resultFile,compareOptions);// Retrieve the list of detected changes
constchanges=comparer.getChanges();// Iterate through each change and output its details
for(leti=0;i<changes.length;i++){constchange=changes[i];console.log(`Change Type: ${change.getType()}, X: ${change.getBox().getX()}, Y: ${change.getBox().getY()}, Text: ${change.getText()}`);}// Exit the process
process.exit(0);
This example creates a Comparer instance, adds the target document, and configures comparison options to calculate coordinates by setting setCalculateCoordinates(true). After performing the comparison, it retrieves all detected changes using getChanges() and iterates through them to extract and display coordinate information (X, Y positions) from each change’s Box object, along with the change type and text content.
The result is as follows:
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.