How to Set a Footer in a Word Document using Node.js
If we’re applying standard document footers to a wide range DOCX files, we might benefit from using an API to streamline that process in our Node.js application.
Conveniently, using the code provided further down the page, we can take advantage of a useful DOCX document conversion API that’s both free and extremely easy to use. This will allow us to set detailed document footers in our DOCX files, specifying everything from paragraph indices to font families in one go.
We’ll receive a temporary document editing URL in our API response, and we can convert this URL into our updated DOCX file encoding through a second API call. Both of our API requests can be authorized with a single free-tier API key, which will allow us to make 800 API calls per month with no additional commitment.
Let’s start off by installing the SDK. We can either run this command:
npm install cloudmersive-convert-api-client --save
Or we can add the following snippet to our package.json:
"dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}
Next, we can structure our input request like the following JSON example:
{
"InputFileBytes": "string",
"InputFileUrl": "string",
"FooterToApply": {
"Path": "string",
"Paragraphs": [
{
"ParagraphIndex": 0,
"Path": "string",
"ContentRuns": [
{
"RunIndex": 0,
"Path": "string",
"TextItems": [
{
"TextIndex": 0,
"Path": "string",
"TextContent": "string"
}
],
"Bold": true,
"Italic": true,
"Underline": "string",
"FontFamily": "string",
"FontSize": "string"
}
],
"StyleID": "string"
}
],
"SectionsWithFooter": [
{
"StartingPageNumbers": [
0
],
"Path": "string"
}
]
}
}
And can call the API using the following 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.EditDocumentApi();
var reqConfig = new CloudmersiveConvertApiClient.DocxSetFooterRequest(); // DocxSetFooterRequest | Document input request
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editDocumentDocxSetFooter(reqConfig, callback);
After this request returns our temporary editing URL, we can use the below code to download the result of our document editing process from that URL:
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.EditDocumentApi();
var reqConfig = new CloudmersiveConvertApiClient.FinishEditingRequest(); // FinishEditingRequest | Cloudmersive Document URL to complete editing on
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editDocumentFinishEditing(reqConfig, callback);
Now we can easily make complex changes to our DOCX footers with fast, in-memory API reqeusts.