Search code examples
javascriptdocx4js

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


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?


Solution

  • 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();