1
0
mirror of https://github.com/fumiama/go-docx.git synced 2026-06-04 23:30:25 +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}
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
}
// AddText adds text to paragraph
// AddText adds text to paragraph with \n
func (p *Paragraph) AddText(text string) *Run {
if text == "\t" {
return p.AddTab()
}
c := make([]interface{}, 0, 64)
for i, s := range strings.Split(text, "\t") {
for i, s := range strings.Split(text, "\n") {
if i > 0 {
c = append(c, &Tab{})
c = append(c, &BarterRabbet{})
}
if s != "" {
c = append(c, &Text{
Text: s,
})
for tabIndex, k := range strings.Split(s, "\t") {
if tabIndex > 0 {
c = append(c, &Tab{})
}
if k != "" {
c = append(c, &Text{
Text: k,
})
}
}
}
run := &Run{
RunProperties: &RunProperties{},
Children: c,
}
p.Children = append(p.Children, 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)
w.AddParagraph().AddPageBreaks()
para5 := w.AddParagraph().Justification("center")
// add text
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":
child = &Tab{}
case "br":
child = &BarterRabbet{}
var brvalue BarterRabbet
err = d.DecodeElement(&brvalue, &tt)
if err != nil {
return nil, err
}
child = &brvalue
case "AlternateContent":
/*var value AlternateContent
value.file = r.file

View File

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