DOCXLibWord documents for Ring

Comments, forms, shapes and saving

Comments, mail merge fields, content controls, shapes, text boxes, right-to-left text and saving the document.

Comments#

MethodParametersDescription
addCommentedParagraph()text, commentText, optionsParagraph with a comment balloon attached
addComment()text, commentText, authorShortcut with explicit author name
Ring
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.

Ring
# 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

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#

Ring
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)
Ring
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#

Ring
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#

MethodShape typeNotes
addRect(options)RectangleUse :rounded=true for rounded corners
addEllipse(options)Ellipse / circleEqual width and height for a circle
addDiamond(options)Diamond / rhombusUseful for flowchart decision nodes
addShape("triangle", options)Right triangle
addLine(options)Horizontal ruleThin coloured rect; use :height for thickness
addShape(type, options)Any of the aboveGeneric form

Shape Options#

OptionTypeDescription
:widthNumberWidth in cm. Default: 5
:heightNumberHeight in cm. Default: 3 (line: 0.1)
:fillColorStringFill colour hex. Default: 4472C4
:lineColorStringBorder colour hex. Default: 2E4E7E
:lineWidthNumberBorder width in pt. Default: 1
:noFilltrue/falseTransparent shape (no fill)
:noBordertrue/falseNo border
:roundedtrue/falseRounded corners (rect only)
:textStringLabel text inside the shape
:textColorStringText colour hex. Default: FFFFFF
:textBoldtrue/falseBold text label. Default: true
:textSizeNumberText size in pt. Default: 11
:alignStringOuter paragraph alignment: "left", "center", "right"
Ring
# 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.

Ring
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#

MethodParametersDescription
setDocumentRTL()bEnableEnable document-wide RTL — flips paragraph alignment and list indents
addRTLParagraph()text, optionsSingle RTL paragraph in an otherwise LTR document
Ring
# 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#

Ring
if doc.save("output.docx")
    ? "Document saved successfully."
else
    ? "Error: save() failed."
ok

save() returns true on success. It automatically:

  1. Creates a temporary working directory
  2. Writes all XML parts: document.xml, styles.xml, settings.xml, numbering.xml, fontTable.xml, footnotes.xml, endnotes.xml, comments.xml, header/footer files, and theme/theme1.xml (when a theme is set)
  3. Copies all referenced image files into word/media/
  4. Assembles [Content_Types].xml and all .rels relationship files
  5. Creates the ZIP archive (DOCX format) using Ring's built-in zip functions
  6. Cleans up the temporary directory
DOCXLib - Word documents for Ring, made with ringdoc.