Files
docx/docs/usage/watermarks.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

13 KiB

Watermarks

!> Watermarks require an understanding of Headers and Footers.

A watermark is faint text or a picture drawn behind the content of every page, such as "DRAFT", "CONFIDENTIAL" or a company logo.

Word stores a watermark as a shape inside the page header, so it repeats on every page of the section. docx follows the same approach: place a TextWatermark or ImageWatermark inside a Paragraph in a Header. The generated markup matches what Word writes, so Word recognizes the watermark and can remove or replace it through its own Design > Watermark menu.

Importing

Watermarks come with the docx package. Import them from docx/watermarks, and everything else, such as the document, its headers and their paragraphs, from docx:

import { Document, Header, Paragraph } from "docx";
import { ImageWatermark, TextWatermark } from "docx/watermarks";

In a page without a bundler, load dist/watermarks.umd.cjs after dist/index.umd.cjs (or dist/watermarks.iife.js after dist/index.iife.js). It adds a docxWatermarks global, such as new docxWatermarks.TextWatermark({ text: "DRAFT" }).

Text Watermark

import { Document, Header, Paragraph } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [new TextWatermark({ text: "DRAFT" })],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

By default the text is drawn diagonally across the page in semi-transparent silver Calibri, which matches Word's default watermark.

Text Watermark Options

Property Type Notes Description
text string Required The text to display
font string Optional Font family. Default is Calibri
fontSize number Optional Font size in points. When omitted the text is sized automatically to fill the watermark's width and height, as in Word
bold boolean Optional Bold text
italics boolean Optional Italic text
color string Optional A named colour such as silver or a hex value such as C0C0C0. Default is silver
opacity number Optional From 0 (invisible) to 1 (solid). Default is 0.5, which is Word's "Semitransparent" setting
layout "diagonal" | "horizontal" Optional Whether the text runs diagonally across the page or horizontally. Default is diagonal
rotation number Optional Clockwise rotation in degrees. Overrides the rotation implied by layout
width number Optional Width of the watermark in points. Default is 527.85, which spans a Letter or A4 page diagonally
height number Optional Height of the watermark in points. Defaults to a height that keeps the letters in proportion for the width and text length

Customising the Text

import { Document, Header, Paragraph } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [
                                new TextWatermark({
                                    text: "CONFIDENTIAL",
                                    font: "Arial",
                                    color: "FF0000",
                                    opacity: 0.3,
                                    bold: true,
                                }),
                            ],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

Horizontal Layout

import { Document, Header, Paragraph } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [
                                new TextWatermark({
                                    text: "SAMPLE",
                                    layout: "horizontal",
                                }),
                            ],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

Sizing

Word stretches watermark text to fill the shape, so the shape's width and height control how large the text appears. Because docx cannot measure fonts, the default height is estimated from the text length so that the letters keep their proportions. Set width and height explicitly to take full control:

import { Document, Header, Paragraph } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [
                                new TextWatermark({
                                    text: "DRAFT",
                                    width: 400,
                                    height: 100,
                                }),
                            ],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

Image Watermark

import * as fs from "fs";
import { Document, Header, Paragraph } from "docx";
import { ImageWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [
                                new ImageWatermark({
                                    type: "png",
                                    data: fs.readFileSync("./demo/assets/images/dog.png"),
                                    transformation: {
                                        width: 400,
                                        height: 400,
                                    },
                                }),
                            ],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

The image is centred on the page and, by default, washed out so that the document text remains readable over it. This matches Word's "Washout" option.

Image Watermark Options

Property Type Notes Description
type "jpg" | "png" | "gif" | "bmp" Required The image format. SVG is not supported by VML pictures
data Buffer | string | Uint8Array | ArrayBuffer Required The image data, or a base64-encoded data URI
transformation { width: number; height: number } Required Size at which the image is drawn, in pixels, matching ImageRun
washout boolean Optional Lightens the image so that text stays readable. Default is true
title string Optional Title of the image, shown by Word as the picture's name

Full-Strength Image

import * as fs from "fs";
import { Document, Header, Paragraph } from "docx";
import { ImageWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [
                                new ImageWatermark({
                                    type: "png",
                                    data: fs.readFileSync("./demo/assets/images/dog.png"),
                                    transformation: { width: 300, height: 300 },
                                    washout: false,
                                }),
                            ],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

Watermarks on Specific Pages

Watermarks live in headers, so they follow the header rules. Use a first header to watermark only the cover page, or omit the watermark from the first header to leave the cover page clean:

import { Document, Header, Paragraph } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            properties: {
                titlePage: true,
            },
            headers: {
                first: new Header({
                    children: [new Paragraph("Cover page without a watermark")],
                }),
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [new TextWatermark({ text: "DRAFT" })],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Cover page"), new Paragraph({ text: "Second page, with the watermark", pageBreakBefore: true })],
        },
    ],
});

Each section has its own headers, so different sections can carry different watermarks, or none at all.

Combining with Header Text

The watermark is an inline element, so it can share a paragraph with ordinary header content:

import { Document, Header, Paragraph, TextRun } from "docx";
import { TextWatermark } from "docx/watermarks";

const doc = new Document({
    sections: [
        {
            headers: {
                default: new Header({
                    children: [
                        new Paragraph({
                            children: [new TextWatermark({ text: "DRAFT" }), new TextRun("Company Name")],
                        }),
                    ],
                }),
            },
            children: [new Paragraph("Document content")],
        },
    ],
});

How It Works

Word draws watermarks with VML (Vector Markup Language), the legacy drawing format still used for objects in headers and footers. A text watermark is a WordArt shape (v:shape of type _x0000_t136) with a v:textpath carrying the text, and an image watermark is a picture frame shape (_x0000_t75) with a v:imagedata referencing the embedded image. Both are anchored to the centre of the page margins and given a negative z-index so they sit behind the document text.

Demos

Text Watermark

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/watermarks/text-watermark.ts

Image Watermark

Example

Source: https://github.com/dolanmiu/docx/blob/master/demo/watermarks/image-watermark.ts