// add header contents in a specific section

import { CreateDocx, OptionsHeader, WordFragment } from "../../../build";
import * as fs from "fs";

const docx = new CreateDocx();

// create sections
docx.addText("First section content.");
docx.addSection();
docx.addText("Second section content.");
docx.addSection();
docx.addText("Third section content.");

// add header contents in the second section
const headersA: OptionsHeader[] = [];

const headerFragmentDefaultA = new WordFragment();
headerFragmentDefaultA.addText("Text header second section.");
const headerContentDefaultA: OptionsHeader = {
    text: headerFragmentDefaultA,
    section: 2,
};

headersA.push(headerContentDefaultA);

docx.addHeader(headersA);

// add empty header contents in the third section. By default, MS Word displays headers/footers from the previous section if no header/footer exist of the same type
const headersB: OptionsHeader[] = [];

const headerFragmentEmpty = new WordFragment();
headerFragmentEmpty.addText("");
const headerContentDefaultB: OptionsHeader = {
    text: headerFragmentEmpty,
    section: 3,
};

headersB.push(headerContentDefaultB);

docx.addHeader(headersB);

docx.save().then(contents => {
    fs.writeFileSync("addHeader_3.docx", Buffer.from(contents));
});
