Add total number of pages in a section
This commit is contained in:
49
demo/demo45.ts
Normal file
49
demo/demo45.ts
Normal file
@ -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);
|
||||||
|
});
|
@ -2,7 +2,7 @@ import { expect } from "chai";
|
|||||||
|
|
||||||
import { Formatter } from "export/formatter";
|
import { Formatter } from "export/formatter";
|
||||||
|
|
||||||
import { NumberOfPages, Page } from "./page-number";
|
import { NumberOfPages, NumberOfPagesSection, Page } from "./page-number";
|
||||||
|
|
||||||
describe("Page", () => {
|
describe("Page", () => {
|
||||||
describe("#constructor()", () => {
|
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"] });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@ -20,3 +20,11 @@ export class NumberOfPages extends XmlComponent {
|
|||||||
this.root.push("NUMPAGES");
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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", () => {
|
describe("#pageNumber", () => {
|
||||||
it("should set the run to the RTL mode", () => {
|
it("should set the run to the RTL mode", () => {
|
||||||
run.pageNumber();
|
run.pageNumber();
|
||||||
|
@ -14,7 +14,7 @@ import {
|
|||||||
SizeComplexScript,
|
SizeComplexScript,
|
||||||
Strike,
|
Strike,
|
||||||
} from "./formatting";
|
} from "./formatting";
|
||||||
import { NumberOfPages, Page } from "./page-number";
|
import { NumberOfPages, NumberOfPagesSection, 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";
|
||||||
@ -92,6 +92,14 @@ export class Run extends XmlComponent {
|
|||||||
return this;
|
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 {
|
public smallCaps(): Run {
|
||||||
this.properties.push(new SmallCaps());
|
this.properties.push(new SmallCaps());
|
||||||
return this;
|
return this;
|
||||||
|
Reference in New Issue
Block a user