* 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>
7.7 KiB
Sections
Every document is made up of one or more sections
A section is a grouping of paragraphs that have a specific set of properties used to define the pages on which the text will appear. Properties include page size, page numbers, page orientation, headers, borders and margins.
For example, you could have one section which is portrait with a header and footer, and another section in landscape with no footer, and a header showing the current page number.
Basic Example
This creates a simple section in a document with one paragraph inside:
import { Document, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
children: [
new Paragraph({
children: [new TextRun("Hello World")],
}),
],
},
],
});
Multiple Sections
Create documents with multiple sections for different layouts:
import { Document, PageOrientation, Paragraph } from "docx";
const doc = new Document({
sections: [
{
// First section - portrait
children: [new Paragraph("This is the first section in portrait.")],
},
{
// Second section - landscape
properties: {
page: {
size: {
orientation: PageOrientation.LANDSCAPE,
},
},
},
children: [new Paragraph("This section is in landscape orientation.")],
},
],
});
Section Properties
You can specify additional properties to the section by providing a properties attribute.
Section Type
Setting the section type determines how the contents of the section will be placed relative to the previous section:
| Type | Description |
|---|---|
CONTINUOUS |
Start on the same page as the previous section |
NEXT_PAGE |
Start on a new page (default) |
EVEN_PAGE |
Start on the next even-numbered page |
ODD_PAGE |
Start on the next odd-numbered page |
NEXT_COLUMN |
Start in the next column |
import { Document, Paragraph, SectionType } from "docx";
const doc = new Document({
sections: [
{
children: [new Paragraph("First section content")],
},
{
properties: {
type: SectionType.CONTINUOUS, // Don't start new page
},
children: [new Paragraph("This continues on the same page")],
},
],
});
When to Use Each Section Type
- CONTINUOUS: Multi-column layouts, changing headers without page breaks
- NEXT_PAGE: New chapters, distinct document parts
- EVEN_PAGE/ODD_PAGE: Books with content starting on specific sides
- NEXT_COLUMN: Newsletter-style column breaks
Common Use Cases
Different Headers Per Section
import { Document, Header, Paragraph } from "docx";
const doc = new Document({
sections: [
{
headers: {
default: new Header({
children: [new Paragraph("Chapter 1")],
}),
},
children: [new Paragraph("Chapter 1 content...")],
},
{
headers: {
default: new Header({
children: [new Paragraph("Chapter 2")],
}),
},
children: [new Paragraph("Chapter 2 content...")],
},
],
});
Portrait and Landscape Mix
import { Document, PageOrientation, Paragraph, Table, TableCell, TableRow, convertMillimetersToTwip } from "docx";
const doc = new Document({
sections: [
{
// Portrait section for text
children: [new Paragraph("Introduction text...")],
},
{
// Landscape section for wide table
properties: {
page: {
size: {
orientation: PageOrientation.LANDSCAPE,
// The A4 portrait size, which LANDSCAPE turns on its side
width: convertMillimetersToTwip(210),
height: convertMillimetersToTwip(297),
},
},
},
children: [
new Table({
// Wide table that needs landscape
rows: [
new TableRow({
children: ["January", "February", "March", "April", "May", "June", "July", "August"].map(
(month) => new TableCell({ children: [new Paragraph(month)] }),
),
}),
new TableRow({
children: ["120", "135", "150", "160", "155", "170", "180", "175"].map(
(sales) => new TableCell({ children: [new Paragraph(sales)] }),
),
}),
],
}),
],
},
],
});
Multi-Column Section
import { Document, Paragraph, SectionType } from "docx";
const doc = new Document({
sections: [
{
// Single column intro
children: [new Paragraph("Introduction paragraph...")],
},
{
// Two-column body
properties: {
type: SectionType.CONTINUOUS,
column: {
count: 2,
space: 708,
},
},
children: [
new Paragraph(
"Two-column content flows down the first column and then carries on at the top of the second, like the columns of a newspaper. The section breaks before and after it are continuous, so the columns sit on the same page as the paragraphs around them.",
),
],
},
{
// Back to single column
properties: {
type: SectionType.CONTINUOUS,
},
children: [new Paragraph("Single column conclusion...")],
},
],
});
Section Properties Reference
| Property | Type | Description |
|---|---|---|
| type | SectionType |
How section starts relative to previous |
| page | IPageSizeAttributes |
Page size, margins, borders |
| column | IColumnsAttributes |
Multi-column configuration |
| titlePage | boolean |
Different first page header/footer |
| lineNumbers | ILineNumberAttributes |
Line numbering configuration |
| verticalAlign | VerticalAlign |
Vertical alignment of content |
Related Topics
- Page Layout - Page size, margins, and borders
- Headers & Footers - Section headers and footers
- Columns - Multi-column layouts
- Line Numbers - Line numbering per section
Demos
Multiple Sections
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/multiple-sections.ts
Section Types
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/section-types.ts