2019-08-09 11:56:22 +03:00
|
|
|
// Multiple sections with total number of pages in each section
|
|
|
|
// Import from 'docx' rather than '../build' if you install from npm
|
|
|
|
import * as fs from "fs";
|
2019-11-21 01:12:32 +00:00
|
|
|
import { AlignmentType, Document, Footer, Header, Packer, PageBreak, PageNumber, PageNumberFormat, Paragraph, TextRun } from "../build";
|
2019-08-09 11:56:22 +03:00
|
|
|
|
2019-10-10 01:08:01 +01:00
|
|
|
const header = new Header({
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [
|
|
|
|
new TextRun("Header on another page"),
|
2019-11-21 01:12:32 +00:00
|
|
|
new TextRun({
|
|
|
|
children: ["Page number: ", PageNumber.CURRENT],
|
|
|
|
}),
|
2019-11-23 02:26:59 +00:00
|
|
|
new TextRun({
|
|
|
|
children: [" to ", PageNumber.TOTAL_PAGES_IN_SECTION],
|
|
|
|
}),
|
2019-10-10 01:08:01 +01:00
|
|
|
],
|
|
|
|
alignment: AlignmentType.CENTER,
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
});
|
2019-08-09 11:56:22 +03:00
|
|
|
|
2019-10-10 01:08:01 +01:00
|
|
|
const footer = new Footer({
|
|
|
|
children: [new Paragraph("Foo Bar corp. ")],
|
|
|
|
});
|
2019-08-09 11:56:22 +03:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
page: {
|
|
|
|
pageNumbers: {
|
|
|
|
start: 1,
|
|
|
|
formatType: PageNumberFormat.DECIMAL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
default: header,
|
|
|
|
},
|
|
|
|
footers: {
|
|
|
|
default: footer,
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Section 1"), new PageBreak(), new TextRun("Section 1"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
page: {
|
|
|
|
pageNumbers: {
|
|
|
|
start: 1,
|
|
|
|
formatType: PageNumberFormat.DECIMAL,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
default: header,
|
|
|
|
},
|
|
|
|
footers: {
|
|
|
|
default: footer,
|
|
|
|
},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Section 2"), new PageBreak(), new TextRun("Section 2"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
},
|
2019-10-10 01:08:01 +01:00
|
|
|
],
|
2019-08-09 11:56:22 +03:00
|
|
|
});
|
|
|
|
|
2019-10-10 01:08:01 +01:00
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
2019-08-09 11:56:22 +03:00
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|