This directory contains comprehensive examples demonstrating the features of go-docx v2.
Status: ✅ Complete (Phase 6.5)
Demonstrates: Using the builder pattern for simple document creation.
- DocumentBuilder with fluent API
- Document options (title, author, font, margins)
- Predefined color constants
- Text formatting (bold, italic, color, size)
- Alignment control
- Simple table creation
- Mixed formatting in paragraphs
Status: ✅ Complete (Phase 6.5)
Demonstrates: Building a professional product catalog with the builder pattern.
- Professional document layout
- Multiple sections with headings
- Product tables with pricing
- Mixed text formatting
- Color-coded information
- Document metadata
Status: ✅ Complete (Phase 6)
Demonstrates: Using the comprehensive field system for dynamic content.
- Page numbers and page count
- Table of Contents (TOC)
- Hyperlinks
- Date/Time fields
- Document properties
- Style references
- Field updates
Status: ✅ Complete (Phase 6)
Demonstrates: Using built-in styles and the StyleManager.
- Title, Subtitle, and Heading styles
- Normal paragraph style
- Quote and IntenseQuote styles
- List paragraph style
- Footnote reference style
- Character-level formatting (bold, italic, color)
- StyleManager for querying styles
Status: ✅ Complete (Phase 6)
Demonstrates: Complete page layout and section management.
- Page sizes (A3, A4, A5, Letter, Legal, Tabloid)
- Page orientation (Portrait, Landscape)
- Custom margins (all sides configurable)
- Headers and footers (Default, First, Even)
- Dynamic page numbering
- Multi-column layouts
Status: ✅ Complete (Phase 6)
Demonstrates: Combining all features to create professional documents.
- Professional cover page
- Table of Contents with hyperlinks
- Multiple content sections
- Custom headers and footers
- Mixed text formatting
- Working hyperlinks
- Complete document structure
Status: ✅ Complete (Phase 6)
Demonstrates: Multi-section documents with per-section settings.
- Section breaks (Next Page, Continuous)
- Landscape and portrait sections in a single document
- Independent headers/footers per section
- Column configuration per section
- Dynamic fields spanning across sections
Each example is a standalone Go program. Navigate to the example directory and run:
cd basic
go run main.goOr run directly:
go run ./basic/main.go- Example 01: Basic builder - Start here for simple documents
- Example 02: Intermediate builder - Product catalog example
- Example 05: Style management
- Example 06: Sections and page layout
- Example 07: Complete integration with all features
- Go 1.21 or higher
- go-docx v2 module
Each example creates a .docx file in its directory:
01_basic/01_basic_builder.docx02_intermediate/02_intermediate_builder.docx05_styles/05_styles_demo.docx06_sections/06_sections_demo.docx07_advanced/07_advanced_demo.docx11_multi_section/11_multi_section_demo.docx
All generated documents can be opened with:
- Microsoft Word (Windows, macOS)
- LibreOffice Writer
- Google Docs (upload)
- Pages (macOS)
- Any OOXML-compatible word processor
Documents with fields (TOC, page numbers) may require updating:
- Open the document
- Press F9 (or right-click → "Update Field")
- Choose "Update entire table" for TOC
Beginner - Builder Pattern (Recommended):
- Start with Example 01 (basic builder) - Simple fluent API
- Example 02 (intermediate builder) - Product catalog
- Learn builder features: colors, alignment, tables
Advanced - Direct API:
- Example 05 (styles) - Understanding style system
- Example 06 (sections) - Page layout control
- Example 07 (advanced) - Professional documents with all features
package main
import docx "github.com/mmonterroca/docxgo"
func main() {
// Create document with options
doc, err := docx.NewDocumentBuilder(
docx.WithTitle("My Document"),
docx.WithDefaultFont("Arial", 11),
docx.WithPageSize(docx.PageSizeA4),
).Build()
// Add content using fluent API
doc.AddParagraph().
Text("Hello, World!").
Bold().
Color(docx.Blue).
FontSize(14).
Alignment(docx.AlignmentCenter).
End()
// Save
doc.SaveToFile("output.docx")
}package main
import (
docx "github.com/mmonterroca/docxgo"
"github.com/mmonterroca/docxgo/domain"
)
func main() {
// 1. Create document
doc := docx.NewDocument()
// 2. Configure layout (optional)
section, _ := doc.DefaultSection()
section.SetPageSize(domain.PageSizeA4)
// 3. Add content
para, _ := doc.AddParagraph()
run, _ := para.AddRun()
run.AddText("Hello, World!")
// 4. Save
doc.SaveToFile("output.docx")
}doc.AddParagraph().
Text("Important Notice").
Bold().
Color(docx.Red).
FontSize(14).
End()doc.AddParagraph().
Text("This is ").
Text("bold").Bold().
Text(" and this is ").
Text("red").Color(docx.Red).
Text(".").
End()doc.AddTable().
Width(5000).
Row().
Cell().Text("Header 1").Bold().End().
Cell().Text("Header 2").Bold().End().
End().
Row().
Cell().Text("Data 1").End().
Cell().Text("Data 2").End().
End().
End()h1, _ := doc.AddParagraph()
h1.SetStyle(domain.StyleIDHeading1)
run, _ := h1.AddRun()
run.AddText("Section Title")item, _ := doc.AddParagraph()
item.SetStyle(domain.StyleIDListParagraph)
run, _ := item.AddRun()
run.AddText("• List item")section, _ := doc.DefaultSection()
footer, _ := section.Footer(domain.FooterDefault)
para, _ := footer.AddParagraph()
para.SetAlignment(domain.AlignmentCenter)
r1, _ := para.AddRun()
r1.AddText("Page ")
r2, _ := para.AddRun()
r2.AddField(docx.NewPageNumberField())run, _ := para.AddRun()
linkField := docx.NewHyperlinkField("https://example.com", "Click here")
run.SetColor(0x0000FF)
run.SetUnderline(domain.UnderlineSingle)
run.AddField(linkField)For comprehensive API documentation, see:
- API Documentation
- Migration Guide (v1 to v2)
- Design Document
Found an issue or want to add an example? See CONTRIBUTING.md.
These examples are part of go-docx v2 and are licensed under MIT.