How do I read the header or footer of a Word document with docx4js?

1.4k views Asked by At

I can read the contents of the file with

docx4js.load(fileName)).then(docx => {
    console.log("Content of the body is: " + docx.officeDocument.content.text());
});

But how do I print out the text in the header?

2

There are 2 answers

0
Ahmed Fathy On BEST ANSWER

In docx, there is only 1 header and 1 footer. You cant have a footer in 1st page that is unlike footer in 2nd page and same goes with header. Once you change the header in any of the pages, all headers on other pages change. So getting the 1st header or footer is like getting them for all pages.

To get header you do it like

docx.getObjectPart("word/header1.xml").text();

And you can do the same thing for the footer

docx.getObjectPart("word/footer1.xml").text();

you can get the content/body as well doing like

docx.getObjectPart("word/document.xml").text();

0
Ryan Shillington On

This seems to get me the header text from the first page, at least:

docx.getObjectPart("word/header1.xml").text()