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