* chore: organise the demos into a folder for each topic The demos were one flat list of 124 numbered files, which made it hard to find the one you wanted. Each is now in a folder for its topic, such as demo/tables/basic-table.ts, with a name that says what it shows instead of a number. npm run demo lists every demo and narrows the list as you type. It also takes a demo's path or name, a topic, or search words. The demos' images moved to demo/assets/images, as images is now a topic. The docs' links, the Demos workflow, the shape render script and its reference images use the new paths and names. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TmyffPd5QMJYjXiMBQ3EnZ * fix: list only a topic's own demos, and drop stale output file names npm run demo text also offered the text-boxes demos and others with "text" in their name, so a topic now searches for "text/". Three demos logged their old numbered file name after writing My Document.docx. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
9.4 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, Packer, 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
new TextWatermark({
text: "CONFIDENTIAL",
font: "Arial",
color: "FF0000",
opacity: 0.3,
bold: true,
});
Horizontal Layout
new TextWatermark({
text: "SAMPLE",
layout: "horizontal",
});
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:
new TextWatermark({
text: "DRAFT",
width: 400,
height: 100,
});
Image Watermark
import * as fs from "fs";
import { Document, Header, Packer, 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("./logo.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
new ImageWatermark({
type: "png",
data: fs.readFileSync("./logo.png"),
transformation: { width: 300, height: 300 },
washout: false,
});
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:
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: [/* ... */],
},
],
});
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:
new Header({
children: [
new Paragraph({
children: [new TextWatermark({ text: "DRAFT" }), new TextRun("Company Name")],
}),
],
});
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
Source: https://github.com/dolanmiu/docx/blob/master/demo/watermarks/text-watermark.ts
Image Watermark
Source: https://github.com/dolanmiu/docx/blob/master/demo/watermarks/image-watermark.ts