2020-02-28 18:44:41 +00:00
|
|
|
// Page numbers - Start from 0 on a new section
|
2023-06-05 00:33:43 +01:00
|
|
|
|
2020-02-28 18:44:41 +00:00
|
|
|
import * as fs from "fs";
|
2023-06-05 00:33:43 +01:00
|
|
|
import { AlignmentType, Document, Header, Packer, PageBreak, PageNumber, PageNumberSeparator, Paragraph, TextRun } from "docx";
|
2020-02-28 18:44:41 +00:00
|
|
|
|
2021-03-19 20:53:56 +00:00
|
|
|
const doc = new Document({
|
|
|
|
sections: [
|
|
|
|
{
|
|
|
|
headers: {
|
|
|
|
default: new Header({
|
2020-02-28 18:44:41 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
alignment: AlignmentType.RIGHT,
|
|
|
|
children: [
|
|
|
|
new TextRun("My Title "),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
],
|
2020-02-28 18:44:41 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
first: new Header({
|
2020-02-28 18:44:41 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
alignment: AlignmentType.RIGHT,
|
|
|
|
children: [
|
|
|
|
new TextRun("First Page Header "),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
],
|
2020-02-28 18:44:41 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
2020-02-28 18:44:41 +00:00
|
|
|
children: [
|
|
|
|
new Paragraph({
|
2021-03-19 20:53:56 +00:00
|
|
|
children: [new TextRun("First Page"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph("Second Page"),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
|
|
|
page: {
|
|
|
|
pageNumbers: {
|
|
|
|
start: 1,
|
|
|
|
separator: PageNumberSeparator.EM_DASH,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
default: new Header({
|
2020-02-28 18:44:41 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
alignment: AlignmentType.RIGHT,
|
|
|
|
children: [
|
|
|
|
new TextRun("My Title "),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
],
|
2020-02-28 18:44:41 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
first: new Header({
|
2020-02-28 18:44:41 +00:00
|
|
|
children: [
|
2021-03-19 20:53:56 +00:00
|
|
|
new Paragraph({
|
|
|
|
alignment: AlignmentType.RIGHT,
|
|
|
|
children: [
|
|
|
|
new TextRun("First Page Header of Second section"),
|
|
|
|
new TextRun({
|
|
|
|
children: ["Page ", PageNumber.CURRENT],
|
|
|
|
}),
|
|
|
|
],
|
2020-02-28 18:44:41 +00:00
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
|
|
|
children: [
|
|
|
|
new Paragraph({
|
|
|
|
children: [new TextRun("Third Page"), new PageBreak()],
|
|
|
|
}),
|
|
|
|
new Paragraph("Fourth Page"),
|
2020-02-28 18:44:41 +00:00
|
|
|
],
|
2021-03-19 20:53:56 +00:00
|
|
|
},
|
2020-02-28 18:44:41 +00:00
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
Packer.toBuffer(doc).then((buffer) => {
|
|
|
|
fs.writeFileSync("My Document.docx", buffer);
|
|
|
|
});
|