Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare Word Documents using REST API in Node.js
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result_compareOptions.docx";
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file 1
let target1 = new groupdocs_comparison_cloud.FileInfo();
target1.filePath = "target1.docx";
// target file 2
let target2 = new groupdocs_comparison_cloud.FileInfo();
target2.filePath = "target2.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target1, target2];
options.outputPath = "compared/result_Multiple.docx";
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);
global.clientId = "112f0f38-9dae-42d5-b4fc-cc84ae644972";
global.clientSecret = "16ad3fe0bdc39c910f57d2fd48a5d618";
global.myStorage = "";
const config = new groupdocs_comparison_cloud.Configuration(clientId, clientSecret);
config.apiBaseUrl = "https://api.groupdocs.cloud";
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare settings
let settings = new groupdocs_comparison_cloud.Settings();
// compare sensitivity
settings.sensitivityOfComparison = 100;
// customize changes style for inserted items
settings.insertedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.insertedItemsStyle.highlightColor = "14297642";
settings.insertedItemsStyle.fontColor = "16711680";
settings.insertedItemsStyle.underline = true;
// customize changes style for deleted items
settings.deletedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.deletedItemsStyle.fontColor = "14166746";
settings.deletedItemsStyle.bold = true;
// customize changes style for changed items
settings.changedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.changedItemsStyle.fontColor = "14320170";
settings.changedItemsStyle.italic = true;
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result_compareOptions.docx";
options.settings = settings;
// create comparisons request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
console.log("Output file link: " + response.href);
// construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_comparison_cloud.DownloadFileRequest("compared/result_compareOptions.docx", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save in the working directory
fs.writeFile("C:\\Files\\comparison\\result_compareOptions.docx", response, "binary", function (err) { });
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.docx";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.docx";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result.docx";
// create post changes request
let request = new groupdocs_comparison_cloud.PostChangesRequest(options);
// post changes
let changes = await compareApi.postChanges(request);
console.log("Changes count: " + changes.length);
changes.forEach(change => {
console.log(change.id + 1 +"- Target Text: " + change.targetText + ", Text: " + change.text + ", Type: " + change.type);
});
// construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
let resourcesFolder = 'C:\\Files\\comparison\\uploads\\';
fs.readdir(resourcesFolder, (err, files) => {
files.forEach(file => {
console.log(file);
fs.readFile(resourcesFolder + file, (err, fileStream) => {
let request = new groupdocs_comparison_cloud.UploadFileRequest(file, fileStream, myStorage);
fileApi.uploadFile(request);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment