-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (97 loc) · 2.45 KB
/
main.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
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package main
import (
"fmt"
"log"
"os"
"strings"
"seehuhn.de/go/pdf"
"seehuhn.de/go/pdf/outline"
"seehuhn.de/go/pdf/pagetree"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("usage: show-outline FILENAME")
}
fname := os.Args[1]
doc, err := pdf.Open(fname, nil)
if err != nil {
log.Fatal(err)
}
tree, err := outline.Read(doc)
if err != nil {
log.Fatal(err)
} else if tree == nil {
fmt.Println("no document outline")
return
}
// construct a map of page dictionary objects to page numbers
pageNumbers := map[pdf.Reference]int{}
iter := pagetree.NewIterator(doc).All()
pageNo := 0
iter(func(ref pdf.Reference, _ pdf.Dict) bool {
pageNumbers[ref] = pageNo
pageNo++
return true
})
// print the tree
err = showTree(doc, pageNumbers, tree, "")
if err != nil {
log.Fatal(err)
}
err = doc.Close()
if err != nil {
log.Fatal(err)
}
}
func showTree(doc pdf.Getter, pageNumbers map[pdf.Reference]int, tree *outline.Tree, indent string) error {
var pageLabel string
s, err := pdf.GetName(doc, tree.Action["S"])
if err != nil {
return err
}
if s == "GoTo" {
dest, err := pdf.Resolve(doc, tree.Action["D"])
if err != nil {
return err
}
switch dest := dest.(type) {
case pdf.Array:
if len(dest) > 0 {
ref, ok := dest[0].(pdf.Reference)
if ok {
if n, ok := pageNumbers[ref]; ok {
pageLabel = fmt.Sprintf(" page %d", n+1)
}
}
}
}
}
line := indent + tree.Title
if pageLabel != "" {
rep := max(70-len(line), 3)
line += " " + strings.Repeat(".", rep) + pageLabel
}
fmt.Println(line)
for _, child := range tree.Children {
err = showTree(doc, pageNumbers, child, indent+" ")
if err != nil {
return err
}
}
return nil
}