This example demonstrates the theme system in go-docx, which allows you to apply consistent visual styling to your documents.
The library includes 5 preset themes:
Professional business theme with navy blue and red accents. Ideal for business reports, proposals, and corporate documentation.
Color Palette:
- Primary: Navy Blue (#2F5496)
- Secondary: Light Blue (#4F81BD)
- Accent: Red (#C00000)
Best For: Business reports, corporate presentations, formal proposals
Energetic modern theme with vibrant colors. Perfect for pitch decks, business plans, and innovative proposals.
Color Palette:
- Primary: Slate Blue (#6A5ACD)
- Secondary: Turquoise (#48D1CC)
- Accent: Tomato Red (#FF6347)
Best For: Startup documents, pitch decks, innovation reports
Clean minimalist theme with contemporary styling. Great for technical documentation, white papers, and modern reports.
Color Palette:
- Primary: Wet Asphalt (#34495E)
- Secondary: Concrete (#95A5A6)
- Accent: Peter River Blue (#2980B9)
Best For: Technical documentation, white papers, product specs
Professional financial theme with trustworthy blues and greens. Ideal for financial reports, investment documents, and banking materials.
Color Palette:
- Primary: Dark Cerulean (#005288)
- Secondary: Blue NCS (#007BA7)
- Accent: Teal (#009688)
Best For: Financial reports, investment documents, banking materials
Traditional scholarly theme for academic and research documents. Perfect for research papers, academic reports, and thesis documents.
Color Palette:
- Primary: Black
- Secondary: Dark Gray
- Accent: Maroon (#800000)
Best For: Research papers, academic reports, thesis documents
import (
docx "github.com/mmonterroca/docxgo"
"github.com/mmonterroca/docxgo/themes"
)
// Create a document with a theme
builder := docx.NewDocumentBuilder(
docx.WithTheme(themes.Corporate),
)
builder.AddParagraph().
Text("Hello, World!").
End()
doc, _ := builder.Build()
doc.SaveAs("output.docx")// Clone a theme and customize it
customTheme := themes.Corporate.Clone()
// Modify colors
colors := customTheme.Colors()
colors.Primary = docx.Color{R: 255, G: 0, B: 0} // Change to red
customTheme = customTheme.WithColors(colors)
// Modify fonts
fonts := customTheme.Fonts()
fonts.Body = "Arial"
fonts.Heading = "Arial Black"
customTheme = customTheme.WithFonts(fonts)
// Use the custom theme
builder := docx.NewDocumentBuilder(
docx.WithTheme(customTheme),
)// Open an existing document
doc, _ := docx.OpenDocument("existing.docx")
// Apply a theme
themes.Modern.ApplyTo(doc)
// Save with new styling
doc.SaveAs("restyled.docx")The examples are organized in subdirectories:
Demonstrates all 5 preset themes with full document content:
cd examples/13_themes/01_main
go run main.goGenerates: corporate_theme.docx, startup_theme.docx, modern_theme.docx, fintech_theme.docx, academic_theme.docx, theme_comparison.docx
Shows how to clone and customize themes or build from scratch:
cd examples/13_themes/02_custom_theme
go run main.goGenerates: custom_cloned_theme.docx, custom_brand_theme.docx, custom_builder_theme.docx
Demonstrates using themes from an external package:
cd examples/13_themes/03_external_themes
go run main.goGenerates: gaming_theme_example.docx, medical_theme_example.docx, legal_theme_example.docx
Our showcase example! Modern technical documentation with PlantUML diagrams:
cd examples/13_themes/04_tech_architecture
go run main.goGenerates: tech_architecture_light.docx, tech_architecture_dark.docx
Features:
- Tech Presentation theme (Light & Dark Mode)
- PlantUML diagrams (Class, Component, Sequence)
- Code blocks with syntax highlighting
- Professional tables
- Architecture Decision Records (ADRs)
- Complete microservices architecture example
Each theme defines:
-
Colors - Complete color palette including:
- Primary, Secondary, Accent
- Background, Text, TextLight
- Success, Warning, Error
-
Fonts - Font families and sizes:
- Body font, Heading font, Monospace
- Body size, Small size
-
Spacing - Layout spacing:
- Paragraph spacing (before/after)
- Line spacing
- Heading spacing
- Section spacing
-
Headings - Heading styles:
- H1, H2, H3 sizes
- Bold settings
- Color usage
You can create your own theme from scratch:
import "github.com/mmonterroca/docxgo/themes"
customTheme := themes.NewTheme(
"my-theme",
"My Custom Theme",
"A theme tailored for my organization",
)
// Configure colors
colors := themes.ThemeColors{
Primary: domain.Color{R: 100, G: 100, B: 200},
Secondary: domain.Color{R: 150, G: 150, B: 250},
// ... configure all colors
}
customTheme = customTheme.WithColors(colors)
// Configure fonts
fonts := themes.ThemeFonts{
Body: "Georgia",
Heading: "Helvetica",
BodySize: 24, // 12pt
// ...
}
customTheme = customTheme.WithFonts(fonts)
// Use your custom theme
builder := docx.NewDocumentBuilder(
docx.WithTheme(customTheme),
)- Choose the Right Theme: Select a theme that matches your document's purpose and audience
- Consistency: Use themes to maintain consistency across multiple documents
- Customization: Don't hesitate to customize themes to match your brand
- Testing: Always test themed documents in Microsoft Word to ensure compatibility
- Accessibility: Consider color contrast and readability when customizing colors
Themes work by configuring the document's StyleManager. When a theme is applied:
- The Normal (default) paragraph style is configured with the theme's font and spacing
- Heading styles (H1-H3) are configured with appropriate sizes, colors, and formatting
- Special styles (Title, Subtitle, Quote, etc.) are styled according to the theme
- The configuration is applied to the document's built-in styles
Styles in DOCX follow an inheritance model:
- Most paragraph styles inherit from "Normal"
- Heading styles inherit from "Normal" but override specific properties
- Themes leverage this inheritance to create consistent styling
Themes use only standard OOXML properties and are compatible with:
- Microsoft Word (2010 and later)
- LibreOffice Writer
- Google Docs (with some limitations)
- Other OOXML-compatible applications
External developers can create their own theme packages and distribute them. There are three approaches:
See custom_theme_example.go for a complete example of cloning and customizing existing themes.
Create a separate Go package with your themes:
// mythemes/mythemes.go
package mythemes
import "github.com/mmonterroca/docxgo/themes"
var Gaming = themes.NewTheme("gaming", "Gaming", "Vibrant gaming theme")
var Medical = themes.NewTheme("medical", "Medical", "Clean medical theme")
// ... configure themesUse in your code:
import "your-module/mythemes"
doc := docx.NewDocument()
mythemes.Gaming.ApplyTo(doc)See external_themes/ directory for a complete example package with Gaming, Medical, and Legal themes.
You can publish your themes as a Go module for others to use:
# Create module
go mod init github.com/yourname/my-docx-themes
# Publish to GitHub
git tag v1.0.0
git push --tags
# Others can use it
go get github.com/yourname/my-docx-themes# Main theme demonstration
cd 01_main && go run main.go && cd ..
# Custom theme cloning example
cd 02_custom_theme && go run main.go && cd ..
# External themes package example
cd 03_external_themes && go run main.go && cd ..
# Technical architecture showcase
cd 04_tech_architecture && go run main.go && cd ..Or run all at once with the provided script:
cd examples/13_themes
./run_all.shThis will execute all examples in sequence and generate 14 documents total.
For complete documentation on creating custom themes, see:
- CUSTOM_THEMES_GUIDE.md - Comprehensive guide for external developers
external_themes/- Example theme package with Gaming, Medical, and Legal themescustom_theme_example.go- Example of cloning and customizing themesexternal_example.go- Example of using external theme packages
- Main README - Library documentation
- Styles Example - Working with styles
- V2 API Guide - Complete API reference
- Theme System Design - Theme architecture documentation