Files
docx/docs/usage/shapes.md
T
DolanandClaude Opus 5.5 c169dd1a83 feat: live examples in the docs, which run, draw the document next to the code, and can be edited and downloaded (#3556)
* 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>
2026-09-28 13:00:29 +01:00

102 KiB

Shapes

!> Shapes require an understanding of Paragraphs.

Shapes are drawings made from lines and fills rather than pictures: rectangles, ellipses, lines, arrows, stars, callouts, flowchart symbols and more. Use a ShapeRun for one shape, a ShapeGroupRun for several shapes that belong together, and a ShapeCanvasRun for diagrams whose shapes are joined by connectors.

Like an image, a shape is a run inside a Paragraph. It sits in the line of text unless you make it floating.

Common Use Cases

I want to... Use Example
Draw a bar or blank line in a sentence rectangle with a fill and line: "none" Blanks on a printed form
Draw a line to write or sign on line with height: 0 Signature line
Point at something line with an arrowhead Arrow in a diagram
Put text in a coloured box, circle or callout Any shape with text or children Flowchart step, speech bubble
Make a shape as big as its text width: "fitText" or height: "fitText" Buttons, labels, tags
Place a shape anywhere on the page floating Stamp in a corner
Keep several shapes together as one drawing ShapeGroupRun Badge, simple diagram
Join shapes with lines that follow them ShapeCanvasRun with connectors Flowchart, org chart
Lay out a diagram without working out offsets layout on a canvas or group Flowchart, org chart, grid
Crop a photo to a circle ellipse with a picture fill Profile picture
Add a shadow, glow or reflection effects Card with a drop shadow
Draw a shape that isn't a preset type: "custom" with an SVG path Logo, badge, freeform outline
Draw an icon with shading, holes or details type: "custom" with several paths Cube, cylinder, page, ring

Importing

Shapes come with the docx package. Import them from docx/shapes, and everything else, such as the document and its paragraphs, from docx:

import { Document, Packer, Paragraph } from "docx";
import { ShapeCanvasRun, ShapeGroupRun, ShapeRun } from "docx/shapes";

The examples on this page use ShapeRun, ShapeGroupRun and ShapeCanvasRun from docx/shapes. Types such as IShapeGroupChildOptions come from there too.

In a page without a bundler, load the shapes after docx: dist/shapes.umd.cjs after dist/index.umd.cjs, or dist/shapes.iife.js after dist/index.iife.js. They add a docxShapes global next to the docx one:

<script src="node_modules/docx/dist/index.umd.cjs"></script>
<script src="node_modules/docx/dist/shapes.umd.cjs"></script>
<script>
    const shape = new docxShapes.ShapeRun({ type: "ellipse", transformation: { width: 100, height: 60 } });
</script>

Basic Usage

import { Document, Paragraph, TextRun } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new TextRun("Name: "),
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 200, height: 4 },
                            fill: "000000",
                            line: "none",
                        }),
                    ],
                }),
            ],
        },
    ],
});

This draws a black bar 200 pixels wide and 4 pixels tall after the text "Name: ".

With no fill or line, a shape has no fill and a black line 1pt wide, so it is always visible:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [new ShapeRun({ type: "ellipse", transformation: { width: 100, height: 60 } })],
                }),
            ],
        },
    ],
});

Shape Types

type is the name of one of the 187 preset shapes in Office Open XML. The names say what each shape is, such as roundedRectangle or ribbonUp. Some common ones are:

Category Shapes
Basic shapes rectangle, roundedRectangle, ellipse, triangle, rightTriangle, diamond, parallelogram, trapezoid, hexagon
Lines line, straightConnector, elbowConnector, curvedConnector
Block arrows rightArrow, leftArrow, upArrow, downArrow, leftRightArrow, bentArrow, uTurnArrow, circularArrow
Stars and banners star4, star5, star6, star8, star12, explosion12, ribbonUp, ribbonDown, wave, horizontalScroll
Callouts rectangularCallout, roundedRectangularCallout, ellipticalCallout, cloudCallout, lineCallout
Flowchart flowChartProcess, flowChartDecision, flowChartTerminator, flowChartDocument, flowChartMagneticDisk
Symbols heart, lightningBolt, sun, moon, smileyFace, cloud, noSymbol, cross, gear6, gear9

The PresetShapeType type lists every shape, so your editor can suggest them as you type. Shape Types lists all of them, with the name each one has in the OOXML specification.

Size and Rotation

transformation works the same way as for images:

Property Type Notes Description
width number | "fitText" Required Width in pixels, or "fitText". See Sizing a shape to its text
height number | "fitText" Required Height in pixels, or "fitText"
rotation number Optional Clockwise rotation in degrees
flip { horizontal?: boolean; vertical?: boolean } Optional Mirrors the shape
offset { left?: number; top?: number } Optional Position in pixels. Only used for shapes in a group or canvas

Lines

A line runs from the top-left corner of its box to the bottom-right corner:

  • height: 0 draws a horizontal line, and width: 0 a vertical one.
  • Any other size draws a diagonal line. Add flip: { vertical: true } to run it from the bottom-left to the top-right instead.
  • startArrow goes where the line starts (the top-left, or the bottom-left when flipped vertically) and endArrow where it ends.

To join two shapes, use a connector instead. It is drawn from one shape to the other for you.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // Horizontal
                        new ShapeRun({ type: "line", transformation: { width: 300, height: 0 } }),

                        // Up and to the right, with an arrowhead at the top
                        new ShapeRun({
                            type: "line",
                            transformation: { width: 120, height: 60, flip: { vertical: true } },
                            line: { endArrow: "triangle" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

Fill

fill can be a colour, "none", a solid fill, a gradient, a pattern or a picture. Colours are 6-digit hex values, with or without a #, or colours of the document's theme.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // A colour
                        new ShapeRun({ type: "rectangle", transformation: { width: 100, height: 50 }, fill: "4472C4" }),

                        // A colour with transparency, from 0 (opaque) to 100 (invisible)
                        new ShapeRun({
                            type: "ellipse",
                            transformation: { width: 90, height: 90 },
                            fill: { color: "FF0000", transparency: 50 },
                        }),

                        // No fill (the default)
                        new ShapeRun({ type: "rectangle", transformation: { width: 100, height: 50 }, fill: "none" }),
                    ],
                }),
            ],
        },
    ],
});

Theme colours

Anywhere a shape takes a colour, it can take one of the colours of the document's theme instead: { theme: "accent1" }. The shape changes colour when the theme's colours change, such as when a different theme is chosen on Word's Design tab.

