GroupDocs.Comparison allows you to get a list of changes between the source and target documents.
The following code snippets show how to get a list of all changes:
Get a list of changes from a local disk
The following example compares two local documents and then prints a detailed list of detected changes.
'use strict';// Import the GroupDocs.Comparison for Node.js via Java SDK
constgroupdocs=require('@groupdocs/groupdocs.comparison');// Create a Comparer instance for the source document on disk
constcomparer=newgroupdocs.Comparer('sample-files/source.docx');// Add the target document to be compared against the source
comparer.add('sample-files/target.docx');// Perform the comparison (no result file path is required for retrieving changes)
comparer.compare();// Retrieve the array of ChangeInfo objects that describe each change
constchanges=comparer.getChanges();// Iterate through the collection and print details of every change
for(letindex=0;index<changes.length;index++){constchange=changes[index];constpage=change.getPageInfo().getPageNumber();// Page number where the change is located
consttype=change.getType().name();// Type of change (Inserted, Deleted, etc.)
consttext=(change.getText()||'').trim();// Normalized change text
constsrc=(change.getSourceText()||'').trim();// Text from the source document
consttgt=(change.getTargetText()||'').trim();// Text from the target document
console.log(`[Page ${page}] ${type} "${text}", "${src}" -> "${tgt}"`);}// Exit the process when printing is finished
process.exit(0);
This example creates a Comparer instance with the source document, adds the target document, and performs the comparison. It then retrieves all detected changes using getChanges() and iterates through the collection to extract and display detailed information about each change, including the page number, change type (Inserted, Deleted, etc.), the change text, and the source and target text fragments.
The result is as follows:
Get a list of changes from a stream
The following example loads source and target documents from Java streams, compares them, and prints the list of changes.
'use strict';// Import the GroupDocs.Comparison for Node.js via Java SDK
constgroupdocs=require('@groupdocs/groupdocs.comparison');// Import the Java bridge to work with FileInputStream
constjava=require('java');letInputStream=java.import('java.io.FileInputStream');// Create input streams for the source and target documents
constsourceStream=newInputStream('sample-files/source.docx');consttargetStream=newInputStream('sample-files/target.docx');// Create a Comparer instance using the source stream
constcomparer=newgroupdocs.Comparer(sourceStream);// Add the target stream to be compared against the source
comparer.add(targetStream);// Run the comparison to populate internal changes collection
comparer.compare();// Retrieve the list of changes discovered during comparison
constchanges=comparer.getChanges();// Iterate through the collection and print detailed information about each change
for(letindex=0;index<changes.length;index++){constchange=changes[index];constpage=change.getPageInfo().getPageNumber();// Page number
consttype=change.getType().name();// Change type
consttext=(change.getText()||'').trim();// Combined change text
constsrc=(change.getSourceText()||'').trim();// Source text fragment
consttgt=(change.getTargetText()||'').trim();// Target text fragment
console.log(`[Page ${page}] ${type} "${text}", "${src}" -> "${tgt}"`);}// Exit the process when all changes are printed
process.exit(0);
This example demonstrates the same workflow using Java input streams instead of file paths. It creates InputStream objects for both source and target documents, initializes the comparer with the source stream, adds the target stream, performs the comparison, and then retrieves and prints the list of changes with the same detailed information as the previous example.
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.