Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
8dc590746b | |||
de5f5c9a77 | |||
ca5f6a56a5 | |||
8f6984580a |
33
demo/demo39.ts
Normal file
33
demo/demo39.ts
Normal file
@ -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);
|
||||||
|
});
|
@ -17,6 +17,7 @@
|
|||||||
* [Numbering](usage/numbering.md)
|
* [Numbering](usage/numbering.md)
|
||||||
* [Tab Stops](usage/tab-stops.md)
|
* [Tab Stops](usage/tab-stops.md)
|
||||||
* [Table of Contents](usage/table-of-contents.md)
|
* [Table of Contents](usage/table-of-contents.md)
|
||||||
|
* [Page Numbers](usage/page-numbers.md)
|
||||||
* Styling
|
* Styling
|
||||||
* [Styling with JS](usage/styling-with-js.md)
|
* [Styling with JS](usage/styling-with-js.md)
|
||||||
* [Styling with XML](usage/styling-with-xml.md)
|
* [Styling with XML](usage/styling-with-xml.md)
|
||||||
|
66
docs/usage/page-numbers.md
Normal file
66
docs/usage/page-numbers.md
Normal file
@ -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_
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "docx",
|
"name": "docx",
|
||||||
"version": "4.6.0",
|
"version": "4.7.0",
|
||||||
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
"description": "Generate .docx documents with JavaScript (formerly Office-Clippy)",
|
||||||
"main": "build/index.js",
|
"main": "build/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
23
src/file/paragraph/run/page-number.spec.ts
Normal file
23
src/file/paragraph/run/page-number.spec.ts
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
import { expect } from "chai";
|
||||||
|
|
||||||
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
|
import { NumberOfPages, Page } from "./page-number";
|
||||||
|
|
||||||
|
describe("Page", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("uses the font name for both ascii and hAnsi", () => {
|
||||||
|
const tree = new Formatter().format(new Page());
|
||||||
|
expect(tree).to.deep.equal({ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("NumberOfPages", () => {
|
||||||
|
describe("#constructor()", () => {
|
||||||
|
it("uses the font name for both ascii and hAnsi", () => {
|
||||||
|
const tree = new Formatter().format(new NumberOfPages());
|
||||||
|
expect(tree).to.deep.equal({ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -12,3 +12,11 @@ export class Page extends XmlComponent {
|
|||||||
this.root.push("PAGE");
|
this.root.push("PAGE");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class NumberOfPages extends XmlComponent {
|
||||||
|
constructor() {
|
||||||
|
super("w:instrText");
|
||||||
|
this.root.push(new TextAttributes({ space: SpaceType.PRESERVE }));
|
||||||
|
this.root.push("NUMPAGES");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -156,6 +156,38 @@ describe("Run", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("#numberOfTotalPages", () => {
|
||||||
|
it("should set the run to the RTL mode", () => {
|
||||||
|
run.numberOfTotalPages();
|
||||||
|
const tree = new Formatter().format(run);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:r": [
|
||||||
|
{ "w:rPr": [] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "begin" } }] },
|
||||||
|
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "NUMPAGES"] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "separate" } }] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "end" } }] },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("#pageNumber", () => {
|
||||||
|
it("should set the run to the RTL mode", () => {
|
||||||
|
run.pageNumber();
|
||||||
|
const tree = new Formatter().format(run);
|
||||||
|
expect(tree).to.deep.equal({
|
||||||
|
"w:r": [
|
||||||
|
{ "w:rPr": [] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "begin" } }] },
|
||||||
|
{ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "PAGE"] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "separate" } }] },
|
||||||
|
{ "w:fldChar": [{ _attr: { "w:fldCharType": "end" } }] },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("#style", () => {
|
describe("#style", () => {
|
||||||
it("should set the style to the given styleId", () => {
|
it("should set the style to the given styleId", () => {
|
||||||
run.style("myRunStyle");
|
run.style("myRunStyle");
|
||||||
|
@ -14,7 +14,7 @@ import {
|
|||||||
SizeComplexScript,
|
SizeComplexScript,
|
||||||
Strike,
|
Strike,
|
||||||
} from "./formatting";
|
} from "./formatting";
|
||||||
import { Page } from "./page-number";
|
import { NumberOfPages, Page } from "./page-number";
|
||||||
import { RunProperties } from "./properties";
|
import { RunProperties } from "./properties";
|
||||||
import { RunFonts } from "./run-fonts";
|
import { RunFonts } from "./run-fonts";
|
||||||
import { SubScript, SuperScript } from "./script";
|
import { SubScript, SuperScript } from "./script";
|
||||||
@ -84,6 +84,14 @@ export class Run extends XmlComponent {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public numberOfTotalPages(): Run {
|
||||||
|
this.root.push(new Begin());
|
||||||
|
this.root.push(new NumberOfPages());
|
||||||
|
this.root.push(new Separate());
|
||||||
|
this.root.push(new End());
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public smallCaps(): Run {
|
public smallCaps(): Run {
|
||||||
this.properties.push(new SmallCaps());
|
this.properties.push(new SmallCaps());
|
||||||
return this;
|
return this;
|
||||||
|
Reference in New Issue
Block a user