1.4 KiB
1.4 KiB
Page Numbers
This feature allows you to set page numbers on each page
?> Note: This feature only works on Headers and Footers
doc.Header.createParagraph().addRun(new TextRun("Page Number: ").pageNumber()).addRun(new TextRun("to ").numberOfTotalPages());
Current page number
To get the current page number, call the .pageNumber()
method on a TextRun
. Then add the newly created TextRun
into a paragraph
pageNumber();
For example:
const currentPageRun = new TextRun("Current Page Number: ").pageNumber();
paragraph.addRun(currentPageRun);
Total number of pages
numberOfTotalPages();
For example:
const lastPage = new TextRun("Total Page Number: ").numberOfTotalPages();
paragraph.addRun(lastPage);
Both
You can combine the two to get "Page 2 of 10" effect:
const currentPageRun = new TextRun("Page ").pageNumber();
const lastPage = new TextRun("of ").numberOfTotalPages();
paragraph.addRun(currentPageRun);
paragraph.addRun(lastPage);
Or:
doc.Header.createParagraph().addRun(new TextRun("Page ").pageNumber()).addRun(new TextRun("of ").numberOfTotalPages());
Examples
Simple Example
Adding page numbers to Header and Footer
Source: https://github.com/dolanmiu/docx/blob/master/demo/demo39.ts