lighter and darker make the colour lighter or darker, from 0 (unchanged) to 100 (white or black), as Word's colour menus do: "Blue, Accent 1, Lighter 80%" is { theme: "accent1", lighter: 80 }. Text, borders and shading take the same colours: see Themes.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // Filled in a light version of the theme's first accent colour, with a darker outline
                        new ShapeRun({
                            type: "roundedRectangle",
                            transformation: { width: 120, height: 60 },
                            fill: { theme: "accent1", lighter: 80 },
                            line: { theme: "accent1", darker: 25 },
                        }),

                        // Theme colours with transparency, and in gradients, patterns, lines and effects
                        new ShapeRun({
                            type: "ellipse",
                            transformation: { width: 90, height: 90 },
                            fill: { color: { theme: "accent2" }, transparency: 40 },
                            line: { color: { theme: "dark2" }, width: 2 },
                            effects: { glow: { color: { theme: "accent4" }, size: 6 } },
                        }),
                    ],
                }),
            ],
        },
    ],
});
theme OOXML name Office's colour
dark1 dk1 Black
light1 lt1 White
dark2 dk2 44546A
light2 lt2 E7E6E6
accent1 accent1 4472C4
accent2 accent2 ED7D31
accent3 accent3 A5A5A5
accent4 accent4 FFC000
accent5 accent5 5B9BD5
accent6 accent6 70AD47
hyperlink hlink 0563C1
followedHyperlink folHlink 954F72

They are written as a:schemeClr, and lighter and darker as Word writes them: lighter scales the colour's luminance (a:lumMod) and adds to it (a:lumOff), and darker scales it.

Gradients

A gradient needs at least two stops. Each stop has a position from 0 to 100 along the gradient, a color and an optional transparency.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // Linear, from top to bottom
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 160, height: 80 },
                            fill: {
                                type: "gradient",
                                angle: 90,
                                stops: [
                                    { position: 0, color: "9DC3E6" },
                                    { position: 100, color: "1F4E79" },
                                ],
                            },
                        }),

                        // Radial, from the centre outwards
                        new ShapeRun({
                            type: "ellipse",
                            transformation: { width: 80, height: 80 },
                            fill: {
                                type: "gradient",
                                path: "circle",
                                stops: [
                                    { position: 0, color: "FFFFFF" },
                                    { position: 100, color: "ED7D31" },
                                ],
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Property Type Notes Description
type "gradient" Required
stops GradientStop[] Required At least two colour stops
angle number Optional Direction of a linear gradient in degrees, clockwise. 0 (the default) runs left to right and 90 runs top to bottom
path "circle" | "rectangle" | "shape" Optional Makes the gradient radiate from the centre in a circle, a rectangle or following the shape's outline, instead of being linear

Pattern fills

A pattern fill repeats lines or dots in one colour over another. Shape Patterns and Text Warps lists all 54 patterns.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 80, height: 60 },
                            fill: { type: "pattern", pattern: "wideUpwardDiagonal", color: "2F5597", backgroundColor: "DEEBF7" },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Property Type Notes Description
type "pattern" Required
pattern ShapePattern Required Such as "percent20", "horizontal", "smallCheckerBoard"
color ShapeColor Optional Colour of the lines and dots. Default is 000000
backgroundColor ShapeColor Optional Colour of the space behind them. Default is FFFFFF

Picture fills

A picture fill puts a picture inside the shape, and the shape's outline crops it. image takes the same type and data as an ImageRun, including SVG with a fallback. The picture is stretched to fill the shape unless you tile it.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // A photo cropped to a circle
                        new ShapeRun({
                            type: "ellipse",
                            transformation: { width: 80, height: 80 },
                            fill: { type: "picture", image: { type: "jpg", data: fs.readFileSync("./demo/assets/images/cat.jpg") } },
                            line: { color: "FFFFFF", width: 3 },
                        }),

                        // A picture repeated across a heart, at a fifth of its size, with every other tile mirrored
                        new ShapeRun({
                            type: "heart",
                            transformation: { width: 80, height: 80 },
                            fill: {
                                type: "picture",
                                image: { type: "png", data: fs.readFileSync("./demo/assets/images/dog.png") },
                                tile: { scale: 20, mirror: "both" },
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Property Type Notes Description
type "picture" Required
image ImageSource Required { type, data }, as for an ImageRun
tile PictureTile Optional Repeats the picture at its own size instead of stretching it. See below
crop ICropOptions Optional Crops a percentage, from 0 to 100, off each side before the picture is stretched
transparency number Optional From 0 (opaque) to 100 (invisible)

tile takes a scale (percent of the picture's own size, default 100), an alignment for the first tile ("topLeft", the default, "top", "topRight", "left", "center", "right", "bottomLeft", "bottom" or "bottomRight") and mirror ("none", the default, "horizontal", "vertical" or "both"), which mirrors every other tile.

Line

line is the shape's outline, or the line itself for line and connector shapes. It can be a colour, a theme colour, "none" or an object:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "line",
                            transformation: { width: 300, height: 0 },
                            line: {
                                color: "C00000",
                                width: 2,
                                dash: "dash",
                                startArrow: "oval",
                                endArrow: { type: "triangle", width: "large", length: "large" },
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Property Type Notes Description
color ShapeColor Optional Hex or theme colour. Default is 000000 (black)
width number Optional Width in points, from 0 to 1584. Default is 1
transparency number Optional From 0 (opaque) to 100 (invisible)
dash LineDash | CustomLineDash Optional A dash pattern, or dashes of your own. Default is a solid line
cap "flat" | "round" | "square" Optional The shape of the line's ends and of each dash's ends
join "miter" | "round" | "bevel" Optional The shape of the corners where the line turns: sharp, rounded or cut off
compound "single" | "double" | "thickThin" | "thinThick" | "triple" Optional Draws the line as two or three parallel lines
gradient { stops, angle?, path? } Optional Colours the line with a gradient instead of color
startArrow Arrowhead Optional Arrowhead at the start of the line
endArrow Arrowhead Optional Arrowhead at the end of the line
dash Pattern
solid Solid (default)
shortDot Round dot
shortDash Square dot
dash Dash
dashDot Dash dot
longDash Long dash
longDashDot Long dash dot
longDashDotDot Long dash dot dot
dot Dots with wider gaps
shortDashDot Dash dot with the dots close together
shortDashDotDot Dash dot dot with the dots close together

A custom dash is a list of dashes and the gaps after them, each measured in multiples of the line's width. With round caps, a dash of length 0 draws a round dot:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "line",
                            transformation: { width: 300, height: 0 },
                            // Round dots, then dashes three times as long as the line is wide
                            line: {
                                width: 3,
                                cap: "round",
                                dash: [
                                    { length: 0, gap: 2 },
                                    { length: 3, gap: 2 },
                                ],
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});

An Arrowhead is one of "triangle", "stealth", "diamond", "oval" or "arrow", or an object with a type and a width and length of "small", "medium" (the default) or "large".

?> Arrowheads can be put on any shape's line, but they are only drawn on open shapes such as line, the connectors and arc.

Text in Shapes

text puts a line of text in the middle of a shape, in the document's default font. Each \n starts a new, centred paragraph. If the document's paragraphs have space before or after them, these paragraphs don't, so the text stays in the middle:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "flowChartProcess",
                            text: "Review",
                            transformation: { width: 120, height: 40 },
                            fill: "DEEBF7",
                        }),
                    ],
                }),
            ],
        },
    ],
});

For text with formatting, add paragraphs to children. The text is centred vertically. Use the paragraph's alignment to centre it horizontally. text comes before children when a shape has both.

