Comments, forms, shapes and saving
Comments, mail merge fields, content controls, shapes, text boxes, right-to-left text and saving the document.
Comments#
| Method | Parameters | Description |
|---|---|---|
addCommentedParagraph() | text, commentText, options | Paragraph with a comment balloon attached |
addComment() | text, commentText, author | Shortcut with explicit author name |
doc.addCommentedParagraph(
"The Ring VM is stack-based and written in C.",
"Please verify against the source",
[:commentAuthor = "Mahmoud",
:commentDate = "2026-03-15T09:00:00Z"]
)
# Shortcut form:
doc.addComment("See Table 3 for benchmark data.",
"Check these numbers against the latest run.",
"Reviewer")Mail Merge Fields#
Mail merge fields create .docx templates that Word merges with data sources. Each MERGEFIELD placeholder is replaced with a real value during the merge.
# Formal letter template
doc.addMergeFieldParagraph([[:field="RecipientName"]], [:bold=true, :size=13])
doc.addMergeFieldParagraph([[:field="CompanyName"]], NULL)
doc.addMergeFieldParagraph([[:field="Address"]], NULL)
doc.addMergeFieldParagraph([[:field="City"], ", ", [:field="Country"]], NULL)
doc.addParagraph("", NULL)
doc.addMergeFieldParagraph(["Dear ", [:field="Salutation"], " ", [:field="LastName"], ","], NULL)
# Batch template for invoice fields:
doc.addMergeTemplate([
["Invoice #: ", [:field="InvoiceNo"]],
["Date: ", [:field="InvoiceDate"]],
["Client: ", [:field="ClientName"]],
["Amount Due: $", [:field="TotalAmount"]]
])Note: In addMergeFieldParagraph(), pass a plain string for literal text and [:field="Name"] for a MERGEFIELD placeholder. They can be freely mixed.
Content Controls#
Content controls embed interactive form elements in the DOCX. Users can fill in the form without enabling macros.
Checkbox#
doc.addCheckbox("I accept the Terms and Conditions", true) # pre-checked
doc.addCheckbox("Subscribe to the Ring newsletter", false) # unchecked
doc.addCheckbox("Enable two-factor authentication", true)
doc.addCheckbox("Allow anonymous usage statistics", false)Dropdown List#
doc.addDropdown("Country",
["Saudi Arabia", "Egypt", "UAE", "Kuwait", "Jordan", "Morocco", "Other"],
"Saudi Arabia" # default selection
)
doc.addDropdown("Experience Level",
["Beginner", "Intermediate", "Advanced", "Expert"],
"Intermediate"
)Text Input#
doc.addTextInput("Full Name", "", "Enter your full name")
doc.addTextInput("Email", "", "name@example.com")
doc.addTextInput("Organisation", "", "Company or university")
doc.addTextInput("Comments", "Pre-filled default", NULL)Drawing Shapes#
Inline DrawingML shapes embedded in the document flow. All shapes support fill colour, border, and an optional text label.
Shape Methods#
| Method | Shape type | Notes |
|---|---|---|
addRect(options) | Rectangle | Use :rounded=true for rounded corners |
addEllipse(options) | Ellipse / circle | Equal width and height for a circle |
addDiamond(options) | Diamond / rhombus | Useful for flowchart decision nodes |
addShape("triangle", options) | Right triangle | |
addLine(options) | Horizontal rule | Thin coloured rect; use :height for thickness |
addShape(type, options) | Any of the above | Generic form |
Shape Options#
| Option | Type | Description |
|---|---|---|
:width | Number | Width in cm. Default: 5 |
:height | Number | Height in cm. Default: 3 (line: 0.1) |
:fillColor | String | Fill colour hex. Default: 4472C4 |
:lineColor | String | Border colour hex. Default: 2E4E7E |
:lineWidth | Number | Border width in pt. Default: 1 |
:noFill | true/false | Transparent shape (no fill) |
:noBorder | true/false | No border |
:rounded | true/false | Rounded corners (rect only) |
:text | String | Label text inside the shape |
:textColor | String | Text colour hex. Default: FFFFFF |
:textBold | true/false | Bold text label. Default: true |
:textSize | Number | Text size in pt. Default: 11 |
:align | String | Outer paragraph alignment: "left", "center", "right" |
# Solid rectangle with white text
doc.addRect([:width=8, :height=1.8, :fillColor="1E6BB5",
:text="Section Header", :textColor="FFFFFF"])
# Rounded border-only box
doc.addRect([:width=6, :height=1.4, :noFill=true,
:lineColor="C00000", :lineWidth=2,
:text="Border Only", :textColor="C00000", :rounded=true])
# Thin horizontal rule
doc.addLine([:width=14, :height=0.08, :fillColor="4472C4", :noBorder=true])
# Circle with text
doc.addEllipse([:width=3, :height=3, :fillColor="7030A0",
:text="75%", :textSize=18])Text Boxes#
addTextBox() inserts a floating VML text box positioned absolutely relative to the page.
doc.addTextBox("This content floats beside the main body text.", [
:width = 6, # cm
:height = 4, # cm
:top = 2, # cm from top of page
:left = 10.5, # cm from left of page
:bgColor = "F0F4FF",
:borderColor = "1E6BB5",
:bold = true,
:fontSize = 11,
:align = "left"
])RTL Support#
| Method | Parameters | Description |
|---|---|---|
setDocumentRTL() | bEnable | Enable document-wide RTL — flips paragraph alignment and list indents |
addRTLParagraph() | text, options | Single RTL paragraph in an otherwise LTR document |
# Full RTL document (Arabic)
doc.setDocumentRTL(true)
doc.addHeading("مرحباً بكم في DOCXLib", 1)
doc.addParagraph("هذا مستند منشأ باستخدام لغة Ring.", NULL)
# Single RTL paragraph inside an LTR document
doc.addRTLParagraph("النص العربي هنا", NULL)Saving the Document#
if doc.save("output.docx")
? "Document saved successfully."
else
? "Error: save() failed."
oksave() returns true on success. It automatically:
- Creates a temporary working directory
- Writes all XML parts:
document.xml,styles.xml,settings.xml,numbering.xml,fontTable.xml,footnotes.xml,endnotes.xml,comments.xml, header/footer files, andtheme/theme1.xml(when a theme is set) - Copies all referenced image files into
word/media/ - Assembles
[Content_Types].xmland all.relsrelationship files - Creates the ZIP archive (DOCX format) using Ring's built-in zip functions
- Cleans up the temporary directory