mirror of
https://github.com/ZeroHawkeye/wordZero.git
synced 2025-09-26 20:01:17 +08:00
Page:
en Style System
Pages
01 快速开始
02 基础功能
03 样式系统
04 文本格式化
05 表格操作
06 页面设置
07 图片操作
08 高级功能
09 最佳实践
10 API参考
11 示例项目
12 模板功能
13 性能基准测试
14 功能特性详览
15 项目结构详解
16 Markdown双向转换
Home
en API Reference
en Advanced Features
en Basic Features
en Best Practices
en Example Projects
en Feature Overview
en Home
en Image Operations
en Markdown Conversion
en Page Settings
en Performance Benchmarks
en Project Structure
en Quick Start
en Style System
en Table Operations
en Template Features
en Text Formatting
Clone
1
en Style System
zero edited this page 2025-06-05 09:38:49 +08:00
Style System
WordZero provides a comprehensive style system that allows you to create professional-looking documents with consistent formatting. This chapter covers predefined styles, custom styles, and style management.
🎨 Predefined Styles
WordZero includes 18 built-in styles that are compatible with Microsoft Word:
Heading Styles
style.StyleHeading1
throughstyle.StyleHeading9
- Compatible with Word's navigation pane
- Automatic table of contents generation
Document Styles
style.StyleTitle
- Document titlestyle.StyleSubtitle
- Document subtitlestyle.StyleNormal
- Default body text
Specialized Styles
style.StyleQuote
- Block quotesstyle.StyleCodeBlock
- Code blocksstyle.StyleCodeChar
- Inline codestyle.StyleListParagraph
- List itemsstyle.StyleEmphasis
- Emphasized textstyle.StyleStrong
- Strong text
📝 Using Predefined Styles
Basic Style Application
import (
"github.com/ZeroHawkeye/wordZero/pkg/document"
"github.com/ZeroHawkeye/wordZero/pkg/style"
)
doc := document.New()
// Apply title style
title := doc.AddParagraph("Document Title")
title.SetStyle(style.StyleTitle)
// Apply heading styles
h1 := doc.AddParagraph("Chapter 1")
h1.SetStyle(style.StyleHeading1)
h2 := doc.AddParagraph("Section 1.1")
h2.SetStyle(style.StyleHeading2)
// Apply body text style
body := doc.AddParagraph("This is normal body text.")
body.SetStyle(style.StyleNormal)
Complete Style Example
package main
import (
"fmt"
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
"github.com/ZeroHawkeye/wordZero/pkg/style"
)
func main() {
doc := document.New()
// Document title and subtitle
title := doc.AddParagraph("WordZero Style System Guide")
title.SetStyle(style.StyleTitle)
subtitle := doc.AddParagraph("Comprehensive Style Management")
subtitle.SetStyle(style.StyleSubtitle)
// Heading hierarchy
h1 := doc.AddParagraph("1. Introduction")
h1.SetStyle(style.StyleHeading1)
h2 := doc.AddParagraph("1.1 What are Styles?")
h2.SetStyle(style.StyleHeading2)
h3 := doc.AddParagraph("1.1.1 Style Benefits")
h3.SetStyle(style.StyleHeading3)
// Body content
para1 := doc.AddParagraph("Styles provide consistent formatting throughout your document and enable features like automatic table of contents generation.")
para1.SetStyle(style.StyleNormal)
// Quote
quote := doc.AddParagraph("Consistency is key to professional document presentation.")
quote.SetStyle(style.StyleQuote)
// Code example
codeTitle := doc.AddParagraph("1.2 Code Example")
codeTitle.SetStyle(style.StyleHeading2)
code := doc.AddParagraph(`// Apply style to paragraph
para := doc.AddParagraph("Hello World")
para.SetStyle(style.StyleNormal)`)
code.SetStyle(style.StyleCodeBlock)
// List
listTitle := doc.AddParagraph("1.3 Available Styles")
listTitle.SetStyle(style.StyleHeading2)
items := []string{
"Title and Subtitle styles",
"Heading styles (1-9 levels)",
"Body text styles",
"Code and quote styles",
}
for _, item := range items {
listItem := doc.AddParagraph("• " + item)
listItem.SetStyle(style.StyleListParagraph)
}
err := doc.Save("style_guide.docx")
if err != nil {
log.Fatalf("Failed to save: %v", err)
}
fmt.Println("Style guide created successfully!")
}
🔧 Custom Styles
Creating Custom Styles
// Create a custom style
customStyle := &style.Style{
Name: "CustomHeading",
Type: style.TypeParagraph,
FontName: "Arial",
FontSize: 16,
Bold: true,
FontColor: "336699",
Alignment: style.AlignmentCenter,
}
// Register the style
doc.RegisterStyle(customStyle)
// Use the custom style
heading := doc.AddParagraph("Custom Styled Heading")
heading.SetCustomStyle("CustomHeading")
Style Properties
// Complete custom style definition
customStyle := &style.Style{
Name: "MyStyle",
Type: style.TypeParagraph,
FontName: "Times New Roman",
FontSize: 12,
Bold: true,
Italic: false,
Underline: false,
FontColor: "000000",
Alignment: style.AlignmentLeft,
LineSpacing: 1.5,
SpaceBefore: 6,
SpaceAfter: 6,
FirstLineIndent: 0,
LeftIndent: 0,
RightIndent: 0,
}
📊 Style Management
Querying Styles
// Get all available styles
allStyles := doc.GetAllStyles()
// Get styles by type
headingStyles := doc.GetStylesByType(style.TypeHeading)
paragraphStyles := doc.GetStylesByType(style.TypeParagraph)
// Check if style exists
if doc.HasStyle("CustomStyle") {
// Style exists
}
Style Validation
// Validate style before use
if err := style.ValidateStyle(customStyle); err != nil {
log.Printf("Invalid style: %v", err)
return
}
// Apply validated style
para.SetCustomStyle(customStyle.Name)
🎯 Style Best Practices
1. Consistent Hierarchy
// Use heading styles in order
doc.AddParagraph("Chapter").SetStyle(style.StyleHeading1)
doc.AddParagraph("Section").SetStyle(style.StyleHeading2)
doc.AddParagraph("Subsection").SetStyle(style.StyleHeading3)
// Don't skip levels (H1 → H3 without H2)
2. Semantic Styling
// Use styles based on content meaning, not appearance
doc.AddParagraph("Code Example").SetStyle(style.StyleHeading2)
doc.AddParagraph("if err != nil { return }").SetStyle(style.StyleCodeBlock)
doc.AddParagraph("Important note").SetStyle(style.StyleQuote)
3. Style Reuse
// Define styles once, use multiple times
emphasisStyle := &style.Style{
Name: "Emphasis",
Type: style.TypeCharacter,
Bold: true,
FontColor: "CC0000",
}
doc.RegisterStyle(emphasisStyle)
// Reuse throughout document
para1.SetCustomStyle("Emphasis")
para2.SetCustomStyle("Emphasis")
🔍 Style Inheritance
WordZero supports style inheritance for consistency:
// Base style
baseStyle := &style.Style{
Name: "BaseHeading",
FontName: "Arial",
Bold: true,
}
// Derived style inherits base properties
derivedStyle := &style.Style{
Name: "SpecialHeading",
BasedOn: "BaseHeading",
FontColor: "336699",
FontSize: 18,
}
doc.RegisterStyle(baseStyle)
doc.RegisterStyle(derivedStyle)
📋 Complete Style Example
Here's a comprehensive example using various styles:
package main
import (
"fmt"
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
"github.com/ZeroHawkeye/wordZero/pkg/style"
)
func createStyledDocument() error {
doc := document.New()
// Create custom styles
warningStyle := &style.Style{
Name: "Warning",
Type: style.TypeParagraph,
Bold: true,
FontColor: "CC0000",
FontSize: 12,
}
highlightStyle := &style.Style{
Name: "Highlight",
Type: style.TypeParagraph,
FontColor: "FFFFFF",
BackgroundColor: "336699",
FontSize: 11,
}
// Register custom styles
doc.RegisterStyle(warningStyle)
doc.RegisterStyle(highlightStyle)
// Document structure
title := doc.AddParagraph("Style System Demonstration")
title.SetStyle(style.StyleTitle)
// Section 1
section1 := doc.AddParagraph("1. Predefined Styles")
section1.SetStyle(style.StyleHeading1)
normal := doc.AddParagraph("This paragraph uses the normal style.")
normal.SetStyle(style.StyleNormal)
quote := doc.AddParagraph("This is a quote demonstrating the quote style.")
quote.SetStyle(style.StyleQuote)
// Section 2
section2 := doc.AddParagraph("2. Custom Styles")
section2.SetStyle(style.StyleHeading1)
warning := doc.AddParagraph("⚠️ This is a warning using custom styling!")
warning.SetCustomStyle("Warning")
highlight := doc.AddParagraph("This text is highlighted with custom background.")
highlight.SetCustomStyle("Highlight")
// Section 3 - Code example
section3 := doc.AddParagraph("3. Code Examples")
section3.SetStyle(style.StyleHeading1)
codeBlock := doc.AddParagraph(`func main() {
doc := document.New()
para := doc.AddParagraph("Hello")
para.SetStyle(style.StyleNormal)
}`)
codeBlock.SetStyle(style.StyleCodeBlock)
return doc.Save("styled_document.docx")
}
func main() {
if err := createStyledDocument(); err != nil {
log.Fatalf("Error: %v", err)
}
fmt.Println("Styled document created successfully!")
}
💡 Tips and Tricks
1. Style Naming
- Use descriptive names:
"WarningText"
instead of"RedBold"
- Follow consistent naming conventions
- Use semantic names based on content purpose
2. Performance
- Register custom styles once at document creation
- Reuse styles rather than creating similar ones
- Use predefined styles when possible
3. Compatibility
- Test custom styles in Microsoft Word
- Use standard fonts for better compatibility
- Keep style definitions simple for best results
Next Steps
Continue learning about specific formatting features:
- Text Formatting - Detailed text formatting
- Table Operations - Table styling
- Page Settings - Document layout
Master the style system to create professional, consistent documents!