import { AlignmentType, Document, Paragraph, TextRun } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "flowChartTerminator",
                            transformation: { width: 120, height: 48 },
                            fill: "4472C4",
                            line: "none",
                            children: [
                                new Paragraph({
                                    alignment: AlignmentType.CENTER,
                                    children: [new TextRun({ text: "Start", color: "FFFFFF", bold: true })],
                                }),
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

Sizing a shape to its text

Set width or height to "fitText" to size a shape to its text:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // As wide as "Write the draft" on one line, and 40 pixels tall
                        new ShapeRun({
                            type: "flowChartProcess",
                            text: "Write the draft",
                            transformation: { width: "fitText", height: 40 },
                        }),

                        // 160 pixels wide, and as tall as the text wrapped at that width
                        new ShapeRun({
                            type: "rectangle",
                            children: [new Paragraph("A longer note that wraps")],
                            transformation: { width: 160, height: "fitText" },
                        }),
                    ],
                }),
            ],
        },
    ],
});
  • width: "fitText" fits the longest line of text, without wrapping.
  • height: "fitText" fits every line, wrapped at the shape's width unless textOptions.wrap is false.
  • Both include the text margins, and allow for shapes whose text box is smaller than the shape, such as ellipses and diamonds.
  • Text that runs up or down the shape (textOptions.direction) is measured across the shape's height.

The size is worked out when the document is written, from how wide each character is in its font and size. Widths are built in for Calibri, Cambria, Arial, Times New Roman and Courier New. Other fonts are measured as the one most like them: Aptos and other sans-serif fonts as Arial, and serif fonts as Times New Roman.

The text is measured in the document's styles, as Word formats it:

  • The font, size and bold of the document's defaults (styles.default.document), the paragraph's style (such as a heading or Normal), the run's character style, and the run's own formatting, each overriding the one before. All caps, small caps, hidden text, character spacing and scale are included.
  • The paragraph's space before and after, line spacing and indents, from the same styles and the paragraph's own formatting. Contextual spacing leaves out the space between paragraphs of the same style.
  • Text in a document with no default font or size is measured in Word's own defaults, 10pt Times New Roman.
import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    // Word's defaults for new documents: 11pt Calibri, with space after each paragraph
    styles: { default: { document: { run: { font: "Calibri", size: 22 }, paragraph: { spacing: { after: 160, line: 259 } } } } },
    sections: [
        {
            children: [
                new Paragraph({
                    // As wide as "Write the draft" in 11pt Calibri
                    children: [
                        new ShapeRun({
                            type: "flowChartProcess",
                            text: "Write the draft",
                            transformation: { width: "fitText", height: 40 },
                        }),
                    ],
                }),
            ],
        },
    ],
});

Fonts that come from the document's theme, such as those in styles copied from a Word document with externalStyles, aren't known, as the library doesn't write a theme. Give those styles a font by name if shapes should fit their text.

The estimate is close for the built-in fonts and rougher for others. For an exact fit once Word has laid the text out, use textOptions.resizeShapeToFitText as well.

Text layout

textOptions sets how the text is laid out in the shape:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 200, height: 100 },
                            children: [new Paragraph("Top left")],
                            textOptions: {
                                verticalAlignment: "top",
                                margins: { top: 4, right: 8, bottom: 4, left: 8 }, // points
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Property Type Description
verticalAlignment "top" | "center" | "bottom" Where the text sits between the top and bottom of the shape. Default is "center"
margins { top, right, bottom, left } Space between the shape's edges and the text, in points. Word's defaults are 7.2 left and right, 3.6 top and bottom
wrap boolean Whether lines wrap at the shape's edges. Default is true
resizeShapeToFitText boolean Grows or shrinks the shape to fit its text when Word lays it out
shrinkTextOnOverflow boolean Makes the text smaller when there is too much of it. Can't be used with resizeShapeToFitText
direction ShapeTextDirection "horizontal" (the default), "topToBottom", "bottomToTop", "stacked" and more
columns { count, spacing? } Lays the text out in 1 to 16 columns, spacing points apart
warp ShapeTextWarp Bends or stretches the text like WordArt, such as "archUp", "wave" or "inflate"

Shape Patterns and Text Warps lists every warp and direction. Word applies resizeShapeToFitText and shrinkTextOnOverflow when it next lays the shape out, such as when the text is edited.

import { AlignmentType, Document, Paragraph, TextRun } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // WordArt: text bent into an arch
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 200, height: 80 },
                            line: "none",
                            children: [
                                new Paragraph({
                                    alignment: AlignmentType.CENTER,
                                    children: [new TextRun({ text: "WordArt", size: 48, bold: true })],
                                }),
                            ],
                            textOptions: { warp: "archUp", wrap: false },
                        }),
                    ],
                }),
            ],
        },
    ],
});

?> Word may not show text in columns inside a shape. PowerPoint and LibreOffice do.

Adjustments

Many shapes have handles in Word that change their proportions, such as the corner radius of a rounded rectangle or the thickness of an arrow. adjustments sets them. Each shape has its own adjustments, named after what they change:

Shape Adjustments Default
roundedRectangle cornerRadius: radius of the corners 16.667
triangle apexPosition: where the top point is, from the left 50
star5 innerRadius: radius of the inner points 38.196
donut thickness: thickness of the ring 25
rightArrow shaftThickness, headLength 50, 50
pie, arc, chord startAngle, endAngle depends on shape
rectangularCallout, ellipticalCallout pointerX, pointerY: tip of the pointer -20.833, 62.5

Lengths and positions are percentages, and angles are in degrees clockwise from 3 o'clock. Most lengths are a percentage of the shape's shorter side, so a cornerRadius of 50 makes the ends of a rounded rectangle fully round. Shape Adjustments lists the adjustments of all 120 shapes that have them, with what each percentage is of and its default.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "roundedRectangle",
                            adjustments: { cornerRadius: 30 },
                            transformation: { width: 120, height: 60 },
                            fill: "ED7D31",
                        }),

                        new ShapeRun({
                            type: "pie",
                            adjustments: { startAngle: 0, endAngle: 270 },
                            transformation: { width: 60, height: 60 },
                            fill: "7030A0",
                        }),
                    ],
                }),
            ],
        },
    ],
});

In TypeScript, adjustments only accepts the names that belong to the shape's type, so your editor suggests them. Word keeps each value within the range the shape allows, so a value that is too large acts like the largest allowed value.

Effects

