GroupDocs.Comparison allows you to get source and target texts of specific changes in the output file.
The following code snippets show how to get specified texts from a file.
Get source and target text from a local disk
The following example compares two DOCX files from disk and prints the source and target text for each detected change.
'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
constsourcePath='sample-files/source.docx';consttargetPath='sample-files/target.docx';constoutputPath='result.docx';// Initialize the comparer with the source document
constcomparer=newgroupdocs.Comparer(sourcePath);// Add the target document to the comparison set
comparer.add(targetPath);// Execute the comparison and write the result to the output file
comparer.compare(outputPath);// Retrieve the list of detected changes after comparison
constchanges=comparer.getChanges();// Iterate over each change and display its source/target text fragments
for(leti=0;i<changes.length;i++){constchange=changes[i];// Blank line for readability between changes
console.log();// Output text from the source and target documents for this change
console.log('Source text: ',change.getSourceText());console.log('Target text: ',change.getTargetText());}// 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 performs the comparison. After the comparison, it retrieves all detected changes using getChanges() and iterates through them to extract and display the source text (original text from the source document) and target text (modified text from the target document) for each change. This allows you to see exactly what text was changed between the documents.
The result is as follows:
Get source and target text from a stream
The following example performs the same operation using Java input streams instead of file paths.
'use strict';// Import the GroupDocs.Comparison for Node.js via Java SDK
constgroupdocs=require('@groupdocs/groupdocs.comparison');// Import Java bridge to work with FileInputStream
constjava=require('java');constInputStream=java.import('java.io.FileInputStream');// Create input streams for source and target documents
constsourceInputStream=newInputStream('sample-files/source.docx');consttargetInputStream=newInputStream('sample-files/target.docx');// Define the output file path for the comparison result
constoutputPath='result.docx';// Initialize the comparer with the source document stream
constcomparer=newgroupdocs.Comparer(sourceInputStream);// Add the target document stream to the comparer
comparer.add(targetInputStream);// Perform the comparison and generate the result file
comparer.compare(outputPath);// Retrieve the list of detected changes
constchanges=comparer.getChanges();// Iterate over each change and output its source/target text portions
for(leti=0;i<changes.length;i++){constchange=changes[i];console.log('Source text: ',change.getSourceText());console.log('Target text: ',change.getTargetText());}// 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 source and target documents, initializes the comparer with the source stream, adds the target stream, performs the comparison, and then retrieves and displays the source and target text for each detected change, just like the previous example but using streams.
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.