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 text runs with individual formatting.
Paragraph
{
"sections": [
{
"children": [
{ "paragraph": { "children": ["Plain text paragraph."] } },
{ "paragraph": { "heading": "Heading1", "children": ["Chapter Title"] } }
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{ paragraph: { children: ["Plain text paragraph."] } },
{ paragraph: { heading: "Heading1", children: ["Chapter Title"] } },
],
},
],
});
Alignment and Spacing
{
"sections": [
{
"children": [
{
"paragraph": {
"alignment": "center",
"spacing": { "before": 200, "after": 200, "line": 360 },
"indent": { "left": 720 },
"children": ["Centered paragraph with spacing and indent."]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
alignment: "center",
spacing: { before: 200, after: 200, line: 360 },
indent: { left: 720 },
children: ["Centered paragraph with spacing and indent."],
},
},
],
},
],
});
Borders
{
"sections": [
{
"children": [
{
"paragraph": {
"border": { "bottom": { "style": "single", "size": 6, "color": "999999" } },
"children": ["Paragraph with a bottom border."]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
border: {
bottom: { style: "single", size: 6, color: "999999" },
},
children: ["Paragraph with a bottom border."],
},
},
],
},
],
});
TextRun
Each text run can have its own formatting:
{
"sections": [
{
"children": [
{
"paragraph": {
"children": [
{ "text": "Bold", "bold": true },
{ "text": ", Italic", "italic": true },
{ "text": ", Underline", "underline": { "type": "single" } },
{ "text": ", Strikethrough", "strike": true },
{ "text": ", 24pt", "size": 24 },
{ "text": ", Colored", "color": "FF0000" },
{ "text": ", Courier", "font": "Courier New" },
{ "text": ", Super", "superScript": true },
{ "text": ", Sub", "subScript": true }
]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
children: [
{ text: "Bold", bold: true },
{ text: ", Italic", italic: true },
{ text: ", Underline", underline: { type: "single" } },
{ text: ", Strikethrough", strike: true },
{ text: ", 24pt", size: 24 },
{ text: ", Colored", color: "FF0000" },
{ text: ", Courier", font: "Courier New" },
{ text: ", Super", superScript: true },
{ text: ", Sub", subScript: true },
],
},
},
],
},
],
});
Numbered and Bulleted Lists
Use numbering to define list styles, then reference them in paragraphs:
{
"numbering": {
"config": [
{
"reference": "my-numbering",
"levels": [
{ "level": 0, "format": "decimal", "text": "%1.", "alignment": "start" },
{ "level": 1, "format": "lowerLetter", "text": "%2)", "alignment": "start" }
]
}
]
},
"sections": [
{
"children": [
{
"paragraph": {
"numbering": { "reference": "my-numbering", "level": 0 },
"children": ["First item"]
}
},
{
"paragraph": {
"numbering": { "reference": "my-numbering", "level": 0 },
"children": ["Second item"]
}
},
{
"paragraph": {
"numbering": { "reference": "my-numbering", "level": 1 },
"children": ["Nested sub-item"]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
numbering: {
config: [
{
reference: "my-numbering",
levels: [
{ level: 0, format: "decimal", text: "%1.", alignment: "start" },
{ level: 1, format: "lowerLetter", text: "%2)", alignment: "start" },
],
},
],
},
sections: [
{
children: [
{
paragraph: {
numbering: { reference: "my-numbering", level: 0 },
children: ["First item"],
},
},
{
paragraph: {
numbering: { reference: "my-numbering", level: 0 },
children: ["Second item"],
},
},
{
paragraph: {
numbering: { reference: "my-numbering", level: 1 },
children: ["Nested sub-item"],
},
},
],
},
],
});
For bullet lists, use format: "bullet" in the level config.
Tab Stops
{
"sections": [
{
"children": [
{
"paragraph": {
"tabStops": [{ "type": "left", "position": 10000 }],
"children": ["Name", "TAB", "John Doe"]
}
}
]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
tabStops: [{ type: "left", position: 10000 }],
children: ["Name", "TAB", "John Doe"],
},
},
],
},
],
});
Page Break
Insert a page break between paragraphs:
{
"sections": [
{
"children": [{ "paragraph": { "pageBreakBefore": true, "children": ["Start of new page"] } }]
}
]
}
import { generateDocument } from "@office-open/docx";
await generateDocument({
sections: [
{
children: [
{
paragraph: {
pageBreakBefore: true,
children: ["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 |
paraId | string | Unique paragraph id (8-digit hex, w14:paraId) |
textId | string | Paragraph text id (w14:textId) |
rsid | string | Revision id (w:rsidR, hex); related: defaultRunRsid, propertiesRsid, runPropertiesRsid, deletionRsid |
TextRun Options Reference
| Option | Type | Description |
|---|---|---|
text | string | Text content |
bold | boolean | Bold formatting |
italic | 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 points (12 = 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 |
rsid | string | Revision id (w:rsidR, hex); related: runPropertiesRsid, deletionRsid |