* feat(layout): write page numbers in every format, with chapter numbers, as Word does docx/layout wrote page numbers in decimal, roman numerals, letters and a few more, and stopped at the rest and at chapter numbers. Two probes saved from Word settle how Word writes each of ST_NumberFormat's formats in page numbers, page references and lists, and how it writes chapter numbers, and the layout now writes them so: the scripts of other languages, words, enclosed numbers and counting systems, up to the numbers Word writes, and the chapter number of the last heading its style numbers before each page reference. Lists a paragraph's style numbers it in are numbered too. pageNumbers takes chapterHeadingLevel, written as w:chapStyle, and PageNumberSeparator.EN_DASH is written as the schema's enDash. * fix(layout): take a style's list and level each from the nearest style that gives it A paragraph style based on another can give its level (w:ilvl) alone and take its list (w:numId) from the style it is based on, as Word's styles inherit each property on its own. The reader only kept a style's numbering when it gave the list, so such a style's paragraphs were numbered at the base style's level. * docs(layout): say page numbers are written in Word's formats but for those listed as left blank The line said every format was written, but the list of what is left blank names Thai and Hindi words, and page numbers Word writes as an error.
9.3 KiB
Page Numbers
This feature allows you to set page numbers on each page
?> Note: This feature only works on Headers and Footers
import { Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun({
children: ["Page #: ", PageNumber.CURRENT],
}),
],
}),
],
}),
},
children: [new Paragraph("Document content")],
},
],
});
Current page number
PageNumber.CURRENT;
For example:
import { Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun({
children: ["Page Number ", PageNumber.CURRENT],
}),
],
}),
],
}),
},
children: [new Paragraph("Document content")],
},
],
});
Total number of pages
PageNumber.TOTAL_PAGES;
For example:
import { Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun({
children: ["Total Pages Number: ", PageNumber.TOTAL_PAGES],
}),
],
}),
],
}),
},
children: [new Paragraph("Document content")],
},
],
});
Word and LibreOffice work out the number of pages when they lay the document out. It is written blank, for applications that show it as it is written, unless the document is given pageNumbers: estimatePageNumbers from docx/layout, which works it out when the document is written. See Layout.
Total number of pages in a section
PageNumber.TOTAL_PAGES_IN_SECTION;
It counts only the pages of the section it is in. To leave pages out of the count, such as a cover page and a table of contents, put them in a section of their own, and start the page numbers of the next section at 1:
import { AlignmentType, Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
import { estimatePageNumbers } from "docx/layout";
const doc = new Document({
// Works out the number of pages in each section, which LibreOffice leaves blank
pageNumbers: estimatePageNumbers,
sections: [
{
// The cover and contents have no footer, and aren't counted
children: [new Paragraph("Cover page"), new Paragraph({ text: "Contents", pageBreakBefore: true })],
},
{
properties: {
page: {
pageNumbers: {
start: 1,
},
},
},
footers: {
default: new Footer({
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
// "1 / 3" on the first page of this section
children: [PageNumber.CURRENT, " / ", PageNumber.TOTAL_PAGES_IN_SECTION],
}),
],
}),
],
}),
},
children: [
new Paragraph("Chapter 1"),
new Paragraph({ text: "Chapter 2", pageBreakBefore: true }),
new Paragraph({ text: "Chapter 3", pageBreakBefore: true }),
],
},
],
});
Word works out the number of pages in a section when it lays the document out, but LibreOffice doesn't. It shows the number as it is written, which is blank unless the document is given pageNumbers: estimatePageNumbers from docx/layout. See Layout.
Both
You can combine the two to get "Page 2 of 10" effect:
import { Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
footers: {
default: new Footer({
children: [
new Paragraph({
children: [
new TextRun("My awesome text here for my university dissertation. "),
new TextRun({
children: ["Page ", PageNumber.CURRENT, " of ", PageNumber.TOTAL_PAGES],
}),
],
}),
],
}),
},
children: [new Paragraph("Document content")],
},
],
});
Restart Page Numbering
When a document has multiple sections, you can restart page numbering for each section using pageNumbers.start in the section properties:
import { Document, Footer, PageNumber, Paragraph, TextRun } from "docx";
const footer = new Footer({
children: [new Paragraph({ children: [new TextRun({ children: ["Page ", PageNumber.CURRENT] })] })],
});
const doc = new Document({
sections: [
{
footers: { default: footer },
children: [new Paragraph("Section 1 content"), new Paragraph({ text: "More of section 1", pageBreakBefore: true })],
},
{
properties: {
page: {
pageNumbers: {
start: 1, // Restart from page 1
},
},
},
footers: { default: footer },
children: [new Paragraph("Section 2 — page numbering restarts here")],
},
],
});
Chapter Numbers
Page numbers can start with the number of the chapter they are in, such as 2-5 for page 5 of chapter 2. Give the level of the headings that start chapters as chapterHeadingLevel, from 1 for HeadingLevel.HEADING_1 to 9, and number those headings through their style, as Word does. The chapter number is the heading's number, from its first number to its last, so a heading numbered "Chapter 2" gives 2. Pages before the first numbered heading have no chapter number. Set what goes between the two numbers with separator:
import { AlignmentType, Document, Footer, HeadingLevel, LevelFormat, PageNumber, PageNumberSeparator, Paragraph, TextRun } from "docx";
const doc = new Document({
numbering: {
config: [
{
reference: "chapters",
levels: [{ level: 0, format: LevelFormat.DECIMAL, text: "Chapter %1", alignment: AlignmentType.LEFT }],
},
],
},
styles: {
paragraphStyles: [{ id: "Heading1", name: "Heading 1", paragraph: { numbering: { reference: "chapters", level: 0 } } }],
},
sections: [
{
properties: {
page: {
pageNumbers: {
start: 1,
chapterHeadingLevel: 1,
separator: PageNumberSeparator.EM_DASH, // "1—1"
},
},
},
footers: {
default: new Footer({
children: [new Paragraph({ children: [new TextRun({ children: ["Page ", PageNumber.CURRENT] })] })],
}),
},
children: [
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun("The coast")] }),
new Paragraph("Document content"),
],
},
],
});
Available separators: COLON, EM_DASH, EN_DASH, HYPHEN (the default), PERIOD.
Examples
Simple Example
Adding page numbers to Header and Footer
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-numbers/page-number-format.ts
Restart Page Numbers
Restarting page numbering in a new section
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-numbers/restart-page-numbers.ts
Total Pages in Each Section
Showing the number of pages in each section in its header
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-numbers/total-pages-in-section.ts