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

optimize(pgSz): use explicit type int instaed of xml.Attr

This commit is contained in:
源文雨
2024-08-23 11:43:24 +08:00
parent 38ee4fb790
commit 51eca30836
2 changed files with 15 additions and 9 deletions

View File

@@ -20,6 +20,7 @@ package docx
import (
"encoding/xml"
"io"
"strconv"
"strings"
)
@@ -31,8 +32,8 @@ type SectPr struct {
// PgSz show the paper size
type PgSz struct {
W xml.Attr `xml:"w:w,attr"` // width of paper
H xml.Attr `xml:"w:h,attr"` // high of paper
W int `xml:"w:w,attr"` // width of paper
H int `xml:"w:h,attr"` // high of paper
}
// UnmarshalXML ...
@@ -72,9 +73,15 @@ func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
for _, attr := range start.Attr {
switch attr.Name.Local {
case "w":
pgsz.W = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
pgsz.W, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
case "h":
pgsz.H = xml.Attr{Name: xml.Name{Local: "w:h"}, Value: attr.Value}
pgsz.H, err = strconv.Atoi(attr.Value)
if err != nil {
return err
}
default:
// ignore other attributes now
}

View File

@@ -21,7 +21,6 @@
package docx
import (
"encoding/xml"
"io/fs"
)
@@ -42,8 +41,8 @@ func (f *Docx) WithDefaultTheme() *Docx {
func (f *Docx) WithA3Page() *Docx {
sectpr := &SectPr{
PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "16838"},
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "23811"},
W: 16838,
H: 23811,
},
}
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
@@ -54,8 +53,8 @@ func (f *Docx) WithA3Page() *Docx {
func (f *Docx) WithA4Page() *Docx {
sectpr := &SectPr{
PgSz: &PgSz{
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "11906"},
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "16838"},
W: 11906,
H: 16838,
},
}
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)