This example demonstrates the comprehensive style management system in go-docx v2.
- Title and Subtitle: Document title styles
- Headings: Heading1 through Heading9
- Normal: Default body text style
- Quote styles: Quote and IntenseQuote
- List styles: ListParagraph, ListBullet, ListNumber
- Reference styles: FootnoteReference, FootnoteText
- Using
StyleManagerto query styles - Applying styles with type-safe constants (
domain.StyleID*) - Checking style availability with
HasStyle() - Character-level formatting (bold, italic, color)
go run main.goThis will create 05_styles_demo.docx demonstrating various built-in styles.
v2 provides constants for all built-in styles:
// Heading styles
para.SetStyle(domain.StyleIDHeading1)
para.SetStyle(domain.StyleIDHeading2)
// Text styles
para.SetStyle(domain.StyleIDNormal)
para.SetStyle(domain.StyleIDQuote)
// List styles
para.SetStyle(domain.StyleIDListParagraph)Access the style manager to query available styles:
styleMgr := doc.StyleManager()
if styleMgr.HasStyle(domain.StyleIDHeading1) {
para.SetStyle(domain.StyleIDHeading1)
}Apply character-level formatting to runs:
run.SetBold(true)
run.SetItalic(true)
run.SetColor(0xFF0000) // Red
run.SetFontSize(14)
run.SetFontFamily("Arial")The generated document includes:
- Styled title and headings
- Normal body paragraphs
- Quoted text with special formatting
- List items
- Footnote references
- Mixed character formatting in paragraphs
- See Example 06 for section and page layout management
- See Example 07 for combining all advanced features
- Read API Documentation for complete style reference