10 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...")],
},
],
});
Document Grid
The document grid sets a fixed number of lines per page and characters per line, as East Asian documents often do. It's Word's Layout → Page Setup → Document Grid, and is set with grid in a section's properties:
type:DocumentGridType.LINESfixes only the line pitch.LINES_AND_CHARSfixes the line pitch and the character pitch.SNAP_TO_CHARSsnaps characters to the grid.DEFAULThas no grid.linePitch: the height of each line, in twips (twentieths of a point).charSpace: how much wider each character is than the Normal font size, in 4096ths of a point. For a number of characters per line, use(text width in points / characters per line - Normal font size in points) * 4096.
This example has 28 characters per line: the text of an A4 page with the default margins is 451.3pt wide, so with 10.5pt text, charSpace is (451.3 / 28 - 10.5) * 4096, which is about 23010.
import { Document, DocumentGridType, Paragraph } from "docx";
const doc = new Document({
styles: {
default: {
// 10.5pt Normal text
document: { run: { size: 21 } },
},
},
sections: [
{
properties: {
grid: {
type: DocumentGridType.LINES_AND_CHARS,
linePitch: 312,
charSpace: 23010,
},
},
children: [
new Paragraph("这一段的每一行有二十八个字。文档网格让每个汉字占据相同的宽度,所以每行的字数是固定的,就像稿纸一样。"),
],
},
],
});
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 |
| grid | IDocGridAttributesProperties |
Lines per page and characters per line, see Document Grid |
| footnoteProperties | IFootnotePropertiesOptions |
Where footnotes go and how they're numbered, see Footnotes |
| endnoteProperties | IEndnotePropertiesOptions |
Where endnotes go and how they're numbered, with the same options as footnoteProperties and EndnotePosition |
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