1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-11 19:10:24 +08:00

add w:u w:i w:b w:highlight

This commit is contained in:
源文雨
2023-02-26 15:31:13 +08:00
parent 0e18f8a163
commit f3265280e3
8 changed files with 88 additions and 22 deletions

View File

@@ -46,8 +46,32 @@ func (r *Run) Shade(val, color, fill string) *Run {
return r return r
} }
// AddTab add a tab in front of the run // Bold ...
func (r *Run) AddTab() *Run { func (r *Run) Bold() *Run {
r.Children = append(r.Children, &WTab{}) r.RunProperties.Bold = &Bold{}
return r
}
// Italic ...
func (r *Run) Italic() *Run {
r.RunProperties.Italic = &Italic{}
return r
}
// Underline ...
func (r *Run) Underline(val string) *Run {
r.RunProperties.Underline = &Underline{Val: val}
return r
}
// Highlight ...
func (r *Run) Highlight(val string) *Run {
r.RunProperties.Highlight = &Highlight{Val: val}
return r
}
// AddTab add a tab in front of the run
func (r *Run) AddTab() *Run {
r.Children = append(r.Children, &Tab{})
return r return r
} }

View File

@@ -25,7 +25,7 @@ import "strings"
// AddTab adds tab to para // AddTab adds tab to para
func (p *Paragraph) AddTab() *Run { func (p *Paragraph) AddTab() *Run {
c := make([]interface{}, 1, 64) c := make([]interface{}, 1, 64)
c[0] = &WTab{} c[0] = &Tab{}
run := &Run{ run := &Run{
RunProperties: &RunProperties{}, RunProperties: &RunProperties{},
@@ -47,7 +47,7 @@ func (p *Paragraph) AddText(text string) *Run {
for i, s := range strings.Split(text, "\t") { for i, s := range strings.Split(text, "\t") {
if i > 0 { if i > 0 {
c = append(c, &WTab{}) c = append(c, &Tab{})
} }
if s != "" { if s != "" {
c = append(c, &Text{ c = append(c, &Text{

View File

@@ -52,9 +52,13 @@ func main() {
r.Children[0].(*docx.Drawing).Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.AlphaModFix = &docx.AAlphaModFix{Amount: 50000} r.Children[0].(*docx.Drawing).Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.AlphaModFix = &docx.AAlphaModFix{Amount: 50000}
// add text // add text
para1.AddText("test").AddTab() para1.AddText("test").AddTab()
para1.AddText("test font size").Size("44").AddTab() para1.AddText("size").Size("44").AddTab()
para1.AddText("test color").Color("808080").AddTab() para1.AddText("color").Color("808080").AddTab()
para1.AddText("test shade").Shade("clear", "auto", "E7E6E6").AddTab() para1.AddText("shade").Shade("clear", "auto", "E7E6E6").AddTab()
para1.AddText("bold").Bold().AddTab()
para1.AddText("italic").Italic().AddTab()
para1.AddText("underline").Underline("double").AddTab()
para1.AddText("highlight").Highlight("yellow").AddTab()
para2 := w.AddParagraph().Justification("end") para2 := w.AddParagraph().Justification("end")
para2.AddText("test font size and color").Size("44").Color("ff0000") para2.AddText("test font size and color").Size("44").Color("ff0000")

View File

@@ -33,7 +33,7 @@ func TestDrawingStructure(t *testing.T) {
// add new paragraph // add new paragraph
para1 := w.AddParagraph() para1 := w.AddParagraph()
// add text // add text
para1.AddText("直接粘贴 inline").Shade("clear", "auto", "E7E6E6").AddTab() para1.AddText("直接粘贴 inline").Shade("clear", "auto", "E7E6E6").AddTab().Bold().Underline("single").Highlight("yellow").Italic()
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG") r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)

View File

@@ -27,6 +27,28 @@ type Size struct {
Val string `xml:"w:val,attr"` Val string `xml:"w:val,attr"`
} }
// Bold ...
type Bold struct {
XMLName xml.Name `xml:"w:b,omitempty"`
}
// Italic ...
type Italic struct {
XMLName xml.Name `xml:"w:i,omitempty"`
}
// Underline ...
type Underline struct {
XMLName xml.Name `xml:"w:u,omitempty"`
Val string `xml:"w:val,attr,omitempty"`
}
// Highlight ...
type Highlight struct {
XMLName xml.Name `xml:"w:highlight,omitempty"`
Val string `xml:"w:val,attr,omitempty"`
}
// Justification contains the way of the horizonal alignment // Justification contains the way of the horizonal alignment
// //
// w:jc 属性的取值可以是以下之一: // w:jc 属性的取值可以是以下之一:

View File

@@ -105,7 +105,7 @@ func (p *Paragraph) String() string {
switch x := c.(type) { switch x := c.(type) {
case *Text: case *Text:
sb.WriteString(x.Text) sb.WriteString(x.Text)
case *WTab: case *Tab:
sb.WriteByte('\t') sb.WriteByte('\t')
case *Drawing: case *Drawing:
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil { if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil {

View File

@@ -84,7 +84,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
} }
child = &value child = &value
case "tab": case "tab":
child = &WTab{} child = &Tab{}
default: default:
err = d.Skip() // skip unsupported tags err = d.Skip() // skip unsupported tags
if err != nil { if err != nil {
@@ -99,19 +99,18 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return nil return nil
} }
// WTab is the literal tab
type WTab struct {
XMLName xml.Name `xml:"w:tab,omitempty"`
}
// RunProperties encapsulates visual properties of a run // RunProperties encapsulates visual properties of a run
type RunProperties struct { type RunProperties struct {
XMLName xml.Name `xml:"w:rPr,omitempty"` XMLName xml.Name `xml:"w:rPr,omitempty"`
Color *Color Bold *Bold
Size *Size Italic *Italic
RunStyle *RunStyle Underline *Underline
Style *Style Highlight *Highlight
Shade *Shade Color *Color
Size *Size
RunStyle *RunStyle
Style *Style
Shade *Shade
} }
// UnmarshalXML ... // UnmarshalXML ...
@@ -127,6 +126,18 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
if tt, ok := t.(xml.StartElement); ok { if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local { switch tt.Name.Local {
case "b":
r.Bold = &Bold{}
case "i":
r.Italic = &Italic{}
case "u":
var value Underline
value.Val = getAtt(tt.Attr, "val")
r.Underline = &value
case "highlight":
var value Highlight
value.Val = getAtt(tt.Attr, "val")
r.Highlight = &value
case "color": case "color":
var value Color var value Color
value.Val = getAtt(tt.Attr, "val") value.Val = getAtt(tt.Attr, "val")

View File

@@ -25,6 +25,11 @@ import (
"io" "io"
) )
// Tab is the literal tab
type Tab struct {
XMLName xml.Name `xml:"w:tab,omitempty"`
}
// Text object contains the actual text // Text object contains the actual text
type Text struct { type Text struct {
XMLName xml.Name `xml:"w:t,omitempty"` XMLName xml.Name `xml:"w:t,omitempty"`