PDF Bookmarks and Outlines - Add/Edit/Read - Go Sample Code

Sample code to use Apryse SDK for programmatically reading and editing existing outline items, and for creating new PDF bookmarks using the high-level API. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 "os"
11 . "pdftron"
12)
13
14import "pdftron/Samples/LicenseKey/GO"
15
16//-----------------------------------------------------------------------------------------
17// The sample code illustrates how to read and edit existing outline items and create
18// new bookmarks using the high-level API.
19//-----------------------------------------------------------------------------------------
20
21// Relattive path to the folder containing the test files.
22var inputPath = "../../TestFiles/"
23var outputPath = "../../TestFiles/Output/"
24
25func PrintIndent(item Bookmark){
26 indent := item.GetIndent() - 1
27 i := 0
28 for i < indent{
29 os.Stdout.Write([]byte(" "))
30 i = i + 1
31 }
32}
33
34// Prints out the outline tree to the standard output
35func PrintOutlineTree (item Bookmark){
36 for item.IsValid(){
37 PrintIndent(item)
38
39 if item.IsOpen(){
40 os.Stdout.Write([]byte("- " + item.GetTitle() + " ACTION -> "))
41 }else{
42 os.Stdout.Write([]byte("+ " + item.GetTitle() + " ACTION -> "))
43 }
44
45 // Print Action
46 action := item.GetAction()
47 if action.IsValid(){
48 if action.GetType() == ActionE_GoTo{
49 dest := action.GetDest()
50 if dest.IsValid(){
51 page := dest.GetPage()
52 fmt.Println("GoTo Page //" + strconv.Itoa(page.GetIndex()))
53 }
54 }else{
55 fmt.Println("Not a 'GoTo' action")
56 }
57 }else{
58 fmt.Println("NULL")
59 }
60 // Recursively print children sub-trees
61 if item.HasChildren(){
62 PrintOutlineTree(item.GetFirstChild())
63 }
64 item = item.GetNext()
65 }
66}
67
68func main(){
69 PDFNetInitialize(PDFTronLicense.Key)
70
71 // The following example illustrates how to create and edit the outline tree
72 // using high-level Bookmark methods.
73
74 doc := NewPDFDoc(inputPath + "numbered.pdf")
75 doc.InitSecurityHandler()
76
77 // Lets first create the root bookmark items.
78 red := BookmarkCreate(doc, "Red")
79 green := BookmarkCreate(doc, "Green")
80 blue := BookmarkCreate(doc, "Blue")
81
82 doc.AddRootBookmark(red)
83 doc.AddRootBookmark(green)
84 doc.AddRootBookmark(blue)
85
86 // You can also add new root bookmarks using Bookmark.AddNext("...")
87 blue.AddNext("foo")
88 blue.AddNext("bar")
89
90 // We can now associate new bookmarks with page destinations:
91
92 // The following example creates an 'explicit' destination (see
93 // section '8.2.1 Destinations' in PDF Reference for more details)
94 itr := doc.GetPageIterator()
95 redDest := DestinationCreateFit(itr.Current())
96 red.SetAction(ActionCreateGoto(redDest))
97
98 // Create an explicit destination to the first green page in the document
99 green.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(10))))
100
101 // The following example creates a 'named' destination (see
102 // section '8.2.1 Destinations' in PDF Reference for more details)
103 // Named destinations have certain advantages over explicit destinations.
104 key := []byte("blue1")
105 blueAction := ActionCreateGoto(&key[0], 2, DestinationCreateFit(doc.GetPage(19)))
106
107 blue.SetAction(blueAction)
108
109 // We can now add children Bookmarks
110 subRed1 := red.AddChild("Red - Page 1")
111 subRed1.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(1))))
112 subRed2 := red.AddChild("Red - Page 2")
113 subRed2.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(2))))
114 subRed3 := red.AddChild("Red - Page 3")
115 subRed3.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(3))))
116 subRed4 := subRed3.AddChild("Red - Page 4")
117 subRed4.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(4))))
118 subRed5 := subRed3.AddChild("Red - Page 5")
119 subRed5.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(5))))
120 subRed6 := subRed3.AddChild("Red - Page 6")
121 subRed6.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(6))))
122
123 // Example of how to find and delete a bookmark by title text.
124 foo := doc.GetFirstBookmark().Find("foo")
125 if foo.IsValid(){
126 foo.Delete()
127 }else{
128 panic("Foo is not Valid")
129 }
130 bar := doc.GetFirstBookmark().Find("bar")
131 if bar.IsValid(){
132 bar.Delete()
133 }else{
134 panic("Bar is not Valid")
135 }
136 // Adding color to Bookmarks. Color and other formatting can help readers
137 // get around more easily in large PDF documents.
138 red.SetColor(1.0, 0.0, 0.0);
139 green.SetColor(0.0, 1.0, 0.0);
140 green.SetFlags(2); // set bold font
141 blue.SetColor(0.0, 0.0, 1.0);
142 blue.SetFlags(3); // set bold and itallic
143
144 doc.Save(outputPath + "bookmark.pdf", uint(0))
145 doc.Close()
146 fmt.Println("Done. Result saved in bookmark.pdf")
147
148 // The following example illustrates how to traverse the outline tree using
149 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
150 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
151
152 // Open the document that was saved in the previous code sample
153 doc = NewPDFDoc(outputPath + "bookmark.pdf")
154 doc.InitSecurityHandler()
155
156 root := doc.GetFirstBookmark()
157 PrintOutlineTree(root)
158
159 doc.Close()
160 fmt.Println("Done.")
161
162 // The following example illustrates how to create a Bookmark to a page
163 // in a remote document. A remote go-to action is similar to an ordinary
164 // go-to action, but jumps to a destination in another PDF file instead
165 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
166 // Reference Manual for details.
167
168 doc = NewPDFDoc(outputPath + "bookmark.pdf")
169 doc.InitSecurityHandler()
170
171 // Create file specification (the file reffered to by the remote bookmark)
172 fileSpec := doc.CreateIndirectDict()
173 fileSpec.PutName("Type", "Filespec")
174 fileSpec.PutString("F", "bookmark.pdf")
175 spec := NewFileSpec(fileSpec)
176 gotoRemote := ActionCreateGotoRemote(spec, 5, true)
177
178 remoteBookmark1 := BookmarkCreate(doc, "REMOTE BOOKMARK 1")
179 remoteBookmark1.SetAction(gotoRemote)
180 doc.AddRootBookmark(remoteBookmark1)
181
182 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
183 // Create a remote action
184 remoteBookmark2 := BookmarkCreate(doc, "REMOTE BOOKMARK 2")
185 doc.AddRootBookmark(remoteBookmark2)
186
187 gotoR := remoteBookmark2.GetSDFObj().PutDict("A")
188 gotoR.PutName("S","GoToR") // Set action type
189 gotoR.PutBool("NewWindow", true)
190
191 // Set the file specification
192 gotoR.Put("F", fileSpec)
193
194 // jump to the first page. Note that pages are indexed from 0.
195 dest := gotoR.PutArray("D") // Set the destination
196 dest.PushBackNumber(9);
197 dest.PushBackName("Fit");
198
199 doc.Save(outputPath + "bookmark_remote.pdf", uint(SDFDocE_linearized))
200 doc.Close()
201 PDFNetTerminate()
202 fmt.Println("Done. Result saved in bookmark_remote.pdf")
203}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales