How to Get Headers and Footers from a Word Document using Node.js

Cloudmersive
2 min readDec 20, 2023

--

With the right APIs, we can ask our applications to seamlessly transfer important content from one DOCX file to another.

The first part of that equation begins with retrieving the content we want. Using the below code, we can take advantage of a free API that will retrieve both header and footer content from our DOCX file. We’ll get our response structured like the below JSON example:

{
"Successful": true,
"Headers": [
{
"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"
}
],
"SectionsWithHeader": [
{
"StartingPageNumbers": [
0
],
"Path": "string"
}
]
}
],
"Footers": [
{
"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"
}
]
}
]
}

To structure our API call, we can start by installing the SDK. To do that, we’ll need to either run this command:

npm install cloudmersive-convert-api-client --save

Or add this snippet to our package.json:

  "dependencies": {
"cloudmersive-convert-api-client": "^2.6.3"
}

Before we call the function, let’s first format our request like the JSON example below:

{
"InputFileBytes": "string",
"InputFileUrl": "string"
}

Now we can copy the final code block into our file. To authorize our API calls, we’ll need a free-tier API key (these allow a limit of 800 API calls per month):

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.GetDocxHeadersAndFootersRequest(); // GetDocxHeadersAndFootersRequest | Document input request


var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editDocumentDocxGetHeadersAndFooters(reqConfig, callback);

Just like that, we can retrieve important content from our DOCX files with minimal code. Easy!

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

No responses yet