forked from ChotiwatMajor/go-docx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paragraph.go
149 lines (123 loc) · 3.57 KB
/
paragraph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package docx
import "encoding/xml"
const (
JUSTIFY_START = "start"
JUSTIFY_END = "end"
JUSTIFY_BOTH = "both"
JUSTIFY_CENTER = "center"
JUSTIFY_DISTRIBUTE = "distribute"
CM = 567
INCH = 1440
)
type Paragraph struct {
XMLName xml.Name `xml:"w:p"`
Data []interface{}
Properties *ParagraphProperties
file *File
}
type ParagraphProperties struct {
XMLName xml.Name `xml:"w:pPr"`
Data []interface{}
}
type Justification struct {
XMLName xml.Name `xml:"w:jc"`
Val string `xml:"w:val,attr"`
}
type Indentation struct {
XMLName xml.Name `xml:"w:ind"`
Left int `xml:"w:left,attr"`
Right int `xml:"w:right,attr"`
}
type Spacing struct {
XMLName xml.Name `xml:"w:spacing"`
Before int `xml:"w:before,attr"`
After int `xml:"w:after,attr"`
}
type LineBreak struct {
XMLName xml.Name `xml:"w:br"`
Type string `xml:"w:type,attr"`
}
type KeepLines struct {
XMLName xml.Name `xml:"w:keepLines"`
}
type PageBreakBefore struct {
XMLName xml.Name `xml:"w:pageBreakBefore"`
Val string `xml:"w:val,attr"`
}
// AddParagraph add new paragraph
func (f *File) AddParagraph() *Paragraph {
props := &ParagraphProperties{}
p := &Paragraph{
Data: make([]interface{}, 0),
Properties: props,
file: f,
}
p.Data = append(p.Data, props)
f.Document.Body.Content = append(f.Document.Body.Content, p)
return p
}
// AddText add text to paragraph
func (p *Paragraph) AddText(text string) *Run {
t := &Text{
Text: text,
}
run := &Run{
Text: t,
RunProperties: &RunProperties{},
}
p.Data = append(p.Data, run)
return run
}
// AddLink add hyperlink to paragraph
func (p *Paragraph) AddLink(text string, link string) *Hyperlink {
rId := p.file.addLinkRelation(link)
hyperlink := &Hyperlink{
ID: rId,
Run: Run{
RunProperties: &RunProperties{
RunStyle: &RunStyle{
Val: HYPERLINK_STYLE,
},
},
InstrText: text,
},
}
p.Data = append(p.Data, hyperlink)
return hyperlink
}
// AddNewLine adds break with type textWrapping, newline after previous
func (p *Paragraph) AddNewLine() {
p.Data = append(p.Data, &LineBreak{Type: "textWrapping"})
}
// AddNewPage adds break with type page, new page after previous
func (p *Paragraph) AddNewPage() {
p.Data = append(p.Data, &LineBreak{Type: "page"})
}
// Justification takes justificaiton type
// Possible values are in constants:
// JUSTIFY_START align to the left
// JUSTIFY_END align to the right
// JUSTIFY_BOTH align to both sides with regular spaces
// JUSTIFY_DISTRIBUTE align to both sides with wider spaces
// JUSTIFY_CENTER align to the center of line
func (prp *ParagraphProperties) Justification(justificaiton string) {
prp.Data = append(prp.Data, &Justification{Val: justificaiton})
}
// Indentation takes left and right indentation for paragraph in twips
// There is constant CM which is equal to 567 twips per centimeter
// There is constant INCH which is equal to 1440 twips per inch
func (prp *ParagraphProperties) Indentation(left, right int) {
prp.Data = append(prp.Data, &Indentation{Left: left, Right: right})
}
// Spacing sets the space before and after paragraph
func (prp *ParagraphProperties) Spacing(before, after int) {
prp.Data = append(prp.Data, &Spacing{Before: before, After: after})
}
// KeepLines prevents pagebreak within a paragraph
func (prp *ParagraphProperties) KeepLines() {
prp.Data = append(prp.Data, &KeepLines{})
}
// Define Pagebreak before paragraph
func (prp *ParagraphProperties) PageBreakBefore(val string) {
prp.Data = append(prp.Data, &PageBreakBefore{Val: val})
}