Skip to content

Latest commit

 

History

History

README.md

go-docx Examples

This directory contains practical, runnable examples demonstrating go-docx v2 capabilities.

✅ Working Examples (v2)

01_basic/ - Basic Builder Pattern

Status: ✅ Fully functional Demonstrates: Document creation with fluent builder API

  • DocumentBuilder with options (title, author, font, margins)
  • Fluent paragraph building (Text, Bold, Italic, Color, FontSize, Alignment)
  • Mixed formatting in paragraphs
  • Simple table creation
  • Predefined color constants

Run:

cd 01_basic && go run main.go

02_intermediate/ - Product Catalog

Status: ✅ Fully functional Demonstrates: Professional multi-section document

  • Professional document layout
  • Multiple sections with headings
  • Product tables (3 tables with different products)
  • Mixed text formatting
  • Document metadata (title, author, subject)

Run:

cd 02_intermediate && go run main.go

03_toc/ - Table of Contents (NEW!)

Status: ✅ Fully functional Demonstrates: Automatic Table of Contents generation

  • Cover page with Title and Subtitle styles
  • TOC field configured for Heading 1 and Heading 2
  • Placeholder result so the TOC looks polished before updating
  • Chapters, sub-sections, and appendix content driven by heading styles

Run:

cd 03_toc && go run main.go

04_fields/ - Fields System (NEW!)

Status: ✅ Fully functional Demonstrates: Complete field system

  • Page numbers and page count fields
  • Table of Contents (TOC) with custom options
  • Hyperlinks to external URLs
  • Headers and footers
  • Page breaks

Run:

cd 04_fields && go run main.go

05_styles/ - Style Management (NEW!)

Status: ✅ Fully functional Demonstrates: Built-in style system

  • 40+ built-in paragraph styles (Normal, Heading1-9, Title, Subtitle, etc.)
  • Character-level formatting (bold, italic, color, font size)
  • Mixed formatting within paragraphs
  • Quote and list paragraph styles

Run:

cd 05_styles && go run main.go

06_sections/ - Sections and Page Layout (NEW!)

Status: ✅ Fully functional Demonstrates: Advanced page layout

  • Custom page sizes (A4, Letter, Legal, etc.)
  • Page orientation (portrait, landscape)
  • Custom margins
  • Headers and footers with dynamic fields
  • Multi-page documents

Run:

cd 06_sections && go run main.go

07_advanced/ - Advanced Integration (NEW!)

Status: ✅ Fully functional Demonstrates: All Phase 6 features combined

  • Professional cover page
  • Table of Contents with hyperlinks
  • Headers and footers
  • Multiple heading levels
  • Mixed formatting and styles
  • Page numbers (Page X of Y)
  • Hyperlinks
  • Quotes and emphasis

Run:

cd 07_advanced && go run main.go

08_images/ - Image Insertion

Status: ✅ Fully functional Demonstrates: Complete image handling

  • 9 image formats (PNG, JPEG, GIF, BMP, TIFF, SVG, WEBP, ICO, EMF)
  • Inline and floating images
  • Custom dimensions (pixels, inches, EMUs)
  • Positioning (left, center, right, custom coordinates)
  • Automatic format detection

Run:

cd 08_images && go run main.go

09_advanced_tables/ - Advanced Table Features

Status: ✅ Fully functional Demonstrates: Complete table manipulation

  • Cell merging (horizontal colspan, vertical rowspan)
  • Nested tables (tables within cells)
  • 8 built-in table styles
  • Row height control
  • Cell alignment and styling

Run:

cd 09_advanced_tables && go run main.go

10_paragraph_spacing/ - Paragraph Spacing (NEW!)

Status: ✅ Fully functional Demonstrates: Line and paragraph spacing controls

  • Set spacing before and after paragraphs (twips)
  • Configure exact vs. at-least line spacing rules
  • Mix typography blocks for comparison
  • Save finished document ready for inspection

Run:

cd 10_paragraph_spacing && go run main.go

