From 5552f9d834c9d4c15209e533c3fa920089440de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Fri, 9 Nov 2018 09:13:27 -0200 Subject: [PATCH 1/4] The page number type attribute of the sections was always been created, leading Word to always reset page numbers to zero in new sections. The page number type DECIMAL is already the default in Word, there is no need to force this to be the default option in the default section, like it was. --- src/file/document/body/body.spec.ts | 4 +--- .../section-properties.spec.ts | 21 ++++++++++++++++++- .../section-properties/section-properties.ts | 8 ++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/file/document/body/body.spec.ts b/src/file/document/body/body.spec.ts index 137ef298ba..b82944ff78 100644 --- a/src/file/document/body/body.spec.ts +++ b/src/file/document/body/body.spec.ts @@ -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" } }] }, ], }, ], diff --git a/src/file/document/body/section-properties/section-properties.spec.ts b/src/file/document/body/section-properties/section-properties.spec.ts index d767744e34..81135daba6 100644 --- a/src/file/document/body/section-properties/section-properties.spec.ts +++ b/src/file/document/body/section-properties/section-properties.spec.ts @@ -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); + }); }); }); diff --git a/src/file/document/body/section-properties/section-properties.ts b/src/file/document/body/section-properties/section-properties.ts index 4056570de2..9ae9e2bfb8 100644 --- a/src/file/document/body/section-properties/section-properties.ts +++ b/src/file/document/body/section-properties/section-properties.ts @@ -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( From 39cef5e61dad2d9c184c134e5bc4fd4e74290e4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Fri, 9 Nov 2018 09:14:38 -0200 Subject: [PATCH 2/4] Demo 16 was updated to show how page number types format and start work. --- demo/demo16.ts | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/demo/demo16.ts b/demo/demo16.ts index 6c75218e53..9113a368b8 100644 --- a/demo/demo16.ts +++ b/demo/demo16.ts @@ -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) => { From 8df78e45d9072a100a6c23c863abd0266f9c55dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Fri, 9 Nov 2018 09:26:27 -0200 Subject: [PATCH 3/4] Update in examples documentation. --- docs/examples.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/examples.md b/docs/examples.md index 05dbfb2de5..5964aecafe 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -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") From 8ad5485347d07ddc7b40960fc218ab299556ead4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mendon=C3=A7a?= Date: Fri, 9 Nov 2018 10:37:36 -0200 Subject: [PATCH 4/4] Improuvments in headers and footers docs --- docs/usage/headers-and-footers.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/docs/usage/headers-and-footers.md b/docs/usage/headers-and-footers.md index 1489576dda..f1d351dee3 100644 --- a/docs/usage/headers-and-footers.md +++ b/docs/usage/headers-and-footers.md @@ -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, });