* feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded The docs' code examples were plain snippets, often fragments that couldn't run. A code block marked ```ts live is now an example readers can see and change: the page runs it with docx, draws the document it makes next to the code with docx-preview, and offers the .docx to download. Clicking the code opens an editor (Monaco) that knows docx's types, the pages are drawn again as the code changes, and View closes the editor. Split, Code and Preview show the code and the pages side by side, or one of them. The demos a page includes are live too. Every example in the docs that builds or patches a document is now live: 443 of them, each a whole file with its imports, so it runs as written. Code about other things, such as installing, bundlers or servers, stays plain. Running them showed docs that were wrong, fixed here: - Landscape: docx swaps a landscape page's width and height itself, so the docs' swapped sizes made a tall page marked as landscape - Comments: `comments.children` takes plain objects, not `new Comment(...)` - Custom properties are text only - APIs that no longer exist: MathText, Row, `.then` on Packer.toStream, patchDocument(buffer, ...), createParagraph().heading1(), a border's `value`, TextRun's `.break()` - The templates and patcher examples read files that aren't in the repository. They now patch the templates in demo/assets, or make their own `npm run build.docs` copies docx's browser builds into docs/live-examples/lib, so the docs use the docx they describe, and the Pages workflow runs it. The folder is gitignored, and cspell and ESLint skip it. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * docs: the bullet examples draw a bullet, and the patcher's usage writes the patched document "ὠ" is U+1F60, a Greek omega, since \u takes four hex digits, so the two live bullet examples on the numbering page drew it as each item's bullet. They use "•" now. The patcher's usage example now awaits patchDocument and writes the document it makes, as the example after it does. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
16 KiB
WPS Text Box
WPS (WordProcessing Shape) text boxes are the modern, DrawingML-based alternative to VML Text Boxes. They support background fills, outlines, rotation, and precise floating positioning.
!> WPS Text Boxes require an understanding of Paragraphs.
!> WpsShapeRun is deprecated. Use ShapeRun from docx/shapes, with type: "rectangle" and children, instead: it has simpler options for fills, lines, effects and text layout, and any shape can hold text.
When to Use WPS vs VML Text Boxes
| Feature | WPS Text Box (WpsShapeRun) |
VML Text Box (Textbox) |
|---|---|---|
| Based on | DrawingML (modern OOXML) | VML (legacy) |
| Background fill | Yes (RGB and scheme colors) | No |
| Border / outline | Yes (with full styling) | No |
| Rotation | Yes | No |
| Floating position | Yes | No |
| Vertical alignment | Yes (top, center, bottom) | No |
| Compatibility | Word 2010+ | All Word versions |
Use WpsShapeRun when you need fills, outlines, rotation, or precise positioning. Use Textbox for simple inline text boxes with maximum backward compatibility.
Basic Usage
A WpsShapeRun is added as a child of a Paragraph, similar to ImageRun:
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Hello from a WPS text box!")],
transformation: {
width: 200,
height: 100,
},
}),
],
}),
],
},
],
});
Options
| Property | Type | Notes | Description |
|---|---|---|---|
| type | "wps" |
Required | Identifies the run as a WPS shape |
| children | Paragraph[] |
Required | Content inside the text box |
| transformation | IMediaTransformation |
Required | Size, position, rotation, and flip |
| floating | IFloating |
Optional | Floating position (anchor mode) |
| solidFill | SolidFillOptions |
Optional | Background fill color |
| outline | OutlineOptions |
Optional | Border / outline styling |
| bodyProperties | IBodyPropertiesOptions |
Optional | Internal margins, vertical anchor, autofit |
| altText | DocPropertiesOptions |
Optional | Accessibility text (title, description) |
Transformation
Controls the size, offset, rotation, and flip of the text box. Dimensions are in pixels (converted to EMUs internally at 1px = 9525 EMUs).
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Rotated text box")],
transformation: {
width: 300,
height: 150,
rotation: 45, // degrees
},
}),
],
}),
],
},
],
});
| Property | Type | Notes | Description |
|---|---|---|---|
| width | number |
Required | Width in pixels |
| height | number |
Required | Height in pixels |
| offset | { top?: number, left?: number } |
Optional | Offset in pixels |
| rotation | number |
Optional | Rotation angle in degrees |
| flip | { vertical?: boolean, horizontal?: boolean } |
Optional | Flip transformations |
Floating Position
By default, a WpsShapeRun is rendered inline. Add the floating property to anchor it at a specific position on the page. Offset values are in EMUs (1 inch = 914400 EMUs).
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Floating text box")],
transformation: {
width: 200,
height: 100,
},
floating: {
zIndex: 10,
horizontalPosition: {
offset: 1014400,
},
verticalPosition: {
offset: 1014400,
},
},
}),
],
}),
],
},
],
});
The floating options are the same as for Images, including horizontalPosition, verticalPosition, wrap, margins, zIndex, behindDocument, lockAnchor, layoutInCell, and allowOverlap.
Background Fill
Set a background color using solidFill:
RGB Color
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Red background")],
transformation: { width: 200, height: 100 },
solidFill: {
type: "rgb",
value: "FF0000", // hex color
},
}),
],
}),
],
},
],
});
Scheme Color
Use theme colors instead of hardcoded values:
import { Document, Paragraph, SchemeColor, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Themed background")],
transformation: { width: 200, height: 100 },
solidFill: {
type: "scheme",
value: SchemeColor.ACCENT1,
},
}),
],
}),
],
},
],
});
Available scheme colors: BG1, BG2, TX1, TX2, ACCENT1-ACCENT6, HLINK, FOLHLINK, DK1, DK2, LT1, LT2, PHCLR.
Outline
Add a border/outline to the text box:
Solid Outline
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Bordered text box")],
transformation: { width: 200, height: 100 },
outline: {
type: "solidFill",
solidFillType: "rgb",
value: "0000FF",
width: 9525, // width in EMUs (9525 = 1px)
cap: "ROUND",
},
}),
],
}),
],
},
],
});
No Outline
import { Document, Paragraph, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("No outline")],
transformation: { width: 200, height: 100 },
solidFill: { type: "rgb", value: "DEEAF6" },
outline: {
type: "noFill",
},
}),
],
}),
],
},
],
});
Outline Options
| Property | Type | Notes | Description |
|---|---|---|---|
| type | "solidFill" | "noFill" |
Required | Fill type |
| solidFillType | "rgb" | "scheme" |
Required for solidFill | Color type |
| value | string | SchemeColor |
Required for solidFill | Color value |
| width | number |
Optional | Line width in EMUs |
| cap | "ROUND" | "SQUARE" | "FLAT" |
Optional | Line cap style |
| compoundLine | "SINGLE" | "DOUBLE" | "THICK_THIN" | "THIN_THICK" | "TRI" |
Optional | Line pattern |
Body Properties
Configure text layout inside the text box:
import { Document, Paragraph, VerticalAnchor, WpsShapeRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [new Paragraph("Centered text")],
transformation: { width: 200, height: 200 },
bodyProperties: {
verticalAnchor: VerticalAnchor.CENTER,
margins: {
top: 45720, // in EMUs
bottom: 45720,
left: 91440,
right: 91440,
},
noAutoFit: true,
},
}),
],
}),
],
},
],
});
| Property | Type | Notes | Description |
|---|---|---|---|
| verticalAnchor | VerticalAnchor |
Optional | Vertical text alignment |
| margins | { top?, bottom?, left?, right? } (numbers) |
Optional | Internal padding in EMUs |
| noAutoFit | boolean |
Optional | Disable auto-fit text to shape |
VerticalAnchor values: TOP, CENTER, BOTTOM.
Complete Example
A floating callout box with background fill, outline, and centered text:
import { AlignmentType, Document, Packer, Paragraph, TextRun, VerticalAnchor, WpsShapeRun } from "docx";
import * as fs from "fs";
const doc = new Document({
sections: [
{
children: [
new Paragraph("Main document content goes here..."),
new Paragraph({
children: [
new WpsShapeRun({
type: "wps",
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "Important Notice",
bold: true,
size: 28,
color: "FFFFFF",
}),
],
}),
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "This callout uses a WPS text box with fill and outline.",
color: "FFFFFF",
}),
],
}),
],
transformation: {
width: 300,
height: 150,
},
solidFill: {
type: "rgb",
value: "2F5496",
},
outline: {
type: "solidFill",
solidFillType: "rgb",
value: "1F3864",
width: 19050,
},
bodyProperties: {
verticalAnchor: VerticalAnchor.CENTER,
},
floating: {
zIndex: 10,
horizontalPosition: {
offset: 4572000,
},
verticalPosition: {
offset: 914400,
},
},
}),
],
}),
new Paragraph("More document content continues..."),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("document.docx", buffer);
});
WPS Text Box vs Text Frame vs VML Text Box
| Feature | WPS Text Box | Text Frame | VML Text Box |
|---|---|---|---|
| Based on | DrawingML shapes | Paragraph framing | VML shapes |
| Background fill | Yes | No | No |
| Border / outline | Yes | Yes (limited) | No |
| Rotation | Yes | No | No |
| Floating position | Yes | Limited | No |
| Auto-sizing | Yes (configurable) | No | Yes |
| Compatibility | Word 2010+ | All versions | Older versions |
For rich formatting and positioning, use WPS Text Box. For simple paragraph framing, use Text Frames. For basic legacy text boxes, use VML Text Box.