mirror of
https://github.com/dolanmiu/docx.git
synced 2026-12-01 16:55:59 +01:00
* feat(layout): lay out footnotes across pages in kept paragraphs, table rows and columns, and page references in them, as Word does word-watertight-notes.docx and word-page-fields.docx, saved from Word, settle the footnote stops left on line 3 of the stops report: - Paragraphs kept with the next that start a footnote's part on a page break as without them, as the text's do at the top of a page (FN9, FN10, FN12). - A table row in a footnote breaks between its cells' lines, as a row of the text does, with the rest at the top of the next page's footnotes (FN11), and Word's empty paragraph after a table that ends a note takes a line below it (FN11, FN12, SP3c). - A footnote too long for the columns of a page, whose reference would be at the top of a later column, gets as much room below the columns as leaves its reference on the page, found by laying the page out again with the room halved (FN3). - A page reference with \p in a footnote or endnote writes "on page" and the page, wherever its bookmark is (PF8). Where the evidence leaves something open, the layout stops: a row in a footnote across more than two pages, a footnote that starts with a row none of which fits, a note ending with a table where the note text style isn't formatted as Normal, and a long footnote in columns that no room leaves with its reference. word-notes-across-pages.ts probes them. * fix(layout): stop where a footnote's reference moves on and back in columns, rather than lay the page out forever A page in columns whose footnote moves its reference on to the next page (FN1) is laid out again without the footnote's room. Where the reference's line then lands at the top of a later column, it stays there, as at the top of any column, its footnote asks for the room again, and the reference moves on again, round and round: paginate never returned. Of 600 random 2-column documents with footnotes, master hung on 100. The layout now stops where a reference moves on a second time, as whether Word moves the line on from the top of a column, or what is above it too, isn't known. Guessing, it moves on. It also no longer lists a moved footnote twice.
Welcome to docx
What is docx?
docx is a TypeScript/JavaScript library for generating Word documents (.docx files) programmatically. It provides a declarative API that works seamlessly in both Node.js and browser environments.
Why docx?
- Declarative API - Define what you want (objects/config), not how to build it step-by-step
- TypeScript First - Full type definitions with IntelliSense support
- Universal - Works in Node.js, browsers, and serverless environments
- No Dependencies - Zero external runtime dependencies
- Full Featured - Tables, images, shapes, charts, headers, footers, styles, and more
Quick Navigation
| I want to... | Go to... |
|---|---|
| Get started quickly | Quickstart Guide |
| Learn a specific feature | Usage Guides |
| See working examples | Demo Files |
| Browse the API | API Documentation |
| Modify existing documents | Patcher |
Installation
npm install --save docx
Then you can require or import as usual:
const docx = require("docx");
import * as docx from "docx";
// or
import { Document, Packer, Paragraph, TextRun } from "docx";
Shapes, watermarks and charts come with the package too, from docx/shapes, docx/watermarks and docx/charts:
import { ShapeRun } from "docx/shapes";
import { TextWatermark } from "docx/watermarks";
import { ChartRun } from "docx/charts";
Basic Usage
import * as fs from "fs";
import { Document, Packer, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new TextRun("Hello World"),
new TextRun({
text: " - Bold text",
bold: true,
}),
],
}),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("My Document.docx", buffer);
});
Made with 💖
