Advanced layout and formatting features including numbering systems, tab stops, page layout, and positioning controls for complex document structures.
Comprehensive numbering format options for lists, outlines, and structured content.
/**
* Number format types for lists and numbering
*/
const NumberFormat = {
/** Arabic numerals (1, 2, 3, ...) */
DECIMAL: "decimal",
/** Uppercase Roman numerals (I, II, III, ...) */
UPPER_ROMAN: "upperRoman",
/** Lowercase Roman numerals (i, ii, iii, ...) */
LOWER_ROMAN: "lowerRoman",
/** Uppercase letters (A, B, C, ...) */
UPPER_LETTER: "upperLetter",
/** Lowercase letters (a, b, c, ...) */
LOWER_LETTER: "lowerLetter",
/** Ordinal numbers (1st, 2nd, 3rd, ...) */
ORDINAL: "ordinal",
/** Cardinal text (One, Two, Three, ...) */
CARDINAL_TEXT: "cardinalText",
/** Ordinal text (First, Second, Third, ...) */
ORDINAL_TEXT: "ordinalText",
/** Japanese counting */
JAPANESE_COUNTING: "japaneseCounting",
/** Chinese counting systems */
CHINESE_COUNTING: "chineseCounting",
/** Korean digital numbering */
KOREAN_DIGITAL: "koreanDigital",
/** Thai letter numbering */
THAI_LETTERS: "thaiLetters",
/** Arabic alphabetic numbering */
ARABIC_ALPHA: "arabicAlpha",
/** Hebrew numbering system 1 */
HEBREW_1: "hebrew1",
/** Hebrew numbering system 2 */
HEBREW_2: "hebrew2",
/** Bullet points */
BULLET: "bullet",
/** No numbering */
NONE: "none"
} as const;
type NumberFormat = typeof NumberFormat[keyof typeof NumberFormat];Usage Example:
import { Document, Paragraph, NumberFormat } from "docx";
const doc = new Document({
sections: [{
children: [
new Paragraph({
text: "First item",
numbering: {
reference: "my-numbering",
level: 0,
format: NumberFormat.DECIMAL,
},
}),
new Paragraph({
text: "Second item",
numbering: {
reference: "my-numbering",
level: 0,
format: NumberFormat.DECIMAL,
},
}),
],
}],
numbering: {
config: [{
reference: "my-numbering",
levels: [{
level: 0,
format: NumberFormat.DECIMAL,
text: "%1.",
alignment: AlignmentType.START,
}],
}],
},
});Tab stop types and leader formatting for precise text alignment.
/**
* Tab stop position types
*/
const TabStopType = {
/** Left-aligned tab stop */
LEFT: "left",
/** Right-aligned tab stop */
RIGHT: "right",
/** Center-aligned tab stop */
CENTER: "center",
/** Decimal-aligned tab stop */
DECIMAL: "decimal",
/** Bar tab (vertical line) */
BAR: "bar",
/** Clear existing tab stop */
CLEAR: "clear",
/** End tab stop */
END: "end",
/** Number tab stop */
NUM: "num",
/** Start tab stop */
START: "start"
} as const;
type TabStopType = typeof TabStopType[keyof typeof TabStopType];
/**
* Tab leader styles for filling space before tab
*/
const LeaderType = {
/** Dotted leader (.....) */
DOT: "dot",
/** Hyphen leader (-----) */
HYPHEN: "hyphen",
/** Middle dot leader (·····) */
MIDDLE_DOT: "middleDot",
/** No leader */
NONE: "none",
/** Underscore leader (_____) */
UNDERSCORE: "underscore"
} as const;
type LeaderType = typeof LeaderType[keyof typeof LeaderType];
/**
* Tab stop configuration interface
*/
interface ITabStopOptions {
/** Tab stop type */
readonly type: TabStopType;
/** Position in twips from left margin */
readonly position: number;
/** Leader character style */
readonly leader?: LeaderType;
}Usage Example:
import { Paragraph, TabStopType, LeaderType } from "docx";
const paragraphWithTabs = new Paragraph({
children: [
new TextRun("Item Name"),
new TextRun("\t"), // Tab character
new TextRun("$99.99"),
],
tabStops: [
{
type: TabStopType.LEFT,
position: convertInchesToTwip(2), // 2 inches from left
},
{
type: TabStopType.RIGHT,
position: convertInchesToTwip(6), // 6 inches from left
leader: LeaderType.DOT, // Dotted leader
},
],
});Page orientation, breaks, and layout configuration options.
/**
* Page orientation options
*/
const PageOrientation = {
/** Portrait orientation (height > width) */
PORTRAIT: "portrait",
/** Landscape orientation (width > height) */
LANDSCAPE: "landscape"
} as const;
type PageOrientation = typeof PageOrientation[keyof typeof PageOrientation];
/**
* Page break class for forcing page breaks
*/
class PageBreak {
constructor();
}
/**
* Column break class for forcing column breaks
*/
class ColumnBreak {
constructor();
}
/**
* Page break before paragraph option
*/
interface IPageBreakOptions {
/** Force page break before this element */
readonly pageBreakBefore?: boolean;
}Usage Example:
import { Document, Paragraph, PageBreak, ColumnBreak, PageOrientation } from "docx";
const doc = new Document({
sections: [{
properties: {
page: {
size: {
orientation: PageOrientation.LANDSCAPE,
},
},
},
children: [
new Paragraph("Content on page 1"),
new Paragraph({
children: [new PageBreak()],
}),
new Paragraph("Content on page 2"),
new Paragraph({
text: "This paragraph starts a new page",
pageBreakBefore: true,
}),
],
}],
});Advanced positioning options for floating images and text wrapping controls.
/**
* Horizontal position reference points
*/
const HorizontalPositionRelativeFrom = {
/** Relative to character */
CHARACTER: "character",
/** Relative to column */
COLUMN: "column",
/** Relative to inside margin */
INSIDE_MARGIN: "insideMargin",
/** Relative to left margin */
LEFT_MARGIN: "leftMargin",
/** Relative to page margin */
MARGIN: "margin",
/** Relative to outside margin */
OUTSIDE_MARGIN: "outsideMargin",
/** Relative to page */
PAGE: "page",
/** Relative to right margin */
RIGHT_MARGIN: "rightMargin"
} as const;
type HorizontalPositionRelativeFrom = typeof HorizontalPositionRelativeFrom[keyof typeof HorizontalPositionRelativeFrom];
/**
* Vertical position reference points
*/
const VerticalPositionRelativeFrom = {
/** Relative to bottom margin */
BOTTOM_MARGIN: "bottomMargin",
/** Relative to inside margin */
INSIDE_MARGIN: "insideMargin",
/** Relative to text line */
LINE: "line",
/** Relative to page margin */
MARGIN: "margin",
/** Relative to outside margin */
OUTSIDE_MARGIN: "outsideMargin",
/** Relative to page */
PAGE: "page",
/** Relative to paragraph */
PARAGRAPH: "paragraph",
/** Relative to top margin */
TOP_MARGIN: "topMargin"
} as const;
type VerticalPositionRelativeFrom = typeof VerticalPositionRelativeFrom[keyof typeof VerticalPositionRelativeFrom];
/**
* Text wrapping behavior around images
*/
const TextWrappingType = {
/** No text wrapping */
NONE: 0,
/** Square wrapping around image bounds */
SQUARE: 1,
/** Tight wrapping around image contours */
TIGHT: 2,
/** Text above and below only */
TOP_AND_BOTTOM: 3
} as const;
type TextWrappingType = typeof TextWrappingType[keyof typeof TextWrappingType];Usage Example:
import {
ImageRun,
Paragraph,
HorizontalPositionRelativeFrom,
VerticalPositionRelativeFrom,
TextWrappingType
} from "docx";
const floatingImage = new Paragraph({
children: [
new ImageRun({
data: imageBuffer,
transformation: {
width: 200,
height: 150,
},
floating: {
horizontalPosition: {
relative: HorizontalPositionRelativeFrom.PAGE,
align: "center",
},
verticalPosition: {
relative: VerticalPositionRelativeFrom.PAGE,
align: "top",
},
wrap: {
type: TextWrappingType.SQUARE,
side: "bothSides",
},
margins: {
top: 10,
bottom: 10,
left: 10,
right: 10,
},
},
}),
],
});Vertical alignment options for table cells and sections.
/**
* Vertical alignment for table cells
*/
const VerticalAlignTable = {
/** Top alignment */
TOP: "top",
/** Center alignment */
CENTER: "center",
/** Bottom alignment */
BOTTOM: "bottom"
} as const;
type VerticalAlignTable = typeof VerticalAlignTable[keyof typeof VerticalAlignTable];
/**
* Vertical alignment for sections (includes additional options)
*/
const VerticalAlignSection = {
/** Top alignment */
TOP: "top",
/** Center alignment */
CENTER: "center",
/** Bottom alignment */
BOTTOM: "bottom",
/** Justified alignment */
BOTH: "both"
} as const;
type VerticalAlignSection = typeof VerticalAlignSection[keyof typeof VerticalAlignSection];Usage Example:
import { Table, TableCell, VerticalAlignTable } from "docx";
const cell = new TableCell({
children: [new Paragraph("Centered content")],
verticalAlign: VerticalAlignTable.CENTER,
margins: {
top: 100,
bottom: 100,
},
});Special content types for footnotes and emphasis marks.
/**
* Footnote types
*/
const FootnoteType = {
/** Footnote separator */
SEPERATOR: "separator",
/** Continuation separator for multi-page footnotes */
CONTINUATION_SEPERATOR: "continuationSeparator"
} as const;
type FootnoteType = typeof FootnoteType[keyof typeof FootnoteType];
/**
* Emphasis mark types for text decoration
*/
const EmphasisMarkType = {
/** Dot emphasis mark */
DOT: "dot"
} as const;
type EmphasisMarkType = typeof EmphasisMarkType[keyof typeof EmphasisMarkType];Usage Example:
import { TextRun, EmphasisMarkType } from "docx";
const emphasizedRun = new TextRun({
text: "Important text",
emphasisMark: {
type: EmphasisMarkType.DOT,
},
});