11_multi_section/ - Multi-Section Layouts (NEW!)

Status: ✅ Fully functional Demonstrates: Independent layouts per section

  • Section breaks (Next Page, Continuous)
  • Per-section headers and footers
  • Portrait ↔ landscape transitions
  • Unique margin and column settings per section
  • Dynamic page numbering maintained across sections

Run:

cd 11_multi_section && go run main.go

12_read_and_modify/ - Read and Modify Documents (NEW!)

Status: ✅ Fully functional Demonstrates: Complete read/modify/write workflow

  • Create comprehensive showcase document with all features
  • Read existing .docx files using OpenDocument()
  • Inspect document content (paragraphs, tables, text)
  • Modify existing documents (add paragraphs, tables)
  • Save modified versions with different names
  • Preserve existing formatting and content

Run:

cd 12_read_and_modify && go run main.go

13_themes/ - Theme System (NEW!)

Status: ✅ Fully functional Demonstrates: Complete theme system

  • 7 preset themes (Corporate, Startup, Modern, Fintech, Academic, TechPresentation, TechDarkMode)
  • Custom colors, fonts, spacing, and heading styles
  • Professional document generation with consistent branding
  • Theme application via WithTheme() builder option

Run:

cd 13_themes && go run main.go

14_mail_merge/ - Mail Merge / Templates (NEW!)

Status: ✅ Fully functional Demonstrates: Template engine with mail merge

  • Placeholder detection ({{key}} syntax) across body, tables, headers, footers
  • Template validation (missing/unused keys)
  • Single and batch mail merge
  • Formatting preservation during merge
  • Custom delimiter support

Run:

cd 14_mail_merge && go run main.go

15_external_template/ - External Word Template (NEW!)

Status: ✅ Fully functional Demonstrates: Reading an existing Word document as a mail merge template

  • Opens a real .docx file created in Microsoft Word with MERGEFIELD fields
  • Custom delimiters («» guillemet syntax) for Word-style merge fields
  • Placeholder detection, validation, and batch merge
  • Preserves original document formatting and structure
  • Includes reminder_letter.docx — a real Word template with 20 merge fields

Run:

cd 15_external_template && go run main.go

Testing All Examples

Quick Test - Verify Compilation

Run the included test script to verify all examples compile:

./test_all.sh

This will test all 15 working examples and report results.


Complete Validation - Generate and Validate Documents

🚀 Run All Examples

Execute all examples and generate .docx files:

./run_all_examples.sh

Output: .docx files in their respective directories

🔍 Validate OOXML Integrity

Verify the generated documents are valid:

./validate_docx.sh

Checks:

  • ✅ ZIP structure integrity
  • ✅ Required OOXML files present
  • ✅ Valid Office Open XML format

📊 View Validation Results

See detailed validation reports:


Generated Documents

After running ./run_all_examples.sh, you'll have:

examples/
├── 01_basic/01_basic_builder.docx (4.1KB)
├── 02_intermediate/02_intermediate_builder.docx (4.7KB)
├── 03_toc/03_toc_demo.docx (4.3KB)
├── 04_fields/fields_example.docx (4.0KB)
├── 05_styles/05_styles_demo.docx (3.7KB)
├── 06_sections/06_sections_demo.docx (3.9KB)
├── 07_advanced/07_advanced_demo.docx (4.6KB)
├── 08_images/08_images_output.docx (4.2KB)
├── 09_advanced_tables/09_advanced_tables_output.docx (4.8KB)
├── 11_multi_section/11_multi_section_demo.docx (4.4KB)
├── 12_read_and_modify/
│   ├── 12_showcase_original.docx (3.8KB)
│   └── 12_modified_document.docx (4.2KB)
└── 13_themes/
    └── theme_*.docx (7 themed documents)

All documents are ready to open in:

  • Microsoft Word (Windows/macOS)
  • LibreOffice Writer
  • Google Docs
  • Any OOXML-compatible word processor

Documentation

Contributing Examples

Have a great example? We'd love to include it! See CONTRIBUTING.md.