From e9a89648f87242b94b8ce6037defbc3068709845 Mon Sep 17 00:00:00 2001 From: mabiao0525 <70356026+mabiao0525@users.noreply.github.com> Date: Sun, 17 Mar 2024 18:58:15 +0800 Subject: [PATCH] feat: add_pagabreaks (#21) --- apipara.go | 14 ++++++++++++++ apitext.go | 24 ++++++++++++------------ cmd/main/main.go | 1 + structrun.go | 7 ++++++- structtext.go | 3 ++- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/apipara.go b/apipara.go index 0e123ce..4ac1b89 100644 --- a/apipara.go +++ b/apipara.go @@ -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 +} diff --git a/apitext.go b/apitext.go index 3baef09..6e3b879 100644 --- a/apitext.go +++ b/apitext.go @@ -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 } diff --git a/cmd/main/main.go b/cmd/main/main.go index 9974872..02703e0 100644 --- a/cmd/main/main.go +++ b/cmd/main/main.go @@ -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") diff --git a/structrun.go b/structrun.go index 7b65895..4160595 100644 --- a/structrun.go +++ b/structrun.go @@ -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 diff --git a/structtext.go b/structtext.go index e62c78a..c3fe016 100644 --- a/structtext.go +++ b/structtext.go @@ -88,9 +88,10 @@ func (t *Tab) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { return err } -// BarterRabbet is
+// BarterRabbet is
, 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