How to Validate MS Office Documents using APIs in Nodejs
Using APIs to validate our Office files, we’ll easily automate an important step in our file processing workflows.
We’ll identify any errors or warnings associated with invalid documents before those documents reach sensitive locations, and we’ll also find out if Office documents contain password protection measures. If our documents came from an untrusted external location (e.g., a file upload form), the existence of password protection can indicate a potential threat.
To structure our API calls, we can copy from the complementary, ready-to-run Nodejs code examples provided below. We’ll need a free Cloudmersive API key to authorize our requests, and we’ll be able to make up to 800 API calls per month with no additional commitments.
First, we can run the following NPM command to install the SDK:
npm install cloudmersive-convert-api-client --save
Or we can simply add this snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Next, we can call our document validation functions. We can use the below code examples to validate Word (DOCX), Excel (XLSX), and PowerPoint (PPTX) files respectively, and we can replace the ‘YOUR API KEY’
placeholder text with our API key to authorize each request.
To validate Word documents, let’s use the below code examples:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentDocxValidation(inputFile, callback);
To validate Excel documents, let’s use the below code:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentXlsxValidation(inputFile, callback);
Finally, to validate our PowerPoint documents, let’s use the below code:
var CloudmersiveConvertApiClient = require('cloudmersive-convert-api-client');
var defaultClient = CloudmersiveConvertApiClient.ApiClient.instance;
// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.ValidateDocumentApi();
var inputFile = Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer); // File | Input file to perform the operation on.
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.validateDocumentPptxValidation(inputFile, callback);
That’s all there is to it — now we can easily validate a few common Office documents using just a few lines of code.