Merge pull request #376 from mforman1/Numbering

Add total number of pages in a section
This commit is contained in:
Dolan
2019-08-14 13:29:35 +01:00
committed by GitHub
5 changed files with 92 additions and 2 deletions

View 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);
});

View File

@ -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"] });
});
});
});

View File

@ -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");
}
}

View File

@ -284,6 +284,22 @@ 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({
"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", () => {
const run = new Run({});

View File

@ -21,7 +21,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";
@ -161,4 +161,12 @@ export class Run extends XmlComponent {
this.root.push(new End());
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;
}
}