Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Compare PDF Files using REST API in Node.js
Compare PDF Files using REST API in Node.js
1. Document Comparison REST API and Node.js SDK
2. Compare PDF Files using a REST API in Node.js
2.1 Upload PDF files to the Cloud
2.2 Compare PDF Files using Node.js
2.3 Download the resultant file
3. Compare Multiple PDF Files using Node.js
4. Get List of Changes using Node.js
5. Compare and Save with Password and Metadata using 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.pdf";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.pdf";
let settings = new groupdocs_comparison_cloud.Settings();
// set metadata
settings.metadata = new groupdocs_comparison_cloud.Metadata();
settings.metadata.author = "Author";
settings.metadata.company = "GroupDocs";
settings.metadata.lastSaveBy = "Last saved by";
settings.cloneMetadata = groupdocs_comparison_cloud.Settings.CloneMetadataEnum.FileAuthor;
// set password
settings.password = "password";
settings.passwordSaveOption = groupdocs_comparison_cloud.Settings.PasswordSaveOptionEnum.User;
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result_Metadata.pdf";
options.settings = settings;
// create comparison request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.pdf";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.pdf";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result.pdf";
// create comparison 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.pdf";
// target file 1
let target1 = new groupdocs_comparison_cloud.FileInfo();
target1.filePath = "target.pdf";
// target file 2
let target2 = new groupdocs_comparison_cloud.FileInfo();
target2.filePath = "target2.pdf";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target1, target2];
options.outputPath = "compared/result.pdf";
// create comparison request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
// initialize api
let compareApi = groupdocs_comparison_cloud.CompareApi.fromKeys(clientId, clientSecret);
// source file
let source = new groupdocs_comparison_cloud.FileInfo();
source.filePath = "source.pdf";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.pdf";
// define compare settings
let settings = new groupdocs_comparison_cloud.Settings();
// compare sensitivity
settings.sensitivityOfComparison = 100;
// customize changes style
settings.insertedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.insertedItemsStyle.highlightColor = "14297642";
settings.insertedItemsStyle.fontColor = "16711680";
settings.insertedItemsStyle.underline = true;
settings.deletedItemsStyle = new groupdocs_comparison_cloud.ItemsStyle();
settings.deletedItemsStyle.fontColor = "14166746";
settings.deletedItemsStyle.bold = true;
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.pdf";
options.settings = settings;
// create comparison request
let request = new groupdocs_comparison_cloud.ComparisonsRequest(options);
// compare
let response = await compareApi.comparisons(request);
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";
// construct FileApi
let fileApi = groupdocs_comparison_cloud.FileApi.fromConfig(config);
// create download file request
let request = new groupdocs_comparison_cloud.DownloadFileRequest("compared\\result.pdf", myStorage);
// download file
let response = await fileApi.downloadFile(request);
// save in the working directory
fs.writeFile("C:\\Files\\result.pdf", 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.pdf";
// target file
let target = new groupdocs_comparison_cloud.FileInfo();
target.filePath = "target.pdf";
// define compare options
let options = new groupdocs_comparison_cloud.ComparisonOptions();
options.sourceFile = source;
options.targetFiles = [target];
options.outputPath = "compared/result.pdf";
// create comparison request
let request = new groupdocs_comparison_cloud.PostChangesRequest(options);
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\\';
fs.readdir(resourcesFolder, (err, files) => {
files.forEach(file => {
// read files one by one
fs.readFile(resourcesFolder + file, (err, fileStream) => {
// create upload file request
let request = new groupdocs_comparison_cloud.UploadFileRequest(file, fileStream, myStorage);
// upload file
fileApi.uploadFile(request)
.then(function (response) {
console.log(file + " uploaded: " + response.uploaded.length);
})
.catch(function (error) {
console.log("Error: " + error.message);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment