diff --git a/demo/demo39.ts b/demo/demo39.ts new file mode 100644 index 0000000000..d06aec50b2 --- /dev/null +++ b/demo/demo39.ts @@ -0,0 +1,33 @@ +// Scaling images +// Import from 'docx' rather than '../build' if you install from npm +import * as fs from "fs"; +import { Document, Packer, PageNumberFormat, TextRun } from "../build"; + +const doc = new Document( + {}, + { + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, + }, +); + +doc.Header.createParagraph("Foo Bar corp. ") + .addRun(new TextRun("Page Number ").pageNumber()) + .addRun(new TextRun(" to ").numberOfTotalPages()); + +doc.Footer.createParagraph("Foo Bar corp. ") + .center() + .addRun(new TextRun("Page Number: ").pageNumber()) + .addRun(new TextRun(" to ").numberOfTotalPages()); + +doc.createParagraph("Hello World 1").pageBreak(); +doc.createParagraph("Hello World 2").pageBreak(); +doc.createParagraph("Hello World 3").pageBreak(); +doc.createParagraph("Hello World 4").pageBreak(); +doc.createParagraph("Hello World 5").pageBreak(); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 4b9a626a03..a36b611623 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -17,6 +17,7 @@ * [Numbering](usage/numbering.md) * [Tab Stops](usage/tab-stops.md) * [Table of Contents](usage/table-of-contents.md) + * [Page Numbers](usage/page-numbers.md) * Styling * [Styling with JS](usage/styling-with-js.md) * [Styling with XML](usage/styling-with-xml.md) diff --git a/docs/usage/page-numbers.md b/docs/usage/page-numbers.md new file mode 100644 index 0000000000..f26a9a42b5 --- /dev/null +++ b/docs/usage/page-numbers.md @@ -0,0 +1,66 @@ +# Page Numbers + +> This feature allows you to set page numbers on each page + +?> **Note:** This feature only works on Headers and Footers + +```ts +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 + +```ts +pageNumber(); +``` + +For example: + +```ts +const currentPageRun = new TextRun("Current Page Number: ").pageNumber(); +paragraph.addRun(currentPageRun); +``` + +## Total number of pages + +```ts +numberOfTotalPages(); +``` + +For example: + +```ts +const lastPage = new TextRun("Total Page Number: ").numberOfTotalPages(); +paragraph.addRun(lastPage); +``` + + +## Both + +You can combine the two to get "Page 2 of 10" effect: + +```ts +const currentPageRun = new TextRun("Page ").pageNumber(); +const lastPage = new TextRun("of ").numberOfTotalPages(); + +paragraph.addRun(currentPageRun); +paragraph.addRun(lastPage); +``` + +Or: + +```ts +doc.Header.createParagraph().addRun(new TextRun("Page ").pageNumber()).addRun(new TextRun("of ").numberOfTotalPages()); +``` + +## Examples + +### Simple Example + +Adding page numbers to Header and Footer + +[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo39.ts ":include") + +_Source: https://github.com/dolanmiu/docx/blob/master/demo/demo39.ts_ diff --git a/package.json b/package.json index ba3b2cdc53..57ab21b8ff 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docx", - "version": "4.6.0", + "version": "4.7.0", "description": "Generate .docx documents with JavaScript (formerly Office-Clippy)", "main": "build/index.js", "scripts": {