Page Numbers
This feature allows you to set page numbers on each page
PageNumber values are explicit field objects. Plain strings are always rendered literally, so
text such as "CURRENT", "TOTAL_PAGES", or "SECTION" is safe to use as ordinary content.
Note: This feature only works on Headers and Footers
new Paragraph({
children: [
new TextRun({
children: ["Page #: ", PageNumber.CURRENT],
}),
],
});Current page number
PageNumber.CURRENT;For example:
new Paragraph({
children: [
new TextRun({
children: ["Page Number ", PageNumber.CURRENT],
}),
],
});Total number of pages
PageNumber.TOTAL_PAGES;For example:
new Paragraph({
children: [
new TextRun({
children: ["Total Pages Number: ", PageNumber.TOTAL_PAGES],
}),
],
});Total number of pages in the current section
PageNumber.TOTAL_PAGES_IN_SECTION;Like TOTAL_PAGES, but only counts the pages of the current Section. For example:
new Paragraph({
children: [
new TextRun({
children: ["Pages in this chapter: ", PageNumber.TOTAL_PAGES_IN_SECTION],
}),
],
});Current section number
PageNumber.CURRENT_SECTION;Inserts the number of the current Section. For example:
new Paragraph({
children: [
new TextRun({
children: ["Chapter ", PageNumber.CURRENT_SECTION],
}),
],
});Both
You can combine the two to get "Page 2 of 10" effect:
new Paragraph({
children: [
new TextRun("My awesome text here for my university dissertation. ")
new TextRun({
children: ["Page ", PageNumber.CURRENT, " of ", PageNumber.TOTAL_PAGES],
})
]
})Starting value and number format
The starting page number and the display format (decimal, roman numerals, etc.) are configured per section via the page.pageNumbers property — see Sections:
const doc = new Document({
sections: [
{
properties: {
page: {
pageNumbers: {
start: 1,
formatType: NumberFormat.LOWER_ROMAN,
},
},
},
children: [],
},
],
});Examples
Simple Example
Adding page numbers to Header and Footer
Source: https://github.com/dolanmiu/docx/blob/master/demo/39-page-numbers.ts