Merge pull request #197 from dolanmiu/bugfix/correct-section-page-num-type
Bugfix - Correct default section options
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
// Multiple sections and headers
|
||||
// Import from 'docx' rather than '../build' if you install from npm
|
||||
import * as fs from "fs";
|
||||
import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph } from "../build";
|
||||
import { Document, Packer, PageNumberFormat, PageOrientation, Paragraph, TextRun } from "../build";
|
||||
|
||||
const doc = new Document();
|
||||
|
||||
@ -41,6 +41,40 @@ doc.addSection({
|
||||
|
||||
doc.createParagraph("hello in landscape");
|
||||
|
||||
const header2 = doc.createHeader();
|
||||
const pageNumber = new TextRun("Page number: ").pageNumber();
|
||||
header2.createParagraph().addRun(pageNumber);
|
||||
|
||||
doc.addSection({
|
||||
headers: {
|
||||
default: header2,
|
||||
},
|
||||
orientation: PageOrientation.PORTRAIT,
|
||||
});
|
||||
|
||||
doc.createParagraph("Page number in the header must be 2, because it continues from the previous section.");
|
||||
|
||||
doc.addSection({
|
||||
headers: {
|
||||
default: header2,
|
||||
},
|
||||
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
|
||||
orientation: PageOrientation.PORTRAIT,
|
||||
});
|
||||
|
||||
doc.createParagraph("Page number in the header must be III, because it continues from the previous section, but is defined as upper roman.");
|
||||
|
||||
doc.addSection({
|
||||
headers: {
|
||||
default: header2,
|
||||
},
|
||||
pageNumberFormatType: PageNumberFormat.DECIMAL,
|
||||
pageNumberStart: 25,
|
||||
orientation: PageOrientation.PORTRAIT,
|
||||
});
|
||||
|
||||
doc.createParagraph("Page number in the header must be 25, because it is defined to start at 25 and to be decimal in this section.");
|
||||
|
||||
const packer = new Packer();
|
||||
|
||||
packer.toBuffer(doc).then((buffer) => {
|
||||
|
@ -158,7 +158,8 @@ _Source: https://github.com/dolanmiu/docx/blob/master/demo/demo15.ts_
|
||||
|
||||
## Sections
|
||||
|
||||
Example of how sections work. Sections allow multiple headers and footers, and `landscape`/`portrait` inside the same document
|
||||
Example of how sections work. Sections allow multiple headers and footers, and `landscape`/`portrait` inside the same document.
|
||||
Also you can have different page number formats and starts for different sections.
|
||||
|
||||
[Example](https://raw.githubusercontent.com/dolanmiu/docx/master/demo/demo16.ts ":include")
|
||||
|
||||
|
@ -37,8 +37,12 @@ Also all the supported section properties are implemented according to: http://o
|
||||
|
||||
// Add new section with another header and footer
|
||||
doc.addSection({
|
||||
headerId: header.Header.ReferenceId,
|
||||
footerId: footer.Footer.ReferenceId,
|
||||
headers: {
|
||||
default: header
|
||||
},
|
||||
footers: {
|
||||
default: footer
|
||||
},
|
||||
pageNumberStart: 1,
|
||||
pageNumberFormatType: docx.PageNumberFormat.DECIMAL,
|
||||
});
|
||||
|
@ -17,7 +17,7 @@ describe("Body", () => {
|
||||
expect(formatted)
|
||||
.to.have.property("w:sectPr")
|
||||
.and.to.be.an.instanceof(Array);
|
||||
expect(formatted["w:sectPr"]).to.have.length(5);
|
||||
expect(formatted["w:sectPr"]).to.have.length(4);
|
||||
});
|
||||
});
|
||||
|
||||
@ -76,7 +76,6 @@ describe("Body", () => {
|
||||
},
|
||||
{ "w:cols": [{ _attr: { "w:space": 708 } }] },
|
||||
{ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] },
|
||||
{ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
@ -104,7 +103,6 @@ describe("Body", () => {
|
||||
},
|
||||
{ "w:cols": [{ _attr: { "w:space": 708 } }] },
|
||||
{ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] },
|
||||
{ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] },
|
||||
],
|
||||
},
|
||||
],
|
||||
|
@ -88,7 +88,6 @@ describe("SectionProperties", () => {
|
||||
});
|
||||
expect(tree["w:sectPr"][2]).to.deep.equal({ "w:cols": [{ _attr: { "w:space": 708 } }] });
|
||||
expect(tree["w:sectPr"][3]).to.deep.equal({ "w:docGrid": [{ _attr: { "w:linePitch": 360 } }] });
|
||||
expect(tree["w:sectPr"][4]).to.deep.equal({ "w:pgNumType": [{ _attr: { "w:fmt": "decimal" } }] });
|
||||
});
|
||||
|
||||
it("should create section properties with changed options", () => {
|
||||
@ -183,5 +182,25 @@ describe("SectionProperties", () => {
|
||||
"w:pgBorders": [{ _attr: { "w:offsetFrom": "page" } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should create section properties with page number type, but without start attribute", () => {
|
||||
const properties = new SectionProperties({
|
||||
pageNumberFormatType: PageNumberFormat.UPPER_ROMAN,
|
||||
});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
||||
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
|
||||
expect(pgNumType).to.deep.equal({
|
||||
"w:pgNumType": [{ _attr: { "w:fmt": "upperRoman" } }],
|
||||
});
|
||||
});
|
||||
|
||||
it("should create section properties without page number type", () => {
|
||||
const properties = new SectionProperties({});
|
||||
const tree = new Formatter().format(properties);
|
||||
expect(Object.keys(tree)).to.deep.equal(["w:sectPr"]);
|
||||
const pgNumType = tree["w:sectPr"].find((item) => item["w:pgNumType"] !== undefined);
|
||||
expect(pgNumType).to.equal(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -14,7 +14,7 @@ import { HeaderReference } from "./header-reference/header-reference";
|
||||
import { IPageBordersOptions, PageBorders } from "./page-border";
|
||||
import { PageMargin } from "./page-margin/page-margin";
|
||||
import { IPageMarginAttributes } from "./page-margin/page-margin-attributes";
|
||||
import { IPageNumberTypeAttributes, PageNumberFormat, PageNumberType } from "./page-number";
|
||||
import { IPageNumberTypeAttributes, PageNumberType } from "./page-number";
|
||||
import { PageSize } from "./page-size/page-size";
|
||||
import { IPageSizeAttributes, PageOrientation } from "./page-size/page-size-attributes";
|
||||
import { TitlePage } from "./title-page/title-page";
|
||||
@ -69,7 +69,7 @@ export class SectionProperties extends XmlComponent {
|
||||
orientation = PageOrientation.PORTRAIT,
|
||||
headers,
|
||||
footers,
|
||||
pageNumberFormatType = PageNumberFormat.DECIMAL,
|
||||
pageNumberFormatType,
|
||||
pageNumberStart,
|
||||
pageBorders,
|
||||
pageBorderTop,
|
||||
@ -88,7 +88,9 @@ export class SectionProperties extends XmlComponent {
|
||||
this.addHeaders(headers);
|
||||
this.addFooters(footers);
|
||||
|
||||
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
|
||||
if (pageNumberStart || pageNumberFormatType) {
|
||||
this.root.push(new PageNumberType(pageNumberStart, pageNumberFormatType));
|
||||
}
|
||||
|
||||
if (pageBorders || pageBorderTop || pageBorderRight || pageBorderBottom || pageBorderLeft) {
|
||||
this.root.push(
|
||||
|
Reference in New Issue
Block a user