mirror of
https://github.com/fumiama/go-docx.git
synced 2026-06-05 07:40:24 +08:00
add w:u w:i w:b w:highlight
This commit is contained in:
30
apirun.go
30
apirun.go
@@ -46,8 +46,32 @@ func (r *Run) Shade(val, color, fill string) *Run {
|
||||
return r
|
||||
}
|
||||
|
||||
// AddTab add a tab in front of the run
|
||||
func (r *Run) AddTab() *Run {
|
||||
r.Children = append(r.Children, &WTab{})
|
||||
// Bold ...
|
||||
func (r *Run) Bold() *Run {
|
||||
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
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ import "strings"
|
||||
// AddTab adds tab to para
|
||||
func (p *Paragraph) AddTab() *Run {
|
||||
c := make([]interface{}, 1, 64)
|
||||
c[0] = &WTab{}
|
||||
c[0] = &Tab{}
|
||||
|
||||
run := &Run{
|
||||
RunProperties: &RunProperties{},
|
||||
@@ -47,7 +47,7 @@ func (p *Paragraph) AddText(text string) *Run {
|
||||
|
||||
for i, s := range strings.Split(text, "\t") {
|
||||
if i > 0 {
|
||||
c = append(c, &WTab{})
|
||||
c = append(c, &Tab{})
|
||||
}
|
||||
if s != "" {
|
||||
c = append(c, &Text{
|
||||
|
||||
@@ -52,9 +52,13 @@ func main() {
|
||||
r.Children[0].(*docx.Drawing).Anchor.Graphic.GraphicData.Pic.BlipFill.Blip.AlphaModFix = &docx.AAlphaModFix{Amount: 50000}
|
||||
// add text
|
||||
para1.AddText("test").AddTab()
|
||||
para1.AddText("test font size").Size("44").AddTab()
|
||||
para1.AddText("test color").Color("808080").AddTab()
|
||||
para1.AddText("test shade").Shade("clear", "auto", "E7E6E6").AddTab()
|
||||
para1.AddText("size").Size("44").AddTab()
|
||||
para1.AddText("color").Color("808080").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.AddText("test font size and color").Size("44").Color("ff0000")
|
||||
|
||||
@@ -33,7 +33,7 @@ func TestDrawingStructure(t *testing.T) {
|
||||
// add new paragraph
|
||||
para1 := w.AddParagraph()
|
||||
// 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")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -27,6 +27,28 @@ type Size struct {
|
||||
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
|
||||
//
|
||||
// w:jc 属性的取值可以是以下之一:
|
||||
|
||||
@@ -105,7 +105,7 @@ func (p *Paragraph) String() string {
|
||||
switch x := c.(type) {
|
||||
case *Text:
|
||||
sb.WriteString(x.Text)
|
||||
case *WTab:
|
||||
case *Tab:
|
||||
sb.WriteByte('\t')
|
||||
case *Drawing:
|
||||
if x.Inline != nil && x.Inline.Graphic != nil && x.Inline.Graphic.GraphicData != nil && x.Inline.Graphic.GraphicData.Pic != nil {
|
||||
|
||||
35
structrun.go
35
structrun.go
@@ -84,7 +84,7 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
}
|
||||
child = &value
|
||||
case "tab":
|
||||
child = &WTab{}
|
||||
child = &Tab{}
|
||||
default:
|
||||
err = d.Skip() // skip unsupported tags
|
||||
if err != nil {
|
||||
@@ -99,19 +99,18 @@ func (r *Run) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// WTab is the literal tab
|
||||
type WTab struct {
|
||||
XMLName xml.Name `xml:"w:tab,omitempty"`
|
||||
}
|
||||
|
||||
// RunProperties encapsulates visual properties of a run
|
||||
type RunProperties struct {
|
||||
XMLName xml.Name `xml:"w:rPr,omitempty"`
|
||||
Color *Color
|
||||
Size *Size
|
||||
RunStyle *RunStyle
|
||||
Style *Style
|
||||
Shade *Shade
|
||||
XMLName xml.Name `xml:"w:rPr,omitempty"`
|
||||
Bold *Bold
|
||||
Italic *Italic
|
||||
Underline *Underline
|
||||
Highlight *Highlight
|
||||
Color *Color
|
||||
Size *Size
|
||||
RunStyle *RunStyle
|
||||
Style *Style
|
||||
Shade *Shade
|
||||
}
|
||||
|
||||
// UnmarshalXML ...
|
||||
@@ -127,6 +126,18 @@ func (r *RunProperties) UnmarshalXML(d *xml.Decoder, start xml.StartElement) err
|
||||
|
||||
if tt, ok := t.(xml.StartElement); ok {
|
||||
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":
|
||||
var value Color
|
||||
value.Val = getAtt(tt.Attr, "val")
|
||||
|
||||
@@ -25,6 +25,11 @@ import (
|
||||
"io"
|
||||
)
|
||||
|
||||
// Tab is the literal tab
|
||||
type Tab struct {
|
||||
XMLName xml.Name `xml:"w:tab,omitempty"`
|
||||
}
|
||||
|
||||
// Text object contains the actual text
|
||||
type Text struct {
|
||||
XMLName xml.Name `xml:"w:t,omitempty"`
|
||||
|
||||
Reference in New Issue
Block a user