forked from unidoc/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_examples_test.go
66 lines (58 loc) · 1.42 KB
/
document_examples_test.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
package document_test
import (
"fmt"
"log"
"github.com/unidoc/unioffice/document"
)
func ExampleNew() {
doc := document.New()
doc.AddParagraph().AddRun().AddText("Hello World!")
doc.SaveToFile("document.docx")
}
func ExampleOpen() {
doc, err := document.Open("existing.docx")
if err != nil {
log.Fatalf("error opening document: %s", err)
}
for _, para := range doc.Paragraphs() {
for _, run := range para.Runs() {
fmt.Print(run.Text())
}
fmt.Println()
}
}
func ExampleOpenTemplate() {
doc, err := document.OpenTemplate("existing.docx")
if err != nil {
log.Fatalf("error opening document template: %s", err)
}
para := doc.AddParagraph()
para.SetStyle("Title")
para.AddRun().AddText("My Document Title")
para = doc.AddParagraph()
para.SetStyle("Subtitle")
para.AddRun().AddText("Document Subtitle")
para = doc.AddParagraph()
para.SetStyle("Heading1")
para.AddRun().AddText("Major Section")
doc.SaveToFile("ouput.docx")
}
func ExampleDocument_FormFields() {
doc, err := document.Open("invitation.docx")
if err != nil {
log.Fatalf("error opening document form: %s", err)
}
for _, field := range doc.FormFields() {
switch field.Name() {
case "attendingEvent":
if field.Type() == document.FormFieldTypeCheckBox {
field.SetChecked(true)
}
case "name":
if field.Type() == document.FormFieldTypeText {
field.SetValue("John Smith")
}
}
}
doc.SaveToFile("invitation-respoonse.docx")
}