effects adds shadows, a glow, soft edges and a reflection. They can be combined, and each takes sizes in points and angles in degrees:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "roundedRectangle",
                            transformation: { width: 120, height: 60 },
                            fill: "DEEBF7",
                            effects: {
                                shadow: {}, // Word's "Offset: Bottom Right" shadow
                                glow: { color: "FFC000", size: 8 },
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
Effect Type Description
shadow ShapeShadow A shadow behind the shape. {} gives a black shadow 60% transparent, 3pt away, down and to the right
innerShadow ShapeShadow A shadow inside the shape, as if it were cut out of the page
glow ShapeGlow A glow around the shape: a color, a size in points (default 5) and a transparency (default 60)
softEdges number Blurs the shape's edges inwards by this many points
reflection ShapeReflection A reflection below the shape. {} gives a half reflection touching the shape

A shadow has a color (default 000000), transparency (default 60), blur in points (default 4), distance in points (default 3) and angle, the direction it falls in degrees clockwise from 3 o'clock (default 45). A reflection has a transparency where it meets the shape (default 50), a size, the percentage of the shape it reflects (default 50), a distance below the shape and a blur, both in points.

The drawing's box grows to make room for the effects, so they aren't cut off.

Custom Shapes

For a shape that isn't one of the presets, give type: "custom" and the outline as SVG path data. The path can be in any units: it is scaled so the box around it fills the shape's transformation.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // A star
                        new ShapeRun({
                            type: "custom",
                            path: "M 50 0 L 61 35 L 98 35 L 68 57 L 79 91 L 50 70 L 21 91 L 32 57 L 2 35 L 39 35 Z",
                            transformation: { width: 80, height: 76 },
                            fill: "FFC000",
                        }),

                        // An open curve, with arrowheads
                        new ShapeRun({
                            type: "custom",
                            path: "M 0 50 C 30 0 70 100 100 50",
                            transformation: { width: 120, height: 50 },
                            line: { width: 2, endArrow: "triangle" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

The path can use the commands M (move), L (line), H and V (horizontal and vertical lines), C and S (cubic curves), Q and T (quadratic curves), A (elliptical arcs) and Z (close), in upper case for absolute coordinates and lower case for relative ones. A path that isn't closed is still filled, as in SVG, but its line and arrowheads are drawn only along the path.

Custom shapes can hold text like any other shape. Connectors attach to the corners and ends of the path, unless the shape has connection points of its own.

Several paths

paths draws a shape from several paths, in the same units, such as the faces of a box or the body and top of a cylinder. The box around all of them is scaled to fill the shape, and later paths are drawn over earlier ones.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // A box whose top is lighter and whose right side is darker than its fill
                        new ShapeRun({
                            type: "custom",
                            paths: [
                                { path: "M 0 25 L 50 0 L 100 25 L 50 50 Z", fill: "lighter" },
                                { path: "M 0 25 L 50 50 L 50 110 L 0 85 Z" },
                                { path: "M 50 50 L 100 25 L 100 85 L 50 110 Z", fill: "darker" },
                            ],
                            transformation: { width: 80, height: 88 },
                            fill: "4472C4",
                        }),

                        // A page with a folded corner, and lines that are drawn but not filled
                        new ShapeRun({
                            type: "custom",
                            paths: [
                                { path: "M 0 0 H 70 L 100 30 V 130 H 0 Z" },
                                { path: "M 70 0 V 30 H 100", fill: "darker" },
                                { path: "M 15 55 H 85 M 15 75 H 85 M 15 95 H 60", fill: false },
                            ],
                            transformation: { width: 68, height: 88 },
                            fill: "F2F2F2",
                            line: "7F7F7F",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Each path is filled with the shape's fill and outlined with its line, as Word's preset shapes shade the top of a cube or the inside of a can:

Option Type Description
path string SVG path data
fill CustomShapePathFill true (the default) fills the path with the shape's fill, and false leaves it empty. "lighter", "slightlyLighter", "darker" and "slightlyDarker" shade the fill
line boolean Whether the shape's line is drawn along the path. Default is true

A shape has one fill and one line, so paths can't have colours of their own. For parts in different colours, put shapes in a group.

Holes

A closed part of a path inside another part is a hole in it, and a part inside a hole is filled again. This works whichever way round each part is drawn: parts are turned round where they need to be, so that applications that fill paths by the non-zero rule, which needs a hole to go the other way, cut the same holes as LibreOffice, which fills by the even-odd rule.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        // A ring
                        new ShapeRun({
                            type: "custom",
                            path: "M 0 50 A 50 50 0 0 1 100 50 A 50 50 0 0 1 0 50 Z M 22 50 A 28 28 0 0 1 78 50 A 28 28 0 0 1 22 50 Z",
                            transformation: { width: 88, height: 88 },
                            fill: "70AD47",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Holes are cut within a path. Separate paths in paths are filled one after the other, so they don't cut holes in each other. A path that crosses itself, such as a star drawn as one line with five points, is filled differently by different applications: LibreOffice leaves its middle empty. Draw the outline of a star instead.

Text area

The text of a custom shape uses the whole shape, unless textArea gives the box it goes in, in the units of the paths. A speech bubble's text can then go in the bubble and not its tail. "fitText" sizes the shape so its text fits in the text area.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "custom",
                            path: "M 10 0 H 150 A 10 10 0 0 1 160 10 V 70 A 10 10 0 0 1 150 80 H 60 L 35 105 L 40 80 H 10 A 10 10 0 0 1 0 70 V 10 A 10 10 0 0 1 10 0 Z",
                            textArea: { left: 0, top: 0, right: 160, bottom: 80 },
                            transformation: { width: 180, height: "fitText" },
                            fill: "FBE5D6",
                            text: "Clear of the tail",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Connection points

Connectors attach to the corners and ends of a custom shape's paths. For a shape whose corners aren't where connectors should meet it, such as a cylinder, connectionPoints gives the points of its own, in the units of the paths. Each point faces the side of the shape it is nearest, or the side given, and connectors leave it towards that side. A connector's end with a side attaches to the point facing that side, and one with a point to the nearest point. An empty list leaves the shape without connection points, and connectors end at the middles of its sides.

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                {
                                    id: "server",
                                    type: "rectangle",
                                    text: "Server",
                                    transformation: { offset: { top: 28 }, width: 110, height: 44 },
                                },
                                {
                                    id: "database",
                                    type: "custom",
                                    paths: [
                                        { path: "M 0 12 A 50 12 0 0 0 100 12 V 88 A 50 12 0 0 1 0 88 Z" },
                                        { path: "M 0 12 A 50 12 0 0 1 100 12 A 50 12 0 0 1 0 12 Z", fill: "lighter" },
                                    ],
                                    // The middles of its top, right side, bottom and left side
                                    connectionPoints: [
                                        { x: 50, y: 0 },
                                        { x: 100, y: 50 },
                                        { x: 50, y: 100 },
                                        { x: 0, y: 50 },
                                    ],
                                    // Below the top
                                    textArea: { left: 0, top: 24, right: 100, bottom: 100 },
                                    text: "Orders",
                                    transformation: { offset: { left: 200 }, width: 90, height: 100 },
                                    fill: "FFC000",
                                },
                                { type: "connector", from: "server", to: "database", line: { endArrow: "triangle" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

The connection points and text area keep their place in the shape when it is resized in Word, as the points of freeforms drawn in Office do.

link makes a shape open a web address when it is clicked (with Ctrl, in Word). decorative: true is Word's Mark as decorative: screen readers skip the shape, which suits divider bars and other shapes that don't carry meaning.

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "roundedRectangle",
                            transformation: { width: 160, height: 40 },
                            fill: "4472C4",
                            children: [new Paragraph("Visit docx.js.org")],
                            link: "https://docx.js.org",
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "rectangle",
                            transformation: { width: 600, height: 3 },
                            fill: "BFBFBF",
                            line: "none",
                            decorative: true,
                        }),
                    ],
                }),
            ],
        },
    ],
});

Groups, canvases and the shapes, pictures and groups inside them take link and decorative too.

Inline and Floating

