Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加页面宽度高度、字体设置,增加序号等 #44

Closed
wants to merge 17 commits into from
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/go-docx.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 21 additions & 4 deletions apirun.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ func (r *Run) Size(size string) *Run {
return r
}

// SizeCs allows to set run size
func (r *Run) SizeCs(size string) *Run {
r.RunProperties.SizeCs = &SizeCs{
Val: size,
}
return r
}

// Spacing allows to set run spacing
func (r *Run) Spacing(line int) *Run {
r.RunProperties.Spacing = &Spacing{
Line: line,
}
return r
}

// Shade allows to set run shade
func (r *Run) Shade(val, color, fill string) *Run {
r.RunProperties.Shade = &Shade{
Expand Down Expand Up @@ -100,11 +116,12 @@ func (r *Run) AddTab() *Run {
}

// Font sets the font of the run
func (r *Run) Font(ascii, hansi, hint string) *Run {
func (r *Run) Font(ascii, eastAsia, hansi, hint string) *Run {
r.RunProperties.Fonts = &RunFonts{
ASCII: ascii,
HAnsi: hansi,
Hint: hint,
ASCII: ascii,
EastAsia: eastAsia,
HAnsi: hansi,
Hint: hint,
}
return r
}
6 changes: 3 additions & 3 deletions cmd/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"strconv"
"strings"

"github.com/fumiama/go-docx"
"github.com/l0g1n/go-docx"
l0g1n marked this conversation as resolved.
Show resolved Hide resolved
)

func main() {
Expand Down Expand Up @@ -66,11 +66,11 @@ func main() {
para1.AddText("italic").Italic().AddTab()
para1.AddText("underline").Underline("double").AddTab()
para1.AddText("highlight").Highlight("yellow").AddTab()
para1.AddText("font").Font("Consolas", "", "cs").AddTab()
para1.AddText("font").Font("Consolas", "", "", "cs").AddTab()

para2 := w.AddParagraph().Justification("end")
para2.AddText("test all font attrs").
Size("44").Color("ff0000").Font("Consolas", "", "cs").
Size("44").Color("ff0000").Font("Consolas", "", "", "cs").
Shade("clear", "auto", "E7E6E6").
Bold().Italic().Underline("wave").
Highlight("yellow")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/fumiama/go-docx
module github.com/l0g1n/go-docx
l0g1n marked this conversation as resolved.
Show resolved Hide resolved

go 1.20

Expand Down
10 changes: 10 additions & 0 deletions structpara.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package docx

import (
"encoding/xml"
"fmt"
"io"
"reflect"
"strings"
Expand All @@ -32,6 +33,7 @@ type ParagraphProperties struct {
XMLName xml.Name `xml:"w:pPr,omitempty"`
Tabs *Tabs
Spacing *Spacing
NumProperties *NumProperties
Ind *Ind
Justification *Justification
Shade *Shade
Expand All @@ -57,6 +59,7 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
return err
}
if tt, ok := t.(xml.StartElement); ok {
fmt.Printf("tt.Name.Local: %s\n", tt.Name.Local)
l0g1n marked this conversation as resolved.
Show resolved Hide resolved
switch tt.Name.Local {
case "tabs":
var value Tabs
Expand Down Expand Up @@ -108,6 +111,13 @@ func (p *ParagraphProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) e
p.RunProperties = &value
case "pStyle":
p.Style = &Style{Val: getAtt(tt.Attr, "val")}
case "numPr":
var value NumProperties
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
p.NumProperties = &value
case "textAlignment":
p.TextAlignment = &TextAlignment{Val: getAtt(tt.Attr, "val")}
case "adjustRightInd":
Expand Down
49 changes: 49 additions & 0 deletions structrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,55 @@ type RunProperties struct {
Strike *Strike
}

type NumProperties struct {
XMLName xml.Name `xml:"w:numPr,omitempty"`
NumId *NumId
Ilvl *Ilevel
}

type NumId struct {
XMLName xml.Name `xml:"w:numId,omitempty"`
Val string `xml:"w:val,attr"`
}

type Ilevel struct {
XMLName xml.Name `xml:"w:ilvl,omitempty"`
Val string `xml:"w:val,attr"`
}

func (n *NumProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
t, err := d.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}

if tt, ok := t.(xml.StartElement); ok {
switch tt.Name.Local {
case "numId":
var value NumId
value.Val = getAtt(tt.Attr, "val")
n.NumId = &value
case "ilvl":
var value Ilevel
value.Val = getAtt(tt.Attr, "val")
n.Ilvl = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
return err
}
continue
}
}
}

return nil
}

// UnmarshalXML ...
func (r *RunProperties) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand Down
132 changes: 132 additions & 0 deletions structsect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ import (
type SectPr struct {
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
PgSz *PgSz `xml:"w:pgSz,omitempty"`
PgMar *PgMar `xml:"w:pgMar,omitempty"`
Cols *Cols `xml:"w:cols,omitempty"`
DocGrid *DocGrid `xml:"w:docGrid,omitempty"`
}

// PgSz show the paper size
Expand All @@ -36,6 +39,25 @@ type PgSz struct {
H int `xml:"w:h,attr"` // high of paper
}

type PgMar struct {
Top int `xml:"w:top,attr"`
Left int `xml:"w:left,attr"`
Bottom int `xml:"w:bottom,attr"`
Right int `xml:"w:right,attr"`
Header int `xml:"w:header,attr"`
Footer int `xml:"w:footer,attr"`
Gutter int `xml:"w:gutter,attr"`
}

type Cols struct {
Space int `xml:"w:space,attr"`
}

type DocGrid struct {
Type string `xml:"w:type,attr"`
LinePitch int `xml:"w:linePitch,attr"`
}

// UnmarshalXML ...
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
for {
Expand All @@ -55,6 +77,27 @@ func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
return err
}
sect.PgSz = &value
case "pgMar":
var value PgMar
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.PgMar = &value
case "cols":
var value Cols
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.Cols = &value
case "docGrid":
var value DocGrid
err = d.DecodeElement(&value, &tt)
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
return err
}
sect.DocGrid = &value
default:
err = d.Skip() // skip unsupported tags
if err != nil {
Expand Down Expand Up @@ -90,3 +133,92 @@ func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
_, err = d.Token()
return err
}

func (pgmar *PgMar) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "top":
pgmar.Top, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "left":
pgmar.Left, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "bottom":
pgmar.Bottom, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "right":
pgmar.Right, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "header":
pgmar.Header, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "footer":
pgmar.Footer, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "gutter":
pgmar.Gutter, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (cols *Cols) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "space":
cols.Space, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}

func (dg *DocGrid) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error

for _, attr := range start.Attr {
switch attr.Name.Local {
case "linePitch":
dg.LinePitch, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "type":
dg.Type = attr.Value
default:
// ignore other attributes now
}
}
// Consume the end element
_, err = d.Token()
return err
}