Skip to content

Commit b601beb

Browse files
committed
document: support non-dirty fields
Fixes #171
1 parent bc44078 commit b601beb

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 Baliance. All rights reserved.
2+
package main
3+
4+
import (
5+
"baliance.com/gooxml/document"
6+
"baliance.com/gooxml/measurement"
7+
"baliance.com/gooxml/schema/soo/wml"
8+
)
9+
10+
var lorem = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin lobortis, lectus dictum feugiat tempus, sem neque finibus enim, sed eleifend sem nunc ac diam. Vestibulum tempus sagittis elementum`
11+
12+
func main() {
13+
doc := document.New()
14+
15+
ftr := doc.AddFooter()
16+
para := ftr.AddParagraph()
17+
para.Properties().AddTabStop(3*measurement.Inch, wml.ST_TabJcCenter, wml.ST_TabTlcNone)
18+
19+
run := para.AddRun()
20+
run.AddTab()
21+
run.AddFieldWithFormatting(document.FieldCurrentPage, "", false)
22+
run.AddText(" of ")
23+
run.AddFieldWithFormatting(document.FieldNumberOfPages, "", false)
24+
doc.BodySection().SetFooter(ftr, wml.ST_HdrFtrDefault)
25+
26+
for i := 0; i < 20; i++ {
27+
para := doc.AddParagraph()
28+
for j := 0; j < 5; j++ {
29+
run := para.AddRun()
30+
run.AddText(lorem)
31+
}
32+
}
33+
doc.SaveToFile("page-numbers.docx")
34+
}

document/run.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,14 @@ func (r Run) AddTab() {
8585

8686
// AddFieldWithFormatting adds a field (automatically computed text) to the
8787
// document with field specifc formatting.
88-
func (r Run) AddFieldWithFormatting(code string, fmt string) {
88+
func (r Run) AddFieldWithFormatting(code string, fmt string, isDirty bool) {
8989
ic := r.newIC()
9090
ic.FldChar = wml.NewCT_FldChar()
9191
ic.FldChar.FldCharTypeAttr = wml.ST_FldCharTypeBegin
92-
ic.FldChar.DirtyAttr = &sharedTypes.ST_OnOff{}
93-
ic.FldChar.DirtyAttr.Bool = gooxml.Bool(true)
92+
if isDirty {
93+
ic.FldChar.DirtyAttr = &sharedTypes.ST_OnOff{}
94+
ic.FldChar.DirtyAttr.Bool = gooxml.Bool(true)
95+
}
9496

9597
ic = r.newIC()
9698
ic.InstrText = wml.NewCT_Text()
@@ -107,7 +109,7 @@ func (r Run) AddFieldWithFormatting(code string, fmt string) {
107109

108110
// AddField adds a field (automatically computed text) to the document.
109111
func (r Run) AddField(code string) {
110-
r.AddFieldWithFormatting(code, "")
112+
r.AddFieldWithFormatting(code, "", true)
111113
}
112114

113115
// Properties returns the run properties.

0 commit comments

Comments
 (0)