-
Notifications
You must be signed in to change notification settings - Fork 0
/
outlines_obj.go
115 lines (101 loc) · 2.55 KB
/
outlines_obj.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
package gopdf
import (
"bytes"
"encoding/binary"
"fmt"
"unicode/utf16"
)
type outlinesObj struct {
buffer bytes.Buffer
getRoot func() *GoPdf
FirstId int
LastId int
innerIndex int
}
func (ol *outlinesObj) init(funcGetRoot func() *GoPdf) {
ol.getRoot = funcGetRoot
}
func (ol *outlinesObj) build(objID int) error {
ol.buffer.WriteString("<< /Type /" + ol.getType())
ol.buffer.WriteString(fmt.Sprintf(" /First %d 0 R /Last %d 0 R ", ol.FirstId, ol.LastId))
ol.buffer.WriteString(">>\n")
return nil
}
func (ol *outlinesObj) getType() string {
return "Outlines"
}
func (ol *outlinesObj) getObjBuff() *bytes.Buffer {
return &(ol.buffer)
}
type OutlineObj struct {
ParentId int
PrevId int
NextId int
FirstId int
LastId int
PageId int
ActionId int
Title string
buffer bytes.Buffer
}
func convertUTF16ToBigEndianBytes(arr []uint16) []byte {
buf := make([]byte, 2*len(arr))
for idx, val := range arr {
binary.BigEndian.PutUint16(buf[idx*2:], val)
}
return buf
}
func (ol *OutlineObj) init(funcGetRoot func() *GoPdf) {}
func (ol *OutlineObj) build(objID int) error {
ol.buffer.WriteString("<< /Title (")
u16Arr := utf16.Encode([]rune(ol.Title))
ol.buffer.Write([]byte{0xfe, 0xff})
ol.buffer.Write(convertUTF16ToBigEndianBytes(u16Arr))
ol.buffer.WriteString(") ")
if ol.ParentId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/Parent %d 0 R ", ol.ParentId))
}
if ol.PrevId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/Prev %d 0 R ", ol.PrevId))
}
if ol.NextId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/Next %d 0 R ", ol.NextId))
}
if ol.FirstId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/First %d 0 R ", ol.FirstId))
}
if ol.LastId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/Last %d 0 R ", ol.LastId))
}
if ol.ActionId > 0 {
ol.buffer.WriteString(fmt.Sprintf("/A %d 0 R ", ol.ActionId))
} else {
ol.buffer.WriteString(fmt.Sprintf("/Dest [ %d 0 R ] ", ol.PageId))
}
ol.buffer.WriteString("/F 0 >>\n")
return nil
}
func (ol *OutlineObj) getType() string {
return "OutlineItem"
}
func (ol *OutlineObj) getObjBuff() *bytes.Buffer {
return &(ol.buffer)
}
type actionObj struct {
buffer bytes.Buffer
PageId int
YOffset float64
}
func (a *actionObj) init(funcGetRoot func() *GoPdf) {
}
func (a *actionObj) build(objID int) error {
a.buffer.WriteString("<< /S /GoTo /Type /" + a.getType())
a.buffer.WriteString(fmt.Sprintf(" /D [%d 0 R /XYZ null %f 0.0] >>\n", a.PageId, a.YOffset))
return nil
}
func (a *actionObj) getType() string {
return "Action"
}
func (a *actionObj) getObjBuff() *bytes.Buffer {
return &(a.buffer)
}