Document setup and text
Metadata, page size, columns, themes, styles, headers and footers, paragraphs, rich text and lists.
Document Setup#
Setup methods configure global document properties and should be called before adding content.
Document Metadata#
| Method | Parameters | Description |
|---|---|---|
setTitle() | title : String | Sets the document title (written to core.xml) |
setAuthor() | author : String | Sets the document author (written to core.xml) |
setDefaultFont() | fontName, fontSize | Body font and size in pt. Defaults: Calibri, 11pt |
addCustomProperty() | name, value | Add a custom document property (written to custom.xml) |
Page Size and Orientation#
| Method | Parameters | Description |
|---|---|---|
setPageSize() | size : String | "letter", "a4", "legal", "a3", "b5". Default: letter |
setCustomPageSize() | widthCm, heightCm | Exact page dimensions in centimetres |
setOrientation() | orientation | "portrait" or "landscape" |
setMargins() | top, bottom, left, right | Page margins in centimetres |
setMarginsInches() | top, bottom, left, right | Page margins in inches |
setNarrowMargins() | — | 0.5" margins on all four sides |
setWideMargins() | — | 1.5" margins on all four sides |
doc.setPageSize("a4")
doc.setMargins(2.54, 2.54, 2.54, 2.54) # 1 inch all sidesMulti-Column Layouts#
| Method | Parameters | Description |
|---|---|---|
setColumns() | numColumns, spaceCm | Set column count and gap in cm |
setTwoColumns() | — | Two equal columns, 0.5 cm gap |
setThreeColumns() | — | Three equal columns, 0.5 cm gap |
addColumnBreak() | — | Insert a column break at the current position |
addSectionBreak() | breakType, options | Insert a section break with optional layout |
addLandscapeStart() | — | Begin a landscape section |
addLandscapeEnd() | — | End the landscape section |
addSectionBreak() break types: "continuous", "nextPage", "evenPage", "oddPage". The second argument is an options list — pass [] when no options are needed:
| Option | Type | Description |
|---|---|---|
:numColumns | Number | Column count for the new section (2 or more enables multi-column) |
:columnSpaceCm | Number | Gap between columns in cm |
# Two-column newsletter — begin on same page
doc.addSectionBreak("continuous", [:numColumns=2, :columnSpaceCm=0.6])
doc.addParagraph("Left column content...", [])
doc.addColumnBreak()
doc.addParagraph("Right column content...", [])
# Return to single-column on the next page
doc.addSectionBreak("nextPage", [])
# Landscape page within a portrait document
doc.addLandscapeStart()
doc.addTable(wideData, options)
doc.addLandscapeEnd()Document Themes#
setTheme() applies a colour palette to all heading levels and writes word/theme/theme1.xml. Call it before save().
doc.setTheme("Blue")| Theme | Accent 1 (H1/H2) | Accent 2 (H3/H4) |
|---|---|---|
Office | #4472C4 Blue | #ED7D31 Orange |
Blue | #1E6BB5 Deep Blue | #2E75B6 Mid Blue |
Dark | #88C0D0 Ice Blue | #81A1C1 Steel Blue |
Green | #375623 Forest | #70AD47 Lime |
Red | #C00000 Dark Red | #FF0000 Red |
Purple | #7030A0 Purple | #9B59B6 Violet |
Teal | #00796B Teal | #009688 Medium Teal |
Warm | #E65100 Deep Orange | #F57C00 Orange |
Custom Paragraph Styles#
defineStyle() registers a named paragraph style. Use the style name via :style in addParagraph().
doc.defineStyle("CalloutBox", [
:basedOn = "Normal",
:bold = true,
:size = 11,
:color = "1E6BB5",
:bgColor = "DEEAF1",
:align = "center",
:spaceBefore = 120,
:spaceAfter = 120
])
doc.addParagraph("This uses the custom style.", [:style = "CalloutBox"])defineStyle option | Type | Description |
|---|---|---|
:basedOn | String | Parent style ID, e.g. "Normal", "Heading1" |
:bold | true/false | Bold text |
:italic | true/false | Italic text |
:underline | true/false | Underlined text |
:font | String | Font family name |
:size | Number | Font size in points |
:color | String | Text colour hex without # |
:align | String | "left", "center", "right", "both" |
:bgColor | String | Paragraph background fill colour hex |
:spaceBefore | Number | Space before in twips (20 twips = 1 pt) |
:spaceAfter | Number | Space after in twips |
:lineSpacing | Number | Line spacing multiplier: 1.5, 2.0, etc. |
:indent | Number | Left indent in twips |
:keepNext | true/false | Keep with the next paragraph |
:keepLines | true/false | Prevent splitting across pages |
Headers, Footers, and Page Numbers#
| Method | Parameters | Description |
|---|---|---|
setHeader() | text | Default header text (centred) |
setFooter() | text | Default footer text |
setHeaderFooter() | headerText, footerText | Set both in one call |
showPageNumbers() | align | Add page numbers: "left", "center", "right" |
setFirstPageDifferent() | bEnable | Enable a different header/footer on page 1 |
setFirstPageHeader() | text | Header text for the first page only |
setFirstPageFooter() | text | Footer text for the first page only |
setEvenAndOddHeaders() | bEnable | Enable separate headers for even/odd pages |
setEvenPageHeader() | text | Header text for even-numbered (left) pages |
setEvenPageFooter() | text | Footer text for even-numbered pages |
# Standard header with page numbers
doc.setHeader("Annual Report 2026")
doc.showPageNumbers("right")
# Cover page — no header, subsequent pages with header
doc.setFirstPageDifferent(true)
doc.setFirstPageHeader("") # blank on cover
doc.setHeader("Chapter 1 — Introduction")
# Book-style even/odd headers
doc.setEvenAndOddHeaders(true)
doc.setHeader("Ring Language Study") # odd pages (right side)
doc.setEvenPageHeader("Mahmoud, 2026") # even pages (left side)Text and Paragraphs#
addParagraph()#
The primary content method. Pass [] or NULL as the second argument to use all defaults.
doc.addParagraph("Plain text paragraph.", [])
doc.addParagraph("Bold red heading", [:bold=true, :color="C00000", :size=14])
doc.addParagraph("Centred note", [:align="center", :italic=true])
doc.addParagraph("Custom style", [:style="CalloutBox"])| Option | Type | Description |
|---|---|---|
:bold | true/false | Bold text |
:italic | true/false | Italic text |
:underline | true/false | Underlined text |
:size | Number | Font size in points |
:font | String | Font family name |
:color | String | Text colour hex, e.g. "C00000" |
:charStyle | String | Named character style, e.g. "Strong", "Emphasis" |
:lang | String | BCP 47 language tag, e.g. "ar-SA", "fr-FR", "de-DE" |
:align | String | "left", "center", "right", "both" |
:style | String | Named paragraph style |
:bgColor | String | Paragraph background colour hex |
:spaceBefore | Number | Space before in twips (20 twips = 1 pt) |
:spaceAfter | Number | Space after in twips |
:lineSpacing | Number | Line spacing multiplier, e.g. 1.5 |
:indent | Number | Left indent in twips |
:keepNext | true/false | Keep this paragraph on the same page as the next |
:keepLines | true/false | Prevent the paragraph from splitting across pages |
:suppressLineNumbers | true/false | Exclude from line numbering |
:contextualSpacing | true/false | Suppress spacing between same-style paragraphs |
:widowControl | false | Set false to disable widow/orphan protection (Word default is on) |
:noHyphenate | true | Suppress automatic hyphenation on this paragraph |
Headings and Titles#
doc.addHeading("Chapter 1 — Introduction", 1) # Heading 1
doc.addHeading("1.1 Background", 2) # Heading 2
doc.addHeading("Sub-section detail", 3) # Heading 3 (levels 1–6)
doc.addTitle("Document Title") # Title style
doc.addSubtitle("A subtitle line") # Subtitle styleaddRichParagraph() — Mixed Formatting#
Build a paragraph from an explicit list of [text, options] run pairs. Each run can carry its own bold, italic, colour, size, font, language, and character style.
doc.addRichParagraph([
["Status: ", []],
["Passed", [:bold=true, :color="375623"]],
[" — Score: ", []],
["98/100", [:bold=true, :color="1E6BB5", :size=14]]
], [:align="center"])
# Language-tagged runs (Arabic + English in one paragraph)
doc.addRichParagraph([
["مرحبا", [:font="Arial", :lang="ar-SA"]],
[" — Hello", [:lang="en-US"]]
], [])
# Character styles
doc.addRichParagraph([
["See ", []],
["ring-lang.github.io", [:charStyle="Hyperlink"]]
], [])Run-level option keys: :text, :bold, :italic, :underline, :color, :size, :font, :bgColor, :charStyle, :lang.
Shorthand Methods#
| Method | Description |
|---|---|
bold(text) | Bold paragraph |
italic(text) | Italic paragraph |
underline(text) | Underlined paragraph |
colored(text, color) | Coloured text (hex colour) |
styled(text, font, size, color) | Single-call full style |
centered(text) | Centre-aligned paragraph |
rightAligned(text) | Right-aligned paragraph |
justified(text) | Justified paragraph |
addBlockQuote(text) | Indented block-quote paragraph |
addCaption(text) | Small centred italic caption |
addAbstract(text) | Indented abstract block with bold "Abstract" heading |
addKeywords(text) | Keywords line with bold "Keywords:" prefix |
addEmptyParagraph() | One blank line |
addHorizontalLine() | Full-width horizontal rule |
addPageBreak() | Hard page break |
addLineBreak() | Soft line break within a paragraph |
Shaded and Bordered Paragraphs#
# Solid background colour
doc.addShadedParagraph("Important note", "FFF3CD", [:bold=true])
# Full border box with optional background
doc.addBorderedParagraph("Critical warning", [
:borderStyle = "single",
:borderColor = "C00000",
:bgColor = "FDECEA",
:spaceBefore = 80,
:spaceAfter = 80,
:indent = 240
])Widow/Orphan Control and Auto-Hyphenation#
Word protects against widows (last line stranded on the next page) and orphans (first line stranded at the bottom) by default. You can also suppress automatic hyphenation per paragraph — useful for technical terms, URLs, and headings.
# Disable widow/orphan protection for a data-dense paragraph
doc.addParagraph(denseText, [:align="both", :widowControl=false])
# Suppress auto-hyphenation (useful for URLs, product codes, headings)
doc.addParagraph(longTechnicalTerm, [:noHyphenate=true])
# Both together — dense justified paragraph that must not be hyphenated
doc.addParagraph(body, [:align="both", :widowControl=false, :noHyphenate=true])Lists#
DOCXLib generates proper Word OOXML list numbering — not manually inserted bullet characters.
# Simple bullet list
doc.addBulletList(["Ring language", "Python", "JavaScript"])
# Numbered list
doc.addNumberedList(["Install Ring", "Load docxlib.ring", "Call save()"])
# Nested bullet list — inner list items are sub-lists
doc.addNestedBulletList([
"Programming Languages",
["Ring", "Python", "Java"],
"Databases",
["SQLite", "MySQL"]
])
# Nested numbered list
doc.addNestedNumberedList([
"Phase 1 — Design",
["Requirements", "Architecture"],
"Phase 2 — Development"
])