Skip to content

Commit d744239

Browse files
mabiao0525fumiama
andauthored
feat: A3PageSize (#22)
* feat: A3PageSize * feat: A3PageSize2 * feat: A3PageSize3 * feat: A3PageSize3 * feat: A3PageSize4 * Update structsect.go --------- Co-authored-by: 源文雨 <[email protected]>
1 parent f875425 commit d744239

File tree

6 files changed

+140
-15
lines changed

6 files changed

+140
-15
lines changed

cmd/main/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func main() {
4646
if !*analyzeOnly {
4747
fmt.Printf("Preparing new document to write at %s\n", *fileLocation)
4848

49-
w = docx.New().WithDefaultTheme()
49+
w = docx.New().WithDefaultTheme().WithA4Page()
5050
// add new paragraph
5151
para1 := w.AddParagraph().Justification("distribute")
5252
r, err := para1.AddAnchorDrawingFrom("testdata/fumiama.JPG")
@@ -254,7 +254,7 @@ func main() {
254254
if err != nil {
255255
panic(err)
256256
}
257-
newFile := docx.New().WithDefaultTheme()
257+
newFile := docx.New().WithDefaultTheme().WithA4Page()
258258
for i := 0; i < int(*dupnum); i++ {
259259
newFile.AppendFile(doc)
260260
}

pack.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (f *Docx) pack(zipWriter *zip.Writer) (err error) {
3636

3737
if f.template != "" {
3838
for _, name := range f.tmpfslst {
39-
files[name], err = TemplateXMLFS.Open("xml/" + f.template + "/" + name)
39+
files[name], err = f.tmplfs.Open("xml/" + f.template + "/" + name)
4040
if err != nil {
4141
return
4242
}

structdoc.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,13 @@ func (b *Body) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
9090
return err
9191
}
9292
b.Items = append(b.Items, &value)
93+
case "sectPr":
94+
var value SectPr
95+
err = d.DecodeElement(&value, &tt)
96+
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
97+
return err
98+
}
99+
b.Items = append(b.Items, &value)
93100
default:
94101
err = d.Skip() // skip unsupported tags
95102
if err != nil {

structdoc_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func TestUnmarshalPlainStructure(t *testing.T) {
3333
content string
3434
numParagraphs int
3535
}{
36-
{decoded_doc_1, 5},
37-
{decoded_doc_2, 14},
36+
{decoded_doc_1, 6},
37+
{decoded_doc_2, 15},
3838
}
3939
for _, tc := range testCases {
4040
doc := Document{
@@ -50,16 +50,22 @@ func TestUnmarshalPlainStructure(t *testing.T) {
5050
t.Fatalf("We expected %d paragraphs, we got %d", tc.numParagraphs, len(doc.Body.Items))
5151
}
5252
for i, it := range doc.Body.Items {
53-
p := it.(*Paragraph)
54-
if len(p.Children) == 0 {
55-
t.Fatalf("We were not able to parse paragraph %d", i)
56-
}
57-
for _, child := range p.Children {
58-
if child == nil {
59-
t.Fatalf("There are Paragraph children with all fields nil")
53+
switch v := it.(type) {
54+
case *Paragraph:
55+
if len(v.Children) == 0 {
56+
t.Fatalf("We were not able to parse paragraph %d", i)
57+
}
58+
for _, child := range v.Children {
59+
if child == nil {
60+
t.Fatalf("There are Paragraph children with all fields nil")
61+
}
62+
if o, ok := child.(*Hyperlink); ok && o.ID == "" {
63+
t.Fatalf("We have a link without ID")
64+
}
6065
}
61-
if o, ok := child.(*Hyperlink); ok && o.ID == "" {
62-
t.Fatalf("We have a link without ID")
66+
case *SectPr:
67+
if v.PgSz.W.Value != "11906" || v.PgSz.H.Value != "16838" {
68+
t.Fatalf("We were not able to parse sectPr")
6369
}
6470
}
6571
}

structsect.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Copyright (c) 2024 mabiao0525 (马飚)
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU Affero General Public License as published
6+
by the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Affero General Public License for more details.
13+
14+
You should have received a copy of the GNU Affero General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package docx
19+
20+
import (
21+
"encoding/xml"
22+
"io"
23+
"strings"
24+
)
25+
26+
// SectPr show the properties of the document, like paper size
27+
type SectPr struct {
28+
XMLName xml.Name `xml:"w:sectPr,omitempty"` // properties of the document, including paper size
29+
PgSz *PgSz `xml:"w:pgSz,omitempty"`
30+
}
31+
32+
// PgSz show the paper size
33+
type PgSz struct {
34+
W xml.Attr `xml:"w:w,attr"` // width of paper
35+
H xml.Attr `xml:"w:h,attr"` // high of paper
36+
}
37+
38+
// UnmarshalXML ...
39+
func (sect *SectPr) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
40+
for {
41+
t, err := d.Token()
42+
if err == io.EOF {
43+
break
44+
}
45+
if err != nil {
46+
return err
47+
}
48+
if tt, ok := t.(xml.StartElement); ok {
49+
switch tt.Name.Local {
50+
case "pgSz":
51+
var value PgSz
52+
err = d.DecodeElement(&value, &tt)
53+
if err != nil && !strings.HasPrefix(err.Error(), "expected") {
54+
return err
55+
}
56+
sect.PgSz = &value
57+
default:
58+
err = d.Skip() // skip unsupported tags
59+
if err != nil {
60+
return err
61+
}
62+
}
63+
}
64+
}
65+
return nil
66+
}
67+
68+
// UnmarshalXML ...
69+
func (pgsz *PgSz) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
70+
var err error
71+
72+
for _, attr := range start.Attr {
73+
switch attr.Name.Local {
74+
case "w":
75+
pgsz.W = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
76+
case "h":
77+
pgsz.H = xml.Attr{Name: xml.Name{Local: "w:w"}, Value: attr.Value}
78+
default:
79+
//ignore other attributes now
80+
}
81+
}
82+
// Consume the end element
83+
_, err = d.Token()
84+
return err
85+
}

theme.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020

2121
package docx
2222

23-
import "io/fs"
23+
import (
24+
"encoding/xml"
25+
"io/fs"
26+
)
2427

2528
// UseTemplate will replace template files
2629
func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) *Docx {
@@ -34,3 +37,27 @@ func (f *Docx) UseTemplate(template string, tmpfslst []string, tmplfs fs.FS) *Do
3437
func (f *Docx) WithDefaultTheme() *Docx {
3538
return f.UseTemplate("default", DefaultTemplateFilesList, TemplateXMLFS)
3639
}
40+
41+
// WithA3Page use A3 PageSize
42+
func (f *Docx) WithA3Page() *Docx {
43+
sectpr := &SectPr{
44+
PgSz: &PgSz{
45+
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "16838"},
46+
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "23811"},
47+
},
48+
}
49+
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
50+
return f
51+
}
52+
53+
// WithA4Page use A4 PageSize
54+
func (f *Docx) WithA4Page() *Docx {
55+
sectpr := &SectPr{
56+
PgSz: &PgSz{
57+
W: xml.Attr{Name: xml.Name{Local: "w:w"}, Value: "11906"},
58+
H: xml.Attr{Name: xml.Name{Local: "w:h"}, Value: "16838"},
59+
},
60+
}
61+
f.Document.Body.Items = append(f.Document.Body.Items, sectpr)
62+
return f
63+
}

0 commit comments

Comments
 (0)