* 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>
5.8 KiB
Text Box
Similar to Text Frames, but the difference being that it is VML Shape based.
!> Text Boxes require an understanding of Paragraphs.
Text boxesare paragraphs of text in a document which are positioned in a separate region or frame in the document, and can be positioned with a specific size and position relative to non-frame paragraphs in the current document.
Basic Text Box
Create a text box with basic content:
import { Document, Paragraph, TextRun, Textbox } from "docx";
const doc = new Document({
sections: [
{
children: [
new Textbox({
children: [
new Paragraph({
children: [new TextRun("Hello from a textbox!")],
}),
],
style: {
width: "200pt",
height: "100pt",
},
}),
],
},
],
});
Textbox Options
| Property | Type | Notes | Description |
|---|---|---|---|
| children | Paragraph[] |
Required | Content inside the textbox |
| style | ITextboxStyle |
Optional | Positioning and size options |
| alignment | string |
Optional | Horizontal alignment |
Sizing
Fixed Size
new Textbox({
children: [new Paragraph("Fixed size textbox")],
style: {
width: "200pt",
height: "100pt",
},
});
Auto Height
new Textbox({
children: [new Paragraph("Auto height based on content")],
style: {
width: "200pt",
height: "auto",
},
});
Alignment
Align the textbox horizontally:
new Textbox({
alignment: "center",
children: [new Paragraph("Centered textbox")],
style: {
width: "200pt",
height: "auto",
},
});
Available alignment values:
"left"- Align to left margin"center"- Center on page"right"- Align to right margin
In a Paragraph
A text box can also be one of a paragraph's children, such as after some text. It is then part of that paragraph, and its own paragraph options, such as alignment, don't apply:
new Paragraph({
children: [
new TextRun("See the note: "),
new Textbox({
children: [new Paragraph("A note")],
style: {
width: "200pt",
height: "auto",
},
}),
],
});
Multiple Paragraphs
Text boxes can contain multiple paragraphs:
new Textbox({
children: [
new Paragraph({
text: "Title",
heading: HeadingLevel.HEADING_2,
}),
new Paragraph("First paragraph of content."),
new Paragraph("Second paragraph of content."),
],
style: {
width: "300pt",
height: "auto",
},
});
Styled Content
Apply formatting to text within the textbox:
new Textbox({
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "Important Notice",
bold: true,
size: 28,
}),
],
}),
new Paragraph({
alignment: AlignmentType.CENTER,
children: [new TextRun("This is highlighted information")],
}),
],
style: {
width: "250pt",
height: "auto",
},
});
Complete Example
Sidebar callout box:
import { AlignmentType, Document, HeadingLevel, Packer, Paragraph, TextRun, Textbox } from "docx";
import * as fs from "fs";
const doc = new Document({
sections: [
{
children: [
new Paragraph("Main document content goes here..."),
new Textbox({
alignment: "right",
children: [
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "Did You Know?",
bold: true,
color: "2F5496",
}),
],
}),
new Paragraph({
alignment: AlignmentType.CENTER,
children: [
new TextRun({
text: "docx can generate Word documents in both Node.js and browsers!",
italics: true,
}),
],
}),
],
style: {
width: "180pt",
height: "auto",
},
}),
new Paragraph("More document content continues..."),
],
},
],
});
Packer.toBuffer(doc).then((buffer) => {
fs.writeFileSync("document.docx", buffer);
});
Text Box vs Text Frame
| Feature | Text Box | Text Frame |
|---|---|---|
| Based on | VML Shape | Paragraph framing |
| Positioning | More flexible | Limited |
| Compatibility | Older Word versions | Modern Word |
For most use cases, Text Frames are recommended. See Text Frames for more information.
Demo
Source: https://github.com/dolanmiu/docx/blob/master/demo/text-boxes/text-box.ts