Files
docx/docs
Dolan 06f22dd458 feat(layout): lay out documents in compatibility modes 14, 12 and 11, suppressTopSpacing and useFELayout, as Word does (#3679)
* feat(layout): lay out documents in compatibility modes 14, 12 and 11, suppressTopSpacing and useFELayout, as Word does

Word laid out the same 22 pages alike in compatibility modes 15, 14, 12 and 11
(word-stops-compat-15.docx to -11) but for justified lines, which Word 2010
and before don't squeeze, a table sized to its text, which they give its
cells' margins beside the room, tabs past the margin, a narrow gap beside a
frame, and in 12 and 11 East Asian text and VML text boxes. The layout now
lays out documents in those modes, and stops only at what they lay out
otherwise or haven't been seen with: those, a page break at a paragraph's end,
floating tables, tables sized to their text in cells, indented or as a share,
distributed lines, ligatures without enableOpenTypeFeatures, and the schema's
compatibility settings, seen leaving lines alone only in Word 2013's mode.

suppressTopSpacing leaves out the space above the text of the first line of a
page or column at exact and at-least spacing (word-stops-top-spacing.docx ST1
to ST5), and useFELayout leaves paragraphs without East Asian text as they are
(word-stops-fe-layout.docx FE1). Paragraphs kept together taller than some of 3
columns of different widths, and those kept with them, a gutter at the top
with mirrored margins or below a negative top margin, a table row in an HTML
division and pages printed two to a sheet no longer stop
(word-stops-pages.docx, word-stops-two-on-one.docx).

word-stops-compat2.ts writes the probes of what these left open, for the next
round of Word's PDFs.

* docs(layout): list the compatibility and settings probes in stops2/README.md, as #3678 lists the batch's

for the folder in scripts/layout-probes/README.md. The compatibility-mode, pages and
settings probes are listed there now, rather than in rows of their own.

* perf(layout): look for a paragraph's East Asian text only in documents that lay it out otherwise

unknownInOlderLayout scanned every character of every paragraph for East Asian text,
though only Word 2007's and 2003's modes and useFELayout lay it out otherwise.

* docs(layout): say FE1b turns off only autoSpaceDN, and probe both East Asian auto spacings off in FE2d

docx's autoSpaceEastAsianText writes only w:autoSpaceDN, so word-stops-fe-layout.docx
FE1b, which Word saved, has autoSpaceDE on, though its script's header said both were
off. The header now says what it is, and word-stops-compat2.ts's FE2d writes both off,
in the paragraph's properties in the schema's order, for the next batch.
2026-10-05 12:06:37 +01:00
..
2020-09-05 18:40:40 +01:00
2020-09-05 18:51:50 +01:00

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

clippy the assistant


Made with 💖