BoxLang ๐ A New JVM Dynamic Language Learn More...
A powerful BoxLang module for creating, reading, populating, and converting Word documents.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โก B o x L a n g W o r d โ
โ Dynamic ยท Powerful ยท Production-Ready โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Copyright Since 2023 by Ortus Solutions, Corp
www.boxlang.io | www.ortussolutions.com
The BoxLang Word Module (bx-word) is a
fluent document builder for .docx files in BoxLang. Built
on It lets you create, populate, style, and export Word documents with
a clean, chainable API. Import content from text, Markdown, and HTML
on the fly.
| API Type | Entry Point | Use Case |
|---|---|---|
| Fluent API โจ | word()
| Modern chainable interface (recommended) |
| Importers ๐ฅ | .fromText()
/ .fromMarkdown() / .fromHTML()
| Ingest content from multiple formats |
| Exporters ๐ค | .toText() /
.toMarkdown() / .toHTML()
| Convert documents to text or markup |
๐ก The fluent API is the recommended approach. Chain methods to build, style, and save documents in a single expression.
{{placeholder}} substitution with struct datafromText(),
fromMarkdown(), fromHTML() as fluent
instance methodsUsing CommandBox:
box install bx-word
// Build a document from scratch
word( "/path/to/report.docx" )
.margins( 1.78, 1.78, 1.78, 1.78 )
.addHeading( "My Report", 1 )
.addParagraph( "Hello World" )
.addTable( [ [ "Name", "Role" ], [ "John Doe", "Engineer" ] ] )
.save()
// Open an existing template and fill placeholders
word( "/templates/contract.docx" )
.populate( { name: "John Doe", date: "2024-01-01" } )
.save( "/output/contract.docx" );
// The word() BIF accepts text, markdown, and html arguments directly
word( text="Hello World\n\nParagraph two" )
.save( "from-text.docx" );
word( markdown="## Title\n\nParagraph with **bold** text." )
.save( "from-markdown.docx" );
word( html="<h1>Title</h1><p>Hello <b>World</b></p>" )
.save( "from-html.docx" );
// Append content to an existing document
word().fromMarkdown( "## Title\n\nParagraph with **bold** text." )
.save( "from-markdown.docx" );
word().fromHTML( "<h1>Title</h1><p>Hello <b>World</b></p>" )
.save( "from-html.docx" );
// Build and convert in one chain
doc = word().addHeading( "Report", 1 ).addParagraph( "Content" );
plainText = doc.toText(); // plain text
markdown = doc.toMarkdown(); // markdown with YAML front matter
html = doc.toHTML(); // HTML with inline styles + base64 images
| Method | Description |
|---|---|
word()
| Create a new empty document |
word( path )
| Open an existing .docx file |
word( text=... )
| Create and populate from plain text |
word( markdown=... )
| Create and populate from Markdown |
word( html=... )
| Create and populate from HTML |
.open( path )
| Open an existing .docx file |
.save( path )
| Save to the given file path |
.save()
| Save back to the originally opened path |
.toBytes()
| Get document as byte array |
.toBase64()
| Get document as Base64 string |
.writeTo( stream )
| Write document to an OutputStream |
| Method | Description |
|---|---|
.fromText( text )
| Add paragraphs from plain text (splits on double-newlines) |
.fromMarkdown( md )
| Parse CommonMark and add headings, paragraphs, lists, etc. |
.fromHTML( html )
| Parse HTML and add headings, paragraphs, lists, tables, images, etc. |
| Method | Description |
|---|---|
.toText()
| Extract all visible text (paragraphs + tables) |
.toMarkdown()
| Convert to Markdown with YAML front matter, pipe tables, base64 images |
.toHTML()
| Convert to HTML with inline styles, semantic tags, base64 images |
| Method | Description |
|---|---|
.addHeading( text, level )
| Add a heading (level 1โ6) |
.addParagraph( text )
| Add a plain text paragraph |
.addParagraph( text, options )
| Add a paragraph with formatting (bold,
italic, size, color,
alignment, font) |
.addPageBreak()
| Insert a page break |
.newLine()
| Add an empty line |
.addTable( data )
| Add a table from Array (2-D), Query, or Struct
(headers + data) |
.addUnorderedList( items )
| Add a bulleted list (supports nested via structs) |
.addOrderedList( items )
| Add a numbered list (supports nested via structs) |
.addHyperlink( text, url )
| Add a clickable hyperlink |
.addImage( path )
| Add an image from a file path |
.addImage( path, w, h )
| Add an image with explicit width/height in cm |
.addImage( bytes, filename )
| Add an image from byte array |
.addImageBase64( b64, filename )
| Add an image from Base64 string |
| Method | Description |
|---|---|
.margins( top, bottom, left, right )
| Set all four margins in cm |
.margins( options )
| Set margins via struct (top,
bottom, left, right) |
.pageSize( size )
| Set page size (A4, LETTER, LEGAL) |
.orientation( orientation )
| Set page orientation (portrait, landscape) |
.header( text )
| Set document header text |
.footer( text )
| Set document footer text |
| Method | Description |
|---|---|
.populate( data )
| Replace {{variable}} placeholders with
struct values |
.replaceText( find, replace )
| Literal find-and-replace across all paragraphs and tables |
.setBookmark( name, value )
| Set a named bookmark value |
.setContentControl( tag, value )
| Set a content control (SDT) by tag name |
| Method | Description |
|---|---|
.setTitle( title )
| Set the document title |
.setAuthor( author )
| Set the document author/creator |
.setSubject( subject )
| Set the document subject |
.setKeywords( keywords )
| Set comma-separated keywords |
.setDefaultFont( family, size )
| Apply a default font to all existing runs |
word()
.margins( 1.5, 1.5, 1.5, 1.5 )
.addHeading( "Q4 Sales Report", 1 )
.addParagraph( "Generated on " & now(), { italic: true, size: 10 } )
.addTable( [
[ "Product", "Units", "Revenue" ],
[ "Widget A", "1,200", "$36,000" ],
[ "Widget B", "950", "$28,500" ]
] )
.addPageBreak()
.addHeading( "Summary", 2 )
.addParagraph( "All targets exceeded for the quarter.", { bold: true } )
.save( "sales-report.docx" );
clients = [ { name: "Acme Corp", date: "2024-01-15" }, { name: "Globex", date: "2024-01-20" } ];
clients.each( client => {
word( "/templates/contract-template.docx" )
.populate( { clientName: client.name, signDate: client.date } )
.save( "/output/contract_#client.name#.docx" );
} );
// HTML โ Word โ Markdown pipeline
doc = word().fromHTML( "<h1>Article</h1><p>Content with <b>bold</b> text.</p>" );
markdown = doc.toMarkdown(); // # Article\n\nContent with **bold** text.
// Or use BIF args for one-step creation + conversion
text = word( html="<h1>Title</h1>" ).toText(); // "Title"
md = word( markdown="## Hello" ).toMarkdown(); // "---\n\n## Hello\n"
html = word( text="Hello\n\nWorld" ).toHTML(); // <!DOCTYPE html>...</html>
word( "incoming.docx" )
.replaceText( "[DATE]", dateTimeFormat( now(), "yyyy-mm-dd" ) )
.replaceText( "[AUTHOR]", "Ortus Solutions" )
.addParagraph( "Approved by Management", { bold: true, color: "008000" } )
.save( "finalized.docx" );
Professional Services Available โ Need help with BoxLang implementation, training, or consulting? Contact Ortus Solutions
All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
$
box install bx-word