A shape sits in the line of text by default. It is aligned to the text baseline and its height adds to the height of the line. To raise or lower it from the baseline, give the run it is in a position, as for images: run: { position: "-4pt" }.

Add floating to position it on the page instead. The options are the same as for floating images, including horizontalPosition, verticalPosition, wrap, margins, behindDocument and zIndex. Offsets are in EMUs (914400 per inch).

import {
    Document,
    HorizontalPositionRelativeFrom,
    Paragraph,
    TextRun,
    TextWrappingSide,
    TextWrappingType,
    VerticalPositionRelativeFrom,
} from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "roundedRectangularCallout",
                            transformation: { width: 180, height: 80 },
                            fill: "FFF2CC",
                            children: [new Paragraph("Shapes can float, too!")],
                            floating: {
                                horizontalPosition: { relative: HorizontalPositionRelativeFrom.COLUMN, offset: 4000000 },
                                verticalPosition: { relative: VerticalPositionRelativeFrom.PARAGRAPH, offset: 0 },
                                wrap: { type: TextWrappingType.SQUARE, side: TextWrappingSide.LEFT },
                            },
                        }),
                        new TextRun(
                            "This paragraph wraps on the left of the callout, which floats at the right of the column. The text runs down beside it until it is below the callout, and then goes on across the whole column again.",
                        ),
                    ],
                }),
            ],
        },
    ],
});

?> Use TextWrappingType.SQUARE or TextWrappingType.TOP_AND_BOTTOM to wrap text around a shape's box. TextWrappingType.TIGHT and TextWrappingType.THROUGH wrap it around the shape's outline instead.

Sizes and positions as percentages

A floating shape's width and height can be percentages, such as "100%", and so can its offsets. Word keeps them as percentages, so the shape grows and moves with the page if its size or margins change. A rule as wide as the text:

import { Document, HeadingLevel, HorizontalPositionRelativeFrom, Paragraph, TextRun, VerticalPositionRelativeFrom } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    heading: HeadingLevel.HEADING_1,
                    children: [
                        new TextRun("Page layout"),
                        new ShapeRun({
                            type: "line",
                            transformation: { width: "100%", height: 0 },
                            line: { color: "2E74B5", width: 2 },
                            floating: {
                                horizontalPosition: { relative: HorizontalPositionRelativeFrom.MARGIN, offset: 0 },
                                verticalPosition: { relative: VerticalPositionRelativeFrom.PARAGRAPH, offset: 380000 },
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});
  • A percentage size is of the space between the margins, where the text goes. floating.sizeRelativeTo makes it of something else: { width: "page" }, or "leftMargin", "rightMargin", "insideMargin" or "outsideMargin" for a width, and "page", "topMargin", "bottomMargin", "insideMargin" or "outsideMargin" for a height.
  • A percentage offset, such as offset: "75%", is of the width or height of what the position is relative to: the page, the space between the margins (MARGIN), or one of the margins. It can't be of a column, character, paragraph or line.
  • A shape in a group or canvas, or one in the line of text, can't have a percentage size.

The shape is written with the percentages, as Word 2010 and later read them (wp14:sizeRelH, wp14:sizeRelV, wp14:pctPosHOffset and wp14:pctPosVOffset), and with the size and offset it would have on an A4 page with 1-inch margins for applications that don't. LibreOffice reads the percentage sizes, and places the shape by the offset.

Text that flows from one shape to the next

Give shapes the same textFlow to make their text flow from one to the next, as the columns of a newsletter do. The first shape of the flow in the document holds the text, and what doesn't fit flows on into the next shape with the same textFlow, in the order they are in the document:

import { Document, HorizontalPositionRelativeFrom, Paragraph, VerticalPositionRelativeFrom } from "docx";
import { ShapeRun } from "docx/shapes";

const column = (offset: `${number}%`, children?: readonly Paragraph[]): ShapeRun =>
    new ShapeRun({
        type: "rectangle",
        transformation: { width: "31%", height: 180 },
        textFlow: "article",
        children,
        floating: {
            horizontalPosition: { relative: HorizontalPositionRelativeFrom.MARGIN, offset },
            verticalPosition: { relative: VerticalPositionRelativeFrom.PARAGRAPH, offset: 0 },
        },
    });

const articleParagraphs = [
    "Shapes can hold text that flows from one to the next, as the columns of a newsletter do. The text starts in the first shape of the flow, and what doesn't fit carries on in the next one, wherever it is in the document. Word lays the text out when it opens the document, so the flow follows the fonts and styles the document uses.",
    "This article starts in the box on the left and carries on in the box on the right.",
].map((text) => new Paragraph(text));

const doc = new Document({
    sections: [
        {
            children: [new Paragraph({ children: [column("0%", articleParagraphs), column("34%")] })],
        },
    ],
});

The shapes after the first continue its text, so their own text and children aren't written. Word and LibreOffice lay out the text when they open the document, so it follows the document's fonts. A flow is only for a ShapeRun: shapes in groups and canvases can't be in one.

Alternative Text

Give a shape altText so screen readers can describe it:

import { Document, Paragraph } from "docx";
import { ShapeRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeRun({
                            type: "star5",
                            transformation: { width: 60, height: 60 },
                            fill: "FFC000",
                            altText: { name: "Gold star", description: "A gold star awarded for good work" },
                        }),
                    ],
                }),
            ],
        },
    ],
});

A group or canvas without a description of its own is described from the text of its shapes and its connectors, following each flow from its start, such as "Start, then Write the draft, then Approved? Yes: Publish, then End. No: Fix it, then back to Write the draft." Branches use the connectors' labels, and a shape that leads to several shapes without labels, as in an org chart, lists them: "Chief Executive leads to Technology, Finance and Operations." Shapes are read by their altText.name, or their text, and those with neither are left out. A decorative group or canvas isn't described. Give the diagram a description of its own to say something else.

The rectangle behind a canvas's group, for applications that can't draw canvases, and the bands of swimlanes, are marked as decorative, so screen readers skip them.

Groups

A ShapeGroupRun keeps several shapes together, so they move, resize and wrap as one drawing. Each shape takes the same options as a ShapeRun, except floating, and its transformation.offset positions it in pixels.

import { Document, Paragraph } from "docx";
import { ShapeGroupRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeGroupRun({
                            children: [
                                { type: "ellipse", transformation: { width: 80, height: 80 }, fill: "5B9BD5", line: "none" },
                                { type: "star5", transformation: { offset: { left: 15, top: 12 }, width: 50, height: 50 }, fill: "FFC000" },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

The group is as big as the box around its shapes. Give it a transformation to scale every shape in it to a different size, or to rotate or flip them together:

import { Document, Paragraph } from "docx";
import { ShapeGroupRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeGroupRun({
                            children: [
                                { type: "ellipse", transformation: { width: 80, height: 80 }, fill: "5B9BD5", line: "none" },
                                { type: "star5", transformation: { offset: { left: 15, top: 12 }, width: 50, height: 50 }, fill: "FFC000" },
                            ],
                            transformation: { width: 40, height: 40, rotation: 20 }, // half the size, turned by 20 degrees
                        }),
                    ],
                }),
            ],
        },
    ],
});

A group can be floating, and can have altText, just like a single shape.

