mirror of
https://github.com/dolanmiu/docx.git
synced 2026-11-30 16:47:55 +01:00
* 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>
6.1 KiB
6.1 KiB
Columns
!> Columns require an understanding of Sections.
Create multi-column layouts like newspapers or newsletters by configuring columns in your section properties.
Basic Columns
Create equal-width columns:
import { Document, Paragraph } from "docx";
const doc = new Document({
sections: [
{
properties: {
column: {
count: 2,
space: 708, // Space between columns (~0.5 inches in twips; 1440 twips = 1 inch)
},
},
children: [new Paragraph("This text will flow across two columns...")],
},
],
});
Column Options
| Property | Type | Notes | Description |
|---|---|---|---|
| count | number |
Required | Number of columns |
| space | number |
Optional | Space between columns (twips) |
| separate | boolean |
Optional | Draw a line between columns |
| equalWidth | boolean |
Optional | Whether columns have equal width |
| children | Column[] |
Optional | Individual column definitions |
Column Separator
Add a vertical line between columns:
properties: {
column: {
count: 2,
space: 708,
separate: true, // Adds a line between columns
},
}
Different Width Columns
Create columns with custom widths:
import { Column, Document, Paragraph } from "docx";
const doc = new Document({
sections: [
{
properties: {
column: {
count: 2,
space: 720,
equalWidth: false,
children: [
new Column({ width: 2880, space: 720 }), // Narrow column
new Column({ width: 5760 }), // Wide column
],
},
},
children: [new Paragraph("Content flows through unequal columns...")],
},
],
});
Column Class Options
| Property | Type | Notes | Description |
|---|---|---|---|
| width | number |
Required | Column width (twips) |
| space | number |
Optional | Space after this column |
Column Breaks
Force content to the next column using ColumnBreak:
import { ColumnBreak, Document, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
properties: {
column: {
count: 2,
space: 708,
},
},
children: [
new Paragraph({
children: [new TextRun("This text is in column 1."), new ColumnBreak(), new TextRun("This text is in column 2.")],
}),
],
},
],
});
Three Columns
properties: {
column: {
count: 3,
space: 708,
},
}
Mixing Column Layouts
Use multiple sections to mix column layouts:
import { Document, Paragraph, SectionType } from "docx";
const doc = new Document({
sections: [
// Single column section
{
children: [new Paragraph("This is a full-width introduction paragraph.")],
},
// Two column section
{
properties: {
type: SectionType.CONTINUOUS, // Don't start new page
column: {
count: 2,
space: 708,
},
},
children: [new Paragraph("This content flows in two columns...")],
},
// Back to single column
{
properties: {
type: SectionType.CONTINUOUS,
},
children: [new Paragraph("This is a full-width conclusion.")],
},
],
});
Complete Example
Newsletter-style layout with unequal columns:
import { Column, ColumnBreak, Document, HeadingLevel, Paragraph, TextRun } from "docx";
const doc = new Document({
sections: [
{
properties: {
column: {
count: 2,
space: 720, // 0.5 inches between columns (1440 twips = 1 inch)
separate: true,
equalWidth: false,
children: [
new Column({ width: 3600, space: 720 }), // Narrow column (~2.5 inches)
new Column({ width: 5040 }), // Wide column (~3.5 inches)
],
},
},
children: [
new Paragraph({
text: "Newsletter Title",
heading: HeadingLevel.HEADING_1,
}),
new Paragraph({
children: [
new TextRun("First column content goes here. "),
new TextRun("It will flow naturally within the column width. "),
new TextRun("Add more content as needed."),
],
}),
new Paragraph({
children: [new ColumnBreak()],
}),
new Paragraph({
children: [new TextRun("Second column content starts here. "), new TextRun("This appears in the right column.")],
}),
],
},
],
});
Demos
Multiple Columns
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/multiple-columns.ts
Column Break
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/column-break.ts
Different Width Columns
Source: https://github.com/dolanmiu/docx/blob/master/demo/page-layout/different-width-columns.ts