DOCX
Text and Paragraphs
Work with paragraphs, text runs, formatting, alignment, spacing, indentation, lists, and tab stops
Documents are built from Paragraph elements, each containing TextRun children with individual formatting.
Paragraph
import { Paragraph, TextRun } from "@office-open/docx";
// Simple paragraph
new Paragraph({
children: [new TextRun("Plain text paragraph.")],
});
// Heading
new Paragraph({
heading: "Heading1",
children: [new TextRun("Chapter Title")],
});
Alignment and Spacing
new Paragraph({
alignment: "center",
spacing: { before: 200, after: 200, line: 360 },
indent: { left: 720 },
children: [new TextRun("Centered paragraph with spacing and indent.")],
});
Borders
new Paragraph({
border: {
bottom: { style: "single", size: 6, color: "999999" },
},
children: [new TextRun("Paragraph with a bottom border.")],
});
TextRun
Each TextRun can have its own formatting:
new Paragraph({
children: [
new TextRun({ text: "Bold", bold: true }),
new TextRun({ text: ", Italic", italics: true }),
new TextRun({ text: ", Underline", underline: { type: "single" } }),
new TextRun({ text: ", Strikethrough", strike: true }),
new TextRun({ text: ", 24pt", size: 48 }),
new TextRun({ text: ", Colored", color: "FF0000" }),
new TextRun({ text: ", Courier", font: "Courier New" }),
new TextRun({ text: ", Super", superScript: true }),
new TextRun({ text: ", Sub", subScript: true }),
],
});
Numbered and Bulleted Lists
Use Numbering to define list styles, then reference them in paragraphs:
import { Numbering, Paragraph, TextRun, Document } from "@office-open/docx";
const doc = new Document({
numbering: {
config: [
{
reference: "my-numbering",
levels: [
{ level: 0, format: "decimal", text: "%1.", alignment: "start" },
{ level: 1, format: "lowerLetter", text: "%2)", alignment: "start" },
],
},
],
},
sections: [
{
children: [
new Paragraph({
numbering: { reference: "my-numbering", level: 0 },
children: [new TextRun("First item")],
}),
new Paragraph({
numbering: { reference: "my-numbering", level: 0 },
children: [new TextRun("Second item")],
}),
new Paragraph({
numbering: { reference: "my-numbering", level: 1 },
children: [new TextRun("Nested sub-item")],
}),
],
},
],
});
For bullet lists, use format: "bullet" in the level config.
Tab Stops
import { Paragraph, TextRun, Tab, TabStopType, TabStopPosition } from "@office-open/docx";
new Paragraph({
tabStops: [{ type: TabStopType.LEFT, position: TabStopPosition.MAX }],
children: [new TextRun("Name"), new Tab(), new TextRun("John Doe")],
});
Page Break
Insert a page break between paragraphs:
new Paragraph({
pageBreakBefore: true,
children: [new TextRun("Start of new page")],
});
Paragraph Options Reference
| Option | Type | Description |
|---|---|---|
alignment | "left" | "center" | "right" | "distribute" | Horizontal alignment |
spacing.before | number | Space before in TWIPs |
spacing.after | number | Space after in TWIPs |
spacing.line | number | Line spacing in 1/240ths of a line |
indent.left | number | Left indent in TWIPs |
indent.right | number | Right indent in TWIPs |
indent.firstLine | number | First line indent in TWIPs |
indent.hanging | number | Hanging indent in TWIPs |
heading | string | Heading level ("Heading1" — "Heading6") |
border | object | Paragraph borders |
pageBreakBefore | boolean | Force page break before this paragraph |
numbering | object | List reference and level |
tabStops | array | Tab stop definitions |
style | string | Named style reference |
TextRun Options Reference
| Option | Type | Description |
|---|---|---|
text | string | Text content |
bold | boolean | Bold formatting |
italics | boolean | Italic formatting |
underline | object | Underline style and type |
strike | boolean | Strikethrough |
doubleStrike | boolean | Double strikethrough |
subScript | boolean | Subscript |
superScript | boolean | Superscript |
size | number | Font size in half-points (24 = 12pt) |
color | string | Hex color without # |
font | string | Font family name |
highlight | string | Highlight color |
shading | object | Background shading |
style | string | Named character style reference |
break | number | Number of line breaks |
characterSpacing | number | Character spacing in TWIPs |