Pictures and Groups Inside Groups

A group or canvas can also hold pictures and other groups. A picture takes an image, as for an ImageRun, and a transformation. Connectors attach to the middle of its sides, and it can have a line, effects and a crop:

import { Document, Paragraph } from "docx";
import { ShapeGroupRun } from "docx/shapes";
import * as fs from "fs";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeGroupRun({
                            children: [
                                {
                                    id: "logo",
                                    type: "picture",
                                    image: { type: "png", data: fs.readFileSync("./demo/assets/images/linux-png.png") },
                                    transformation: { width: 60, height: 70 },
                                },
                                {
                                    id: "name",
                                    type: "rectangle",
                                    text: "Linux",
                                    transformation: { offset: { left: 140, top: 15 }, width: 100, height: 40 },
                                },
                                { type: "connector", from: "logo", to: "name", line: { endArrow: "triangle" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A group inside a group keeps its children together. Its children are positioned relative to each other, and its transformation.offset places the group. Give it a width and height to scale its children, or a rotation or flip:

import { Document, Paragraph } from "docx";
import { ShapeGroupRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeGroupRun({
                            children: [
                                { type: "rectangle", transformation: { width: 200, height: 100 }, fill: "F2F2F2" },
                                {
                                    type: "group",
                                    transformation: { offset: { left: 60, top: 10 }, width: 40, height: 40, rotation: 20 }, // half size, turned
                                    children: [
                                        { type: "ellipse", transformation: { width: 80, height: 80 }, fill: "5B9BD5", line: "none" },
                                        {
                                            type: "star5",
                                            transformation: { offset: { left: 15, top: 12 }, width: 50, height: 50 },
                                            fill: "FFC000",
                                        },
                                    ],
                                },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

Connectors can attach to shapes and pictures inside groups, so every id must be different across the whole drawing. A connector inside a group can only join shapes in that group.

Connectors

A connector is a line from one shape to another. Give each shape an id, then add a child with type: "connector" that names the shapes it joins. The connector is drawn between them, so it needs no size or position:

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                { id: "start", type: "flowChartTerminator", transformation: { width: 120, height: 40 }, fill: "4472C4" },
                                {
                                    id: "step",
                                    type: "flowChartProcess",
                                    transformation: { offset: { top: 90 }, width: 120, height: 44 },
                                    fill: "ED7D31",
                                },
                                { type: "connector", from: "start", to: "step", line: { endArrow: "triangle" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A connector attaches to the sides of the shapes that face each other. To choose a side, give an object with the shape's id and a side of "top", "right", "bottom" or "left":

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                {
                                    id: "draft",
                                    type: "flowChartProcess",
                                    text: "Write the draft",
                                    transformation: { width: 130, height: 40 },
                                },
                                {
                                    id: "fix",
                                    type: "flowChartProcess",
                                    text: "Fix it",
                                    transformation: { offset: { left: 200, top: 100 }, width: 100, height: 40 },
                                },
                                { type: "connector", from: { id: "fix", side: "top" }, to: { id: "draft", side: "right" }, route: "elbow" },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

To attach to a particular point, give a point instead of a side, as percentages of the shape's width and height before it is rotated. The connector attaches to the connection point nearest it, such as the tip of a diamond:

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                { id: "ask", type: "flowChartDecision", text: "Ready?", transformation: { width: 110, height: 70 } },
                                {
                                    id: "wait",
                                    type: "flowChartProcess",
                                    text: "Wait",
                                    transformation: { offset: { left: 200, top: 15 }, width: 100, height: 40 },
                                },
                                // From the right-hand point of a decision diamond
                                { type: "connector", from: { id: "ask", point: { x: 100, y: 50 } }, to: "wait" },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

route sets the path the connector takes:

route Path
"straight" A straight line between the two shapes (the default)
"elbow" Horizontal and vertical lines with right-angled bends, like Word's elbow connector
"curved" A smooth curve that leaves and arrives square to the shapes, like Word's curved connector

Elbow and curved connectors leave and arrive at right angles to the sides they attach to, and go around a shape when they have to, for example to join two shapes' top sides. margin sets how far out they go, in pixels (default 24, a quarter of an inch). Elbow connectors whose lines would lie on top of each other between their bends are moved 6 pixels apart, unless they meet at one end, as the lines from a box in an org chart do.

When the usual route would cross another shape in the group or canvas, an elbow or curved connector takes a route with up to four bends that goes around it, a margin away, if there is one. Where shapes are closer together than two margins, the route goes halfway between them. An elbow connector that needs more than four bends to get around the shapes, which Word's connectors can't have, is drawn as a freeform line instead. Word doesn't keep a freeform line attached to the shapes when they are moved.

Without a route, a connector is straight, unless a straight line would go through another shape in the group or canvas. Then it takes an elbow route around it. Give route: "straight" to keep it straight anyway.

label puts text on a connector, in a box centred on its route. Give a background fill to hide the line behind the text:

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                { id: "ask", type: "flowChartDecision", text: "Ready?", transformation: { width: 110, height: 70 } },
                                {
                                    id: "go",
                                    type: "flowChartProcess",
                                    text: "Go",
                                    transformation: { offset: { left: 5, top: 150 }, width: 100, height: 40 },
                                },
                                { type: "connector", from: "ask", to: "go", label: { text: "Yes", fill: "FFFFFF" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A label is a string, or an object with:

  • text: a string or paragraphs.
  • position: "start", "middle" (the default) or "end". At the start or end, the label sits just clear of the shape there, such as "Yes" and "No" beside a decision.
  • width and height: in pixels. By default the box fits the text, measured as for "fitText". A label given as a string has no space before or after it.
  • fill and line.

A label that would sit on a shape, on an earlier label or on another connector's line moves along its route until it is clear. If there is no room on the route, it goes beside it, on the side with fewer shapes and labels near it. In a flow or tree, the space between two levels grows to fit the labels of the connectors between them. A label is a separate text box, so Word doesn't move it when the connector moves.

Connectors that meet

When several connectors with arrowheads meet at the same point of a shape with straight sides, their ends are spread out along the side, so each arrowhead can be seen. Connectors between the same two shapes are spread out at both ends, so they run side by side. The ends are ordered by where the connectors go, so they don't cross.

Ends without arrowheads that go to different shapes stay together, as the lines from a box in an org chart do. So do ends given as a point, and ends on shapes without straight sides, such as ellipses and diamonds.

Spreading works on rectangles, rounded rectangles, pictures, and the process, alternate process, predefined process, internal storage, terminator and document flowchart shapes. The ends stay attached to the shape's connection point, so if a shape is moved in Word, Word joins them there again.

The connector's line works as for any shape: startArrow is drawn at from and endArrow at to. Connectors can come before or after the shapes they join in children; the order sets which is drawn on top.

A connector attaches at one of the points Word offers on the shape, such as the middle of a rectangle's side or the tip of a triangle, so Word treats it as connected. Shapes without connection points, such as the chart shapes, are connected at the middle of a side, or at the point given, without being attached.

Canvases

A ShapeCanvasRun is a drawing canvas: an area of the document that holds shapes, like Word's Insert > Shapes > New Drawing Canvas. It takes the same children as a group, but its shapes keep their own size.

Word only keeps connectors attached on a canvas. When you move a shape on a canvas in Word, its connectors follow it. In a group, connectors are drawn in the same way, but they stay where they are when a shape is moved, so use a canvas for diagrams that people will edit.

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            children: [
                                // Shapes and connectors
                                {
                                    id: "idea",
                                    type: "ellipse",
                                    text: "Idea",
                                    transformation: { offset: { left: 40, top: 40 }, width: 120, height: 60 },
                                    fill: "DEEBF7",
                                },
                                {
                                    id: "plan",
                                    type: "rectangle",
                                    text: "Plan",
                                    transformation: { offset: { left: 240, top: 200 }, width: 120, height: 60 },
                                    fill: "FBE5D6",
                                },
                                { type: "connector", from: "idea", to: "plan", route: "curved", line: { endArrow: "triangle" } },
                            ],
                            transformation: { width: 400, height: 300 },
                            fill: "F7F7F7",
                            line: "BFBFBF",
                        }),
                    ],
                }),
            ],
        },
    ],
});

Shapes are positioned with transformation.offset, in pixels from the canvas's top-left corner, or by a layout. Without a transformation, the canvas reaches from its corner to the right and bottom of the shapes, including their lines, arrowheads and effects. Anything drawn above or to the left of the corner, such as the line of a shape at the corner, moves everything onto the canvas. A canvas can be floating and can have altText.

Laying Out Diagrams

Give a canvas or group a layout to place its shapes for you. Shapes, pictures and groups without an offset are placed by the layout, and connectors are routed between them afterwards:

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            layout: { type: "flow", direction: "down" },
                            children: [
                                { id: "start", type: "flowChartTerminator", text: "Start", transformation: { width: 100, height: 36 } },
                                {
                                    id: "review",
                                    type: "flowChartDecision",
                                    text: "Approved?",
                                    transformation: { width: "fitText", height: 70 },
                                },
                                {
                                    id: "publish",
                                    type: "flowChartProcess",
                                    text: "Publish",
                                    transformation: { width: "fitText", height: 40 },
                                },
                                { id: "fix", type: "flowChartProcess", text: "Fix it", transformation: { width: "fitText", height: 40 } },
                                { type: "connector", from: "start", to: "review", line: { endArrow: "triangle" } },
                                {
                                    type: "connector",
                                    from: "review",
                                    to: "publish",
                                    route: "elbow",
                                    label: { text: "Yes", position: "start" },
                                },
                                { type: "connector", from: "review", to: "fix", route: "elbow", label: { text: "No", position: "start" } },
                                { type: "connector", from: "fix", to: "review", route: "elbow", line: { endArrow: "triangle" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});
layout.type Places the shapes
"flow" In levels along the connectors, like a flowchart. Each shape goes on the level after the shapes that connect to it, each level is ordered so connectors cross as little as they can, and each shape is lined up with the shapes it connects to, so connectors are as straight as they can be
"tree" As a tree, like an org chart. Each connector joins a parent (from) to a child (to), and each parent is centred over its children
"grid" In rows and columns, in the order they are given, each centred in its cell
Option Layouts Description
direction flow, tree "down" (the default), "right", "up" or "left": the way the levels run
spacing all Space between shapes on the same level, or between rows and columns, in pixels
levelSpacing flow, tree Space between one level and the next, in pixels
columns grid The number of columns. By default, enough to make the grid about as wide as it is tall
lanes flow Bands the flow runs along, such as the teams that do each step. See Swimlanes

Spacing defaults to 40 pixels between shapes and 50 between levels in a flow, 20 and 40 in a tree, and 40 in a grid.

In a flow or tree, a connector without a side leaves one level from the side facing the next level and arrives from the side facing the level before, such as bottom to top when the levels run down. A connector back to the level before goes between the same sides the other way, and one that leads further back, such as a loop to redo a step, leaves and arrives at the side (right, or bottom when the levels run across) and goes around the shapes. In a flow, connectors that lead back don't change the levels.

In a flow, a connector given a side across the levels places the shape it leads to on that side: a "No" connector that leaves the right of a decision (from: { id: "review", side: "right" }) puts its shape to the right of the decision's other shapes, and the decision stays lined up with them. Arriving at the left of a shape does the same.

A shape with an offset keeps it. If it connects to shapes the layout places, it takes part in the layout, and the layout is placed around it: give the first shape of a flow an offset to start the flow there. With several such shapes, the layout is placed as near all of them as it can be. A shape with an offset that doesn't connect to the layout's shapes is left out of it, and the layout starts at the top-left corner. A group inside a canvas or group is placed as one shape, and can have a layout of its own for its children. Rotated shapes are placed by the box around them.

The positions are worked out when the document is written, and written as ordinary offsets, so the diagram can be edited in Word like any other.

Swimlanes

Give a flow lanes to draw a process in bands, one for each person or team that does its steps. Each shape with a lane goes in the band of that name, and shapes without one go in the first. The bands are side by side across the flow, each with a header, with its name, at the start of the flow. Connectors cross between the bands.

import { Document, Paragraph } from "docx";
import { ShapeCanvasRun } from "docx/shapes";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ShapeCanvasRun({
                            layout: {
                                type: "flow",
                                lanes: [{ name: "Customer", fill: "F2F2F2" }, { name: "Support", fill: "DEEBF7" }, "Engineering"],
                            },
                            children: [
                                {
                                    id: "report",
                                    lane: "Customer",
                                    type: "flowChartTerminator",
                                    text: "Report a problem",
                                    transformation: { width: "fitText", height: 36 },
                                },
                                {
                                    id: "log",
                                    lane: "Support",
                                    type: "flowChartProcess",
                                    text: "Log the ticket",
                                    transformation: { width: "fitText", height: 40 },
                                },
                                {
                                    id: "fix",
                                    lane: "Engineering",
                                    type: "flowChartProcess",
                                    text: "Fix the bug",
                                    transformation: { width: "fitText", height: 40 },
                                },
                                { type: "connector", from: "report", to: "log", route: "elbow", line: { endArrow: "triangle" } },
                                { type: "connector", from: "log", to: "fix", route: "elbow", line: { endArrow: "triangle" } },
                            ],
                        }),
                    ],
                }),
            ],
        },
    ],
});

