diff --git a/demo/fields/table-of-contents.ts b/demo/fields/table-of-contents.ts index 8158cd2c13..9cbefaf861 100644 --- a/demo/fields/table-of-contents.ts +++ b/demo/fields/table-of-contents.ts @@ -2,17 +2,20 @@ import * as fs from "fs"; import { File, HeadingLevel, Packer, Paragraph, StyleLevel, TableOfContents } from "docx"; +import { estimatePageNumbers } from "docx/layout"; // WordprocessingML docs for TableOfContents can be found here: // http://officeopenxml.com/WPtableOfContents.php // Let's define the properties for generate a TOC for heading 1-5 and MySpectacularStyle, -// making the entries be hyperlinks for the paragraph. The entries are written from the headings, -// and Word fills in their page numbers when it updates the fields +// making the entries be hyperlinks for the paragraph. docx/layout lays out the pages as Word would and +// writes the table of contents with its page numbers (see docs/usage/layout.md), and with updateFields +// off, Word shows it as it is written and doesn't ask to update the fields const doc = new File({ features: { - updateFields: true, + updateFields: false, }, + pageNumbers: estimatePageNumbers, styles: { paragraphStyles: [ { diff --git a/demo/layout/table-of-contents-page-numbers.ts b/demo/layout/table-of-contents-page-numbers.ts index ed3d89d936..1221f76c49 100644 --- a/demo/layout/table-of-contents-page-numbers.ts +++ b/demo/layout/table-of-contents-page-numbers.ts @@ -26,10 +26,10 @@ const chapter = (number: number): readonly Paragraph[] => [ ]; const doc = new Document({ - // Word updates the fields when it opens the document, which checks the page numbers - features: { updateFields: true }, // Lays out the pages as Word would, and writes the page numbers of the table of contents and the page references pageNumbers: estimatePageNumbers, + // With updateFields off, Word shows the page numbers as they are written, and doesn't ask to update the fields + features: { updateFields: false }, sections: [ { children: [ diff --git a/demo/layout/template-page-numbers.ts b/demo/layout/template-page-numbers.ts index 155a799deb..37dd545690 100644 --- a/demo/layout/template-page-numbers.ts +++ b/demo/layout/template-page-numbers.ts @@ -16,9 +16,10 @@ const text = (words: number, seed: number): string => const CHAPTERS = ["The coast", "The harbour wall", "The footings", "The costs", "What is left to do"]; // A report template, as it might be made in Word: a table of contents, and a placeholder for each chapter's text. Its -// table of contents has no page numbers yet +// table of contents has no page numbers yet. Its updateFields is off, which patchDocument keeps as it is, so Word +// shows the page numbers docx/layout writes, and doesn't ask to update the fields const template = new Document({ - features: { updateFields: true }, + features: { updateFields: false }, sections: [ { footers: { diff --git a/docs/usage/layout.md b/docs/usage/layout.md index 3a61df992d..084cbee7c6 100644 --- a/docs/usage/layout.md +++ b/docs/usage/layout.md @@ -109,7 +109,7 @@ const doc = await patchDocument({ }); ``` -The template is read as Word saved it, with its styles, theme, lists, settings, headers, footers and notes, and the numbers are written into its tables of contents, page references and numbers of pages, in its text, headers and footers. The numbers a template was saved with are those of the template before it was filled in, so one that `docx/layout` can't work out, after something it can't lay out, is left blank rather than kept. Its page references and tables of contents are written clean, as a document's are, so Word shows them as they are (see [Opening the document in Word](#opening-the-document-in-word)). What `estimatePageNumbersWith` returns works the same way. +The template is read as Word saved it, with its styles, theme, lists, settings, headers, footers and notes, and the numbers are written into its tables of contents, page references and numbers of pages, in its text, headers and footers. The numbers a template was saved with are those of the template before it was filled in, so one that `docx/layout` can't work out, after something it can't lay out, is left blank rather than kept. Its page references and tables of contents are written clean, as a document's are, so Word shows them as they are (see [Opening the document in Word](#opening-the-document-in-word)). The template's settings are kept as they are, `updateFields` (`w:updateFields`) among them, so a template that has it on still asks Word to update all of its fields when the filled-in document is opened. Leave it off in the template, and Word opens the filled-in document without asking, as it opens a document whose fields are written clean (see [Opening the document in Word](#opening-the-document-in-word) for what still makes it ask). What `estimatePageNumbersWith` returns works the same way. A template can import other documents (`w:altChunk`), which Word turns into paragraphs and tables of the template's own when it opens it. A `.docx` it imports is laid out as Word lays it out: where it is imported, in the text, a table cell, a header, a footnote or another imported document, in the template's styles of the same names, its defaults, its theme and its settings, with the styles only the imported document has as they look there, and paragraphs of no style in its default style, its lists numbered on their own and its footnotes numbered with the template's, and without its own page size, margins, headers and footers. A bookmark of a name the template has is the template's. What the layout doesn't follow stops it (see [What it leaves blank](#what-it-leaves-blank)).