1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-25 05:20:46 +08:00

feat: add_pagabreaks (#21)

This commit is contained in:
mabiao0525
2024-03-17 18:58:15 +08:00
committed by GitHub
parent 047c5e5f03
commit e9a89648f8
5 changed files with 35 additions and 14 deletions

View File

@@ -55,3 +55,17 @@ func (p *Paragraph) Justification(val string) *Paragraph {
p.Properties.Justification = &Justification{Val: val} p.Properties.Justification = &Justification{Val: val}
return p return p
} }
// AddPageBreaks adds a pagebreaks
func (p *Paragraph) AddPageBreaks() *Run {
c := make([]interface{}, 1, 64)
c[0] = &BarterRabbet{
Type: "page",
}
run := &Run{
RunProperties: &RunProperties{},
Children: c,
}
p.Children = append(p.Children, run)
return run
}

View File

@@ -37,31 +37,31 @@ func (p *Paragraph) AddTab() *Run {
return run return run
} }
// AddText adds text to paragraph // AddText adds text to paragraph with \n
func (p *Paragraph) AddText(text string) *Run { func (p *Paragraph) AddText(text string) *Run {
if text == "\t" { if text == "\t" {
return p.AddTab() return p.AddTab()
} }
c := make([]interface{}, 0, 64) c := make([]interface{}, 0, 64)
for i, s := range strings.Split(text, "\n") {
for i, s := range strings.Split(text, "\t") {
if i > 0 { if i > 0 {
c = append(c, &Tab{}) c = append(c, &BarterRabbet{})
} }
if s != "" { for tabIndex, k := range strings.Split(s, "\t") {
c = append(c, &Text{ if tabIndex > 0 {
Text: s, c = append(c, &Tab{})
}) }
if k != "" {
c = append(c, &Text{
Text: k,
})
}
} }
} }
run := &Run{ run := &Run{
RunProperties: &RunProperties{}, RunProperties: &RunProperties{},
Children: c, Children: c,
} }
p.Children = append(p.Children, run) p.Children = append(p.Children, run)
return run return run
} }

View File

@@ -95,6 +95,7 @@ func main() {
} }
r.Children[0].(*docx.Drawing).Inline.Size(r.Children[0].(*docx.Drawing).Inline.Extent.CX*4/5, r.Children[0].(*docx.Drawing).Inline.Extent.CY*4/5) r.Children[0].(*docx.Drawing).Inline.Size(r.Children[0].(*docx.Drawing).Inline.Extent.CX*4/5, r.Children[0].(*docx.Drawing).Inline.Extent.CY*4/5)
w.AddParagraph().AddPageBreaks()
para5 := w.AddParagraph().Justification("center") para5 := w.AddParagraph().Justification("center")
// add text // add text
para5.AddText("一行1个 横向 inline").Size("44") para5.AddText("一行1个 横向 inline").Size("44")

View File

@@ -117,7 +117,12 @@ func (r *Run) parse(d *xml.Decoder, tt xml.StartElement) (child interface{}, err
case "tab": case "tab":
child = &Tab{} child = &Tab{}
case "br": case "br":
child = &BarterRabbet{} var brvalue BarterRabbet
err = d.DecodeElement(&brvalue, &tt)
if err != nil {
return nil, err
}
child = &brvalue
case "AlternateContent": case "AlternateContent":
/*var value AlternateContent /*var value AlternateContent
value.file = r.file value.file = r.file

View File

@@ -88,9 +88,10 @@ func (t *Tab) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
return err return err
} }
// BarterRabbet is <br> // BarterRabbet is <br> , if with type=page , add pagebreaks
type BarterRabbet struct { type BarterRabbet struct {
XMLName xml.Name `xml:"w:br,omitempty"` XMLName xml.Name `xml:"w:br,omitempty"`
Type string `xml:"w:type,attr,omitempty"`
} }
// Text object contains the actual text // Text object contains the actual text