A lane is a name, or an object with these options:

Option Type Description
name string Written in the lane's header. Shapes go in the lane with lane: name
fill ShapeFill The lane's background. Default is none
headerFill ShapeFill The header's background. Default is light grey
line ShapeLine The line around the lane and its header. Default is a thin grey line

Each lane is as wide as its shapes and its name need. The shapes in each lane are lined up with each other, and elbow connectors (route: "elbow") keep a process's lines square as they cross between lanes. Swimlanes that run across the page (direction: "right") have their headers on the left.

Options

ShapeRun

Property Type Notes Description
type PresetShapeType | "custom" Required The preset shape, such as "rectangle", "ellipse" or "line", or "custom"
transformation ShapeTransformation Required Size in pixels, rotation and flip. See Size and Rotation
fill ShapeFill Optional See Fill. Default is no fill
line ShapeLine Optional See Line. Default is a black line 1pt wide
adjustments ShapeAdjustments<type> Optional The shape's handles, which depend on type. See Adjustments
path string Optional SVG path data, for type: "custom". See Custom Shapes
paths CustomShapePath[] Optional Several paths, for type: "custom", instead of path. See Several paths
textArea CustomShapeTextArea Optional Where the text of a custom shape goes. See Text area
connectionPoints CustomShapeConnectionPoint[] Optional The points connectors attach to on a custom shape. See Connection points
effects ShapeEffects Optional Shadows, glow, soft edges and reflection. See Effects
text string Optional Centred text inside the shape. See Text in Shapes
children Paragraph[] Optional Paragraphs inside the shape
textOptions ShapeTextOptions Optional How the text is laid out. See Text layout
floating ShapeFloating Optional Positions the shape on the page. See Inline and Floating
textFlow string Optional The text flow the shape is in. See Text that flows
altText DocPropertiesOptions Optional name, description and title for screen readers
link string Optional A web address the shape opens when it is clicked
decorative boolean Optional Marks the shape as decorative, so screen readers skip it
run IRunPropertiesOptions Optional Formatting of the run the shape is in, such as position to raise or lower it

