Scheduled Maintenance
-
February 10, 2026, from 8:00 AM to 9:00 AM PST. Account-related operations might be temporarily unavailable.

Bookmarks

Sample Java code to use Apryse SDK for programmatically reading and editing existing outline items, and for creating new PDF bookmarks using the high-level API. Learn more about our Android SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2019 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6package com.pdftron.android.pdfnetsdksamples.samples;
7
8import com.pdftron.android.pdfnetsdksamples.OutputListener;
9import com.pdftron.android.pdfnetsdksamples.PDFNetSample;
10import com.pdftron.android.pdfnetsdksamples.R;
11import com.pdftron.android.pdfnetsdksamples.util.Utils;
12import com.pdftron.common.PDFNetException;
13import com.pdftron.pdf.Action;
14import com.pdftron.pdf.Bookmark;
15import com.pdftron.pdf.Destination;
16import com.pdftron.pdf.FileSpec;
17import com.pdftron.pdf.PDFDoc;
18import com.pdftron.pdf.Page;
19import com.pdftron.sdf.Obj;
20import com.pdftron.sdf.SDFDoc;
21
22import java.util.ArrayList;
23
24public class BookmarkTest extends PDFNetSample {
25
26 private static OutputListener mOutputListener;
27
28 private static ArrayList<String> mFileList = new ArrayList<>();
29
30 public BookmarkTest() {
31 setTitle(R.string.sample_bookmarks_title);
32 setDescription(R.string.sample_bookmarks_description);
33 }
34
35 @Override
36 public void run(OutputListener outputListener) {
37 super.run(outputListener);
38 mOutputListener = outputListener;
39 mFileList.clear();
40 printHeader(outputListener);
41
42 // The following example illustrates how to create and edit the outline tree
43 // using high-level Bookmark methods.
44 try (PDFDoc doc = new PDFDoc((Utils.getAssetTempFile(INPUT_PATH + "numbered.pdf").getAbsolutePath()))) {
45 doc.initSecurityHandler();
46
47 // Lets first create the root bookmark items.
48 Bookmark red = Bookmark.create(doc, "Red");
49 Bookmark green = Bookmark.create(doc, "Green");
50 Bookmark blue = Bookmark.create(doc, "Blue");
51
52 doc.addRootBookmark(red);
53 doc.addRootBookmark(green);
54 doc.addRootBookmark(blue);
55
56 // You can also add new root bookmarks using Bookmark.AddNext("...")
57 blue.addNext("foo");
58 blue.addNext("bar");
59
60 // We can now associate new bookmarks with page destinations:
61
62 // The following example creates an 'explicit' destination (see
63 // section '8.2.1 Destinations' in PDF Reference for more details)
64 Destination red_dest = Destination.createFit(doc.getPageIterator().next());
65 red.setAction(Action.createGoto(red_dest));
66
67 // Create an explicit destination to the first green page in the document
68 green.setAction(Action.createGoto(
69 Destination.createFit(doc.getPage(10))));
70
71 // The following example creates a 'named' destination (see
72 // section '8.2.1 Destinations' in PDF Reference for more details)
73 // Named destinations have certain advantages over explicit destinations.
74 byte[] key = {'b', 'l', 'u', 'e', '1'};
75 Action blue_action = Action.createGoto(key,
76 Destination.createFit(doc.getPage(19)));
77
78 blue.setAction(blue_action);
79
80 // We can now add children Bookmarks
81 Bookmark sub_red1 = red.addChild("Red - Page 1");
82 sub_red1.setAction(Action.createGoto(Destination.createFit(doc.getPage(1))));
83 Bookmark sub_red2 = red.addChild("Red - Page 2");
84 sub_red2.setAction(Action.createGoto(Destination.createFit(doc.getPage(2))));
85 Bookmark sub_red3 = red.addChild("Red - Page 3");
86 sub_red3.setAction(Action.createGoto(Destination.createFit(doc.getPage(3))));
87 Bookmark sub_red4 = sub_red3.addChild("Red - Page 4");
88 sub_red4.setAction(Action.createGoto(Destination.createFit(doc.getPage(4))));
89 Bookmark sub_red5 = sub_red3.addChild("Red - Page 5");
90 sub_red5.setAction(Action.createGoto(Destination.createFit(doc.getPage(5))));
91 Bookmark sub_red6 = sub_red3.addChild("Red - Page 6");
92 sub_red6.setAction(Action.createGoto(Destination.createFit(doc.getPage(6))));
93
94 // Example of how to find and delete a bookmark by title text.
95 Bookmark foo = doc.getFirstBookmark().find("foo");
96 if (foo.isValid()) {
97 foo.delete();
98 } else {
99 throw new Exception("Foo is not Valid");
100 }
101
102 Bookmark bar = doc.getFirstBookmark().find("bar");
103 if (bar.isValid()) {
104 bar.delete();
105 } else {
106 throw new Exception("Bar is not Valid");
107 }
108
109 // Adding color to Bookmarks. Color and other formatting can help readers
110 // get around more easily in large PDF documents.
111 red.setColor(1, 0, 0);
112 green.setColor(0, 1, 0);
113 green.setFlags(2); // set bold font
114 blue.setColor(0, 0, 1);
115 blue.setFlags(3); // set bold and itallic
116
117 doc.save((Utils.createExternalFile("bookmark.pdf", mFileList).getAbsolutePath()), SDFDoc.SaveMode.NO_FLAGS, null);
118 mOutputListener.println("Done. Result saved in bookmark.pdf");
119 } catch (Exception e) {
120 mOutputListener.printError(e.getStackTrace());
121 }
122
123 // The following example illustrates how to traverse the outline tree using
124 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
125 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
126 // Open the document that was saved in the previous code sample
127 try (PDFDoc doc = new PDFDoc((Utils.createExternalFile("bookmark.pdf", mFileList).getAbsolutePath()))) {
128 doc.initSecurityHandler();
129
130 Bookmark root = doc.getFirstBookmark();
131 PrintOutlineTree(root);
132 mOutputListener.println("Done.");
133 } catch (Exception e) {
134 mOutputListener.printError(e.getStackTrace());
135 }
136
137 // The following example illustrates how to create a Bookmark to a page
138 // in a remote document. A remote go-to action is similar to an ordinary
139 // go-to action, but jumps to a destination in another PDF file instead
140 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
141 // Reference Manual for details.
142 // Open the document that was saved in the previous code sample
143 try (PDFDoc doc = new PDFDoc((Utils.createExternalFile("bookmark.pdf", mFileList).getAbsolutePath()))) {
144 doc.initSecurityHandler();
145
146 // Create file specification (the file reffered to by the remote bookmark)
147 Obj file_spec = doc.createIndirectDict();
148 file_spec.putName("Type", "Filespec");
149 file_spec.putString("F", "bookmark.pdf");
150 FileSpec spec = new FileSpec(file_spec);
151 Action goto_remote = Action.createGotoRemote(spec, 5, true);
152
153 Bookmark remoteBookmark1 = Bookmark.create(doc, "REMOTE BOOKMARK 1");
154 remoteBookmark1.setAction(goto_remote);
155 doc.addRootBookmark(remoteBookmark1);
156
157 // Create another remote bootmark, but this time using the low-level SDF/Cos API.
158 // Create a remote action
159 Bookmark remoteBookmark2 = Bookmark.create(doc, "REMOTE BOOKMARK 2");
160 doc.addRootBookmark(remoteBookmark2);
161
162 Obj gotoR = remoteBookmark2.getSDFObj().putDict("A");
163 {
164 gotoR.putName("S", "GoToR"); // Set action type
165 gotoR.putBool("NewWindow", true);
166
167 // Set the file specification
168 gotoR.put("F", file_spec);
169
170 // jump to the first page. Note that pages are indexed from 0.
171 Obj dest = gotoR.putArray("D"); // Set the destination
172 dest.pushBackNumber(9);
173 dest.pushBackName("Fit");
174 }
175
176 doc.save((Utils.createExternalFile("bookmark_remote.pdf", mFileList).getAbsolutePath()), SDFDoc.SaveMode.LINEARIZED, null);
177 mOutputListener.println("Done. Result saved in bookmark_remote.pdf");
178 } catch (Exception e) {
179 mOutputListener.printError(e.getStackTrace());
180 }
181
182 for (String file : mFileList) {
183 addToFileList(file);
184 }
185 printFooter(outputListener);
186 }
187
188 static void PrintIndent(Bookmark item) throws PDFNetException {
189 int ident = item.getIndent() - 1;
190 for (int i = 0; i < ident; ++i) mOutputListener.print(" ");
191 }
192
193 // Prints out the outline tree to the standard output
194 static void PrintOutlineTree(Bookmark item) throws PDFNetException {
195 for (; item.isValid(); item = item.getNext()) {
196 PrintIndent(item);
197 mOutputListener.print((item.isOpen() ? "- " : "+ ") + item.getTitle() + " ACTION -> ");
198
199 // Print Action
200 Action action = item.getAction();
201 if (action.isValid()) {
202 if (action.getType() == Action.e_GoTo) {
203 Destination dest = action.getDest();
204 if (dest.isValid()) {
205 Page page = dest.getPage();
206 mOutputListener.println("GoTo Page #" + page.getIndex());
207 }
208 } else {
209 mOutputListener.println("Not a 'GoTo' action");
210 }
211 } else {
212 mOutputListener.println("NULL");
213 }
214
215 if (item.hasChildren()) // Recursively print children sub-trees
216 {
217 PrintOutlineTree(item.getFirstChild());
218 }
219 }
220 }
221
222}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales