-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathoutline.js
63 lines (52 loc) · 1.52 KB
/
outline.js
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
class PDFOutline {
constructor(document, parent, title, dest, options = { expanded: false }) {
this.document = document;
this.options = options;
this.outlineData = {};
if (dest !== null) {
this.outlineData['Dest'] = [dest.dictionary, 'Fit'];
}
if (parent !== null) {
this.outlineData['Parent'] = parent;
}
if (title !== null) {
this.outlineData['Title'] = new String(title);
}
this.dictionary = this.document.ref(this.outlineData);
this.children = [];
}
addItem(title, options = { expanded: false }) {
const result = new PDFOutline(
this.document,
this.dictionary,
title,
this.document.page,
options,
);
this.children.push(result);
return result;
}
endOutline() {
if (this.children.length > 0) {
if (this.options.expanded) {
this.outlineData.Count = this.children.length;
}
const first = this.children[0],
last = this.children[this.children.length - 1];
this.outlineData.First = first.dictionary;
this.outlineData.Last = last.dictionary;
for (let i = 0, len = this.children.length; i < len; i++) {
const child = this.children[i];
if (i > 0) {
child.outlineData.Prev = this.children[i - 1].dictionary;
}
if (i < this.children.length - 1) {
child.outlineData.Next = this.children[i + 1].dictionary;
}
child.endOutline();
}
}
return this.dictionary.end();
}
}
export default PDFOutline;