ShapeGroupRun

Property Type Notes Description
children IShapeGroupChildOptions[] Required Shapes, each with the options of a ShapeRun except floating, plus an optional id, pictures, groups and connectors
transformation IMediaTransformation Optional Size in pixels, rotation and flip. Defaults to the box around the children
layout ShapeLayout Optional Places the children without an offset. See Laying Out Diagrams
floating IFloating Optional Positions the group on the page
altText DocPropertiesOptions Optional name, description and title for screen readers
run IRunPropertiesOptions Optional Formatting of the run the group is in, such as position to raise or lower it

ShapeCanvasRun

Property Type Notes Description
children IShapeCanvasChildOptions[] Required Shapes and connectors, as for a ShapeGroupRun
transformation { width: number; height: number } Optional Size in pixels. Defaults to reaching the right and bottom of the shapes
fill ShapeFill Optional The canvas's background. Default is none
line ShapeLine Optional The canvas's outline. Default is none
layout ShapeLayout Optional Places the children without an offset. See Laying Out Diagrams
floating IFloating Optional Positions the canvas on the page
altText DocPropertiesOptions Optional name, description and title for screen readers
fallback boolean Optional Whether to write the diagram as a group too, for applications that can't draw canvases. Default is true. See Compatibility
run IRunPropertiesOptions Optional Formatting of the run the canvas is in, such as position to raise or lower it

Connector

Property Type Notes Description
type "connector" Required
from ConnectorEnd Required The id of the shape it starts at, or { id, side } or { id, point }
to ConnectorEnd Required The id of the shape it ends at, or { id, side } or { id, point }
route ConnectorRoute Optional "straight", "elbow" or "curved". Default is straight, or elbow around a shape in the way
margin number Optional How far out elbow and curved connectors go, in pixels. Default 24
label string | ConnectorLabel Optional Text in a box on the route, at its position
line ShapeLine Optional See Line. startArrow is at from and endArrow at to
altText DocPropertiesOptions Optional name, description and title for screen readers

ShapeRun vs WpsShapeRun and WpgGroupRun

WpsShapeRun draws a rectangular text box, and WpgGroupRun groups text boxes and pictures. ShapeRun draws any shape, with or without text, and ShapeGroupRun groups shapes, pictures and groups, with simpler options for fills, lines, effects and text. They write the same kinds of DrawingML elements. WpsShapeRun and WpgGroupRun are deprecated, so use ShapeRun and ShapeGroupRun from docx/shapes for new documents. WpsShapeRun and WpgGroupRun stay in docx.

Compatibility

Shapes are written as DrawingML shapes (wps:wsp), groups (wpg:wgp and wpg:grpSp), pictures (pic:pic) and drawing canvases (wpc:wpc), the formats Word has used since Word 2010. Word 2007 and older can't display them. Other word processors differ in how much of DrawingML they draw, so check the result in the applications your readers use.

A canvas is written with the same shapes as a group after it, in mc:AlternateContent. Applications that can't draw canvases, such as Apple Pages, draw the group instead. Word and LibreOffice draw the canvas. Set fallback: false on a ShapeCanvasRun to leave the group out, which halves the space the canvas takes up in the document, if its readers only use Word or LibreOffice.

LibreOffice (checked with version 26.8) draws most shapes as Word does, but:

  • It doesn't draw reflections, inner shadows, double and triple lines (compound) or gradient lines. A gradient line is drawn in its first colour.
  • On a canvas, it draws connectors between their shapes' connection points in its own way, rather than on the route in the document. A connector that goes around a shape may go through it, and connector ends that are spread along a side meet at the connection point. In a group, it draws the route in the document.
  • It draws the text of a shape that has resizeShapeToFitText, or of a group or canvas that starts a new line, on the first line of the paragraph.
  • It doesn't turn the text of a rotated shape with the shape.
  • It fills a custom shape's path by the even-odd rule, so a path that crosses itself has gaps where it encloses an area twice.

Apple Pages (checked with version 15.1) draws shapes and groups, and the group in place of a canvas, but:

  • It draws text that has no size, in its own formatting or the document's styles, larger than Word does. Shapes sized with "fitText" and connector labels are sized for Word's 10pt, so give the document a default size (styles.default.document.run.size) if the text has to fit in Pages as well.

Examples

Inline shapes

Bars, rules and signature lines in the flow of text, as on a printed form.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/inline-shapes.ts

Shapes, lines and arrows

Preset shapes, dashes and arrowheads, gradients, transparency, text in shapes and floating shapes.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shapes.ts

Shape groups

A flowchart built from a group of shapes, the same group scaled down, and a floating, rotated group.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-groups.ts

Connectors and canvases

A flowchart on a canvas whose connectors stay attached, straight, elbow and curved routes, and connectors on chosen sides.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-connectors.ts

Shape styles

Effects, pattern and picture fills, line styles, text layout and WordArt warps, custom shapes, links and decorative shapes.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-styles.ts

Diagrams

Pictures and groups on a canvas, connector labels, connecting to a point on a shape, and connectors that go around shapes.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-diagrams.ts

Automatic layout

Shapes sized to fit their text, a flowchart, an org chart and a grid laid out automatically, spread and side-by-side connectors, and labels at the start of a connector.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-layout.ts

Shapes in the document's styles

Shapes, a canvas and a group sized to fit their text in 11pt Calibri with space after each paragraph, with a heading, a character style and an indented paragraph in a shape.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-document-styles.ts

Swimlanes

A support process in swimlanes down the page, with a decision and a branch to another team, and a shorter process in lanes across the page.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-swimlanes.ts

Page layout

A rule as wide as the text, a sidebar sized and placed as percentages of the text's width, and an article whose text flows from one box into the next.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/shape-page-layout.ts

Theme colours

A document with a theme of its own, swatches of its colours and of lighter and darker versions of one, and a diagram in them.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/styles/theme.ts

Custom shapes

Custom shapes drawn from several paths, with shaded faces, holes, text areas, and connection points of their own on a canvas.

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/shapes/custom-shapes.ts