package document import ( "fmt" ) // Styles 表示Word文档中的样式集合 type Styles struct { Styles []*Style } // Style 表示Word文档中的样式 type Style struct { ID string Type string // paragraph, character, table, numbering Name string BasedOn string Next string Link string Default bool CustomStyle bool ParagraphProperties *ParagraphProperties RunProperties *RunProperties TableProperties *TableProperties } // NewStyles 创建一个新的样式集合 func NewStyles() *Styles { return &Styles{ Styles: make([]*Style, 0), } } // AddStyle 添加一个样式 func (s *Styles) AddStyle(id, name, styleType string) *Style { style := &Style{ ID: id, Type: styleType, Name: name, CustomStyle: true, ParagraphProperties: &ParagraphProperties{}, RunProperties: &RunProperties{}, } s.Styles = append(s.Styles, style) return style } // GetStyle 获取指定ID的样式 func (s *Styles) GetStyle(id string) *Style { for _, style := range s.Styles { if style.ID == id { return style } } return nil } // SetBasedOn 设置样式的基础样式 func (s *Style) SetBasedOn(basedOn string) *Style { s.BasedOn = basedOn return s } // SetNext 设置样式的下一个样式 func (s *Style) SetNext(next string) *Style { s.Next = next return s } // SetLink 设置样式的链接 func (s *Style) SetLink(link string) *Style { s.Link = link return s } // SetDefault 设置样式是否为默认样式 func (s *Style) SetDefault(isDefault bool) *Style { s.Default = isDefault return s } // SetParagraphProperties 设置段落属性 func (s *Style) SetParagraphProperties(props *ParagraphProperties) *Style { s.ParagraphProperties = props return s } // SetRunProperties 设置文本运行属性 func (s *Style) SetRunProperties(props *RunProperties) *Style { s.RunProperties = props return s } // SetTableProperties 设置表格属性 func (s *Style) SetTableProperties(props *TableProperties) *Style { s.TableProperties = props return s } // ToXML 将样式集合转换为XML func (s *Styles) ToXML() string { xml := "" xml += "" // 添加默认样式 xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" xml += "" // 添加所有样式 for _, style := range s.Styles { xml += "" // 样式名称 xml += "" // 基础样式 if style.BasedOn != "" { xml += "" } // 下一个样式 if style.Next != "" { xml += "" } // 链接 if style.Link != "" { xml += "" } // 默认样式 if style.Default { xml += "" } // 自定义样式 if style.CustomStyle { xml += "" } // 段落属性 if style.ParagraphProperties != nil && style.Type == "paragraph" { xml += "" // 对齐方式 if style.ParagraphProperties.Alignment != "" { xml += "" } // 缩进 if style.ParagraphProperties.IndentLeft > 0 || style.ParagraphProperties.IndentRight > 0 || style.ParagraphProperties.IndentFirstLine > 0 { xml += " 0 { xml += " w:left=\"" + fmt.Sprintf("%d", style.ParagraphProperties.IndentLeft) + "\"" } if style.ParagraphProperties.IndentRight > 0 { xml += " w:right=\"" + fmt.Sprintf("%d", style.ParagraphProperties.IndentRight) + "\"" } if style.ParagraphProperties.IndentFirstLine > 0 { xml += " w:firstLine=\"" + fmt.Sprintf("%d", style.ParagraphProperties.IndentFirstLine) + "\"" } xml += " />" } // 间距 if style.ParagraphProperties.SpacingBefore > 0 || style.ParagraphProperties.SpacingAfter > 0 || style.ParagraphProperties.SpacingLine > 0 { xml += " 0 { xml += " w:before=\"" + fmt.Sprintf("%d", style.ParagraphProperties.SpacingBefore) + "\"" } if style.ParagraphProperties.SpacingAfter > 0 { xml += " w:after=\"" + fmt.Sprintf("%d", style.ParagraphProperties.SpacingAfter) + "\"" } if style.ParagraphProperties.SpacingLine > 0 { xml += " w:line=\"" + fmt.Sprintf("%d", style.ParagraphProperties.SpacingLine) + "\"" xml += " w:lineRule=\"" + style.ParagraphProperties.SpacingLineRule + "\"" } xml += " />" } // 分页控制 if style.ParagraphProperties.KeepNext { xml += "" } if style.ParagraphProperties.KeepLines { xml += "" } if style.ParagraphProperties.PageBreakBefore { xml += "" } if style.ParagraphProperties.WidowControl { xml += "" } // 边框 if style.ParagraphProperties.BorderTop != nil || style.ParagraphProperties.BorderBottom != nil || style.ParagraphProperties.BorderLeft != nil || style.ParagraphProperties.BorderRight != nil { xml += "" if style.ParagraphProperties.BorderTop != nil { xml += "" } if style.ParagraphProperties.BorderBottom != nil { xml += "" } if style.ParagraphProperties.BorderLeft != nil { xml += "" } if style.ParagraphProperties.BorderRight != nil { xml += "" } xml += "" } // 底纹 if style.ParagraphProperties.Shading != nil { xml += "" } xml += "" } // 文本运行属性 if style.RunProperties != nil { xml += "" // 字体 if style.RunProperties.FontFamily != "" { xml += "" } // 字号 if style.RunProperties.FontSize > 0 { xml += "" xml += "" } // 颜色 if style.RunProperties.Color != "" { xml += "" } // 粗体 if style.RunProperties.Bold { xml += "" xml += "" } // 斜体 if style.RunProperties.Italic { xml += "" xml += "" } // 下划线 if style.RunProperties.Underline != "" { xml += "" } // 删除线 if style.RunProperties.Strike { xml += "" } // 双删除线 if style.RunProperties.DoubleStrike { xml += "" } // 突出显示颜色 if style.RunProperties.Highlight != "" { xml += "" } // 全部大写 if style.RunProperties.Caps { xml += "" } // 小型大写 if style.RunProperties.SmallCaps { xml += "" } // 字符间距 if style.RunProperties.CharacterSpacing != 0 { xml += "" } // 底纹 if style.RunProperties.Shading != nil { xml += "" } // 垂直对齐方式 if style.RunProperties.VertAlign != "" { xml += "" } xml += "" } // 表格属性 if style.TableProperties != nil && style.Type == "table" { xml += "" // 表格宽度 if style.TableProperties.Width > 0 { xml += "" } // 表格对齐方式 if style.TableProperties.Alignment != "" { xml += "" } // 表格边框 if style.TableProperties.Borders != nil { xml += "" if style.TableProperties.Borders.Top != nil { xml += "" } if style.TableProperties.Borders.Bottom != nil { xml += "" } if style.TableProperties.Borders.Left != nil { xml += "" } if style.TableProperties.Borders.Right != nil { xml += "" } if style.TableProperties.Borders.InsideH != nil { xml += "" } if style.TableProperties.Borders.InsideV != nil { xml += "" } xml += "" } xml += "" } xml += "" } xml += "" return xml }