From a9fc40dad41293ebb1c7a691ffdeb61d1c09d0b6 Mon Sep 17 00:00:00 2001 From: Forman Date: Fri, 9 Aug 2019 11:56:22 +0300 Subject: [PATCH 1/3] Add total number of pages in a section --- demo/demo45.ts | 49 ++++++++++++++++++++++ src/file/paragraph/run/page-number.spec.ts | 11 ++++- src/file/paragraph/run/page-number.ts | 8 ++++ src/file/paragraph/run/run.spec.ts | 15 +++++++ src/file/paragraph/run/run.ts | 10 ++++- 5 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 demo/demo45.ts diff --git a/demo/demo45.ts b/demo/demo45.ts new file mode 100644 index 0000000000..7ccc44e22a --- /dev/null +++ b/demo/demo45.ts @@ -0,0 +1,49 @@ +// Multiple sections with total number of pages in each section +// 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(); + + +const header = doc.createHeader(); +header.createParagraph("Header on another page"); +const footer = doc.createFooter(); +footer.createParagraph("Foo Bar corp. ") + .center() + .addRun(new TextRun("Page Number: ").pageNumber()) + .addRun(new TextRun(" to ").numberOfTotalPagesSection()); + +doc.addSection({ + headers: { + default: header, + }, + footers: { + default: footer, + }, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, +}); + +doc.createParagraph("Section 1").pageBreak(); +doc.createParagraph("Section 1").pageBreak(); + +doc.addSection({ + headers: { + default: header, + }, + footers: { + default: footer, + }, + pageNumberStart: 1, + pageNumberFormatType: PageNumberFormat.DECIMAL, +}); + +doc.createParagraph("Section 2").pageBreak(); +doc.createParagraph("Section 2").pageBreak(); + +const packer = new Packer(); + +packer.toBuffer(doc).then((buffer) => { + fs.writeFileSync("My Document.docx", buffer); +}); diff --git a/src/file/paragraph/run/page-number.spec.ts b/src/file/paragraph/run/page-number.spec.ts index 4a9bc39a3e..aa01aa8d71 100644 --- a/src/file/paragraph/run/page-number.spec.ts +++ b/src/file/paragraph/run/page-number.spec.ts @@ -2,7 +2,7 @@ import { expect } from "chai"; import { Formatter } from "export/formatter"; -import { NumberOfPages, Page } from "./page-number"; +import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number"; describe("Page", () => { describe("#constructor()", () => { @@ -21,3 +21,12 @@ describe("NumberOfPages", () => { }); }); }); + +describe("NumberOfPagesSection", () => { + describe("#constructor()", () => { + it("uses the font name for both ascii and hAnsi", () => { + const tree = new Formatter().format(new NumberOfPagesSection()); + expect(tree).to.deep.equal({ "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "SECTIONPAGES"] }); + }); + }); +}); diff --git a/src/file/paragraph/run/page-number.ts b/src/file/paragraph/run/page-number.ts index 2fdc44e32b..ab5284f3c9 100644 --- a/src/file/paragraph/run/page-number.ts +++ b/src/file/paragraph/run/page-number.ts @@ -20,3 +20,11 @@ export class NumberOfPages extends XmlComponent { this.root.push("NUMPAGES"); } } + +export class NumberOfPagesSection extends XmlComponent { + constructor() { + super("w:instrText"); + this.root.push(new TextAttributes({ space: SpaceType.PRESERVE })); + this.root.push("SECTIONPAGES"); + } +} diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index ae6526a229..174ef86da0 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -204,6 +204,21 @@ describe("Run", () => { }); }); + describe("#numberOfTotalPagesSection", () => { + it("should set the run to the RTL mode", () => { + run.numberOfTotalPagesSection(); + const tree = new Formatter().format(run); + expect(tree).to.deep.equal({ + "w:r": [ + { "w:fldChar": { _attr: { "w:fldCharType": "begin" } } }, + { "w:instrText": [{ _attr: { "xml:space": "preserve" } }, "SECTIONPAGES"] }, + { "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(); diff --git a/src/file/paragraph/run/run.ts b/src/file/paragraph/run/run.ts index 32777cd31d..432a6c79e0 100644 --- a/src/file/paragraph/run/run.ts +++ b/src/file/paragraph/run/run.ts @@ -14,7 +14,7 @@ import { SizeComplexScript, Strike, } from "./formatting"; -import { NumberOfPages, Page } from "./page-number"; +import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number"; import { RunProperties } from "./properties"; import { RunFonts } from "./run-fonts"; import { SubScript, SuperScript } from "./script"; @@ -92,6 +92,14 @@ export class Run extends XmlComponent { return this; } + public numberOfTotalPagesSection(): Run { + this.root.push(new Begin()); + this.root.push(new NumberOfPagesSection()); + this.root.push(new Separate()); + this.root.push(new End()); + return this; + } + public smallCaps(): Run { this.properties.push(new SmallCaps()); return this; From 49e85275c3f2a8f453c3173fd95ae7fac8749bc8 Mon Sep 17 00:00:00 2001 From: Forman Date: Tue, 13 Aug 2019 10:53:29 +0300 Subject: [PATCH 2/3] Fix tests --- src/file/paragraph/run/run.spec.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/file/paragraph/run/run.spec.ts b/src/file/paragraph/run/run.spec.ts index 87a947932a..c16eb82b03 100644 --- a/src/file/paragraph/run/run.spec.ts +++ b/src/file/paragraph/run/run.spec.ts @@ -286,6 +286,7 @@ describe("Run", () => { describe("#numberOfTotalPagesSection", () => { it("should set the run to the RTL mode", () => { + const run = new Run({}); run.numberOfTotalPagesSection(); const tree = new Formatter().format(run); expect(tree).to.deep.equal({ From 2abff6991ff758dbc350f3cb5fc526bcaad1874f Mon Sep 17 00:00:00 2001 From: Forman Date: Wed, 14 Aug 2019 14:13:28 +0300 Subject: [PATCH 3/3] Rename demo --- demo/{demo45.ts => 47-number-of-total-pages-section.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename demo/{demo45.ts => 47-number-of-total-pages-section.ts} (100%) diff --git a/demo/demo45.ts b/demo/47-number-of-total-pages-section.ts similarity index 100% rename from demo/demo45.ts rename to demo/47-number-of-total-pages-section.ts