Bookmarks

Sample C# 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 Xamarin SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6using System;
7using pdftron;
8using pdftron.Common;
9using pdftron.Filters;
10using pdftron.SDF;
11using pdftron.PDF;
12
13using NUnit.Framework;
14
15namespace MiscellaneousSamples
16{
17 /// <summary>
18 //-----------------------------------------------------------------------------------------
19 // The sample code illustrates how to read, write, and edit existing outline items
20 // and create new bookmarks using both the high-level and the SDF/Cos API.
21 //-----------------------------------------------------------------------------------------
22 /// </summary>
23 [TestFixture]
24 public class BookmarkTest
25 {
26
27 static void PrintIndent(Bookmark item)
28 {
29 int indent = item.GetIndent() - 1;
30 for (int i = 0; i < indent; ++i)
31 Console.Write(" ");
32 }
33
34 // Prints out the outline tree to the standard output
35 static void PrintOutlineTree(Bookmark item)
36 {
37 for (; item.IsValid(); item = item.GetNext())
38 {
39 PrintIndent(item);
40 Console.Write("{0:s}{1:s} ACTION -> ", (item.IsOpen() ? "- " : "+ "), item.GetTitle());
41
42 // Print Action
43 pdftron.PDF.Action action = item.GetAction();
44 if (action.IsValid())
45 {
46 if (action.GetType() == pdftron.PDF.Action.Type.e_GoTo)
47 {
48 Destination dest = action.GetDest();
49 if (dest.IsValid())
50 {
51 Page page = dest.GetPage();
52 Console.WriteLine("GoTo Page #{0:d}", page.GetIndex());
53 }
54 }
55 else
56 {
57 Console.WriteLine("Not a 'GoTo' action");
58 }
59 }
60 else
61 {
62 Console.WriteLine("NULL");
63 }
64
65 if (item.HasChildren()) // Recursively print children sub-trees
66 {
67 PrintOutlineTree(item.GetFirstChild());
68 }
69 }
70 }
71
72 /// <summary>
73 /// The main entry point for the application.
74 /// </summary>
75 [Test]
76 public static void Sample()
77 {
78
79 // Relative path to the folder containing test files.
80 const string input_path = "TestFiles/";
81
82
83 // The following example illustrates how to create and edit the outline tree
84 // using high-level Bookmark methods.
85 try
86 {
87 using (PDFDoc doc = new PDFDoc(Utils.GetAssetTempFile(input_path + "numbered.pdf")))
88 {
89 doc.InitSecurityHandler();
90
91 // Lets first create the root bookmark items.
92 Bookmark red = Bookmark.Create(doc, "Red");
93 Bookmark green = Bookmark.Create(doc, "Green");
94 Bookmark blue = Bookmark.Create(doc, "Blue");
95
96 doc.AddRootBookmark(red);
97 doc.AddRootBookmark(green);
98 doc.AddRootBookmark(blue);
99
100 // You can also add new root bookmarks using Bookmark.AddNext("...")
101 blue.AddNext("foo");
102 blue.AddNext("bar");
103
104 // We can now associate new bookmarks with page destinations:
105
106 // The following example creates an 'explicit' destination (see
107 // section '8.2.1 Destinations' in PDF Reference for more details)
108 Destination red_dest = Destination.CreateFit(doc.GetPage(1));
109 red.SetAction(pdftron.PDF.Action.CreateGoto(red_dest));
110
111 // Create an explicit destination to the first green page in the document
112 green.SetAction(pdftron.PDF.Action.CreateGoto(
113 Destination.CreateFit(doc.GetPage(10))));
114
115 // The following example creates a 'named' destination (see
116 // section '8.2.1 Destinations' in PDF Reference for more details)
117 // Named destinations have certain advantages over explicit destinations.
118 String key = "blue1";
119 pdftron.PDF.Action blue_action = pdftron.PDF.Action.CreateGoto(key,
120 Destination.CreateFit(doc.GetPage(19)));
121
122 blue.SetAction(blue_action);
123
124 // We can now add children Bookmarks
125 Bookmark sub_red1 = red.AddChild("Red - Page 1");
126 sub_red1.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))));
127 Bookmark sub_red2 = red.AddChild("Red - Page 2");
128 sub_red2.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(2))));
129 Bookmark sub_red3 = red.AddChild("Red - Page 3");
130 sub_red3.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(3))));
131 Bookmark sub_red4 = sub_red3.AddChild("Red - Page 4");
132 sub_red4.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(4))));
133 Bookmark sub_red5 = sub_red3.AddChild("Red - Page 5");
134 sub_red5.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(5))));
135 Bookmark sub_red6 = sub_red3.AddChild("Red - Page 6");
136 sub_red6.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(6))));
137
138 // Example of how to find and delete a bookmark by title text.
139 Bookmark foo = doc.GetFirstBookmark().Find("foo");
140 if (foo.IsValid())
141 {
142 foo.Delete();
143 }
144
145 Bookmark bar = doc.GetFirstBookmark().Find("bar");
146 if (bar.IsValid())
147 {
148 bar.Delete();
149 }
150
151 // Adding color to Bookmarks. Color and other formatting can help readers
152 // get around more easily in large PDF documents.
153 red.SetColor(1, 0, 0);
154 green.SetColor(0, 1, 0);
155 green.SetFlags(2); // set bold font
156 blue.SetColor(0, 0, 1);
157 blue.SetFlags(3); // set bold and italic
158
159 doc.Save(Utils.CreateExternalFile("bookmark.pdf"), 0);
160 Console.WriteLine("Done. Result saved in bookmark.pdf");
161 }
162 }
163 catch (PDFNetException e)
164 {
165 Console.WriteLine(e.Message);
166 Assert.True(false);
167 }
168
169
170 // The following example illustrates how to traverse the outline tree using
171 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
172 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
173 try
174 {
175 // Open the document that was saved in the previous code sample
176 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("bookmark.pdf")))
177 {
178 doc.InitSecurityHandler();
179
180 Bookmark root = doc.GetFirstBookmark();
181 PrintOutlineTree(root);
182
183 Console.WriteLine("Done.");
184 }
185 }
186 catch (PDFNetException e)
187 {
188 Console.WriteLine(e.Message);
189 Assert.True(false);
190 }
191
192 // The following example illustrates how to create a Bookmark to a page
193 // in a remote document. A remote go-to action is similar to an ordinary
194 // go-to action, but jumps to a destination in another PDF file instead
195 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
196 // Reference Manual for details.
197 try
198 {
199 using (PDFDoc doc = new PDFDoc(Utils.CreateExternalFile("bookmark.pdf")))
200 {
201 doc.InitSecurityHandler();
202
203 // Create file specification (the file referred to by the remote bookmark)
204 Obj file_spec = doc.CreateIndirectDict();
205 file_spec.PutName("Type", "Filespec");
206 file_spec.PutString("F", "bookmark.pdf");
207
208 FileSpec spec = new FileSpec(file_spec);
209 pdftron.PDF.Action goto_remote = pdftron.PDF.Action.CreateGotoRemote(spec, 5, true);
210
211 Bookmark remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1");
212 remoteBookmark1.SetAction(goto_remote);
213 doc.AddRootBookmark(remoteBookmark1);
214
215 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
216 Bookmark remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2");
217 doc.AddRootBookmark(remoteBookmark2);
218 Obj gotoR = remoteBookmark2.GetSDFObj().PutDict("A");
219 { // Create the 'Action' dictionary.
220 gotoR.PutName("S", "GoToR"); // Set action type
221 gotoR.PutBool("NewWindow", true);
222
223 // Set the file specification
224 gotoR.Put("F", file_spec);
225
226 // Set the destination.
227 Obj dest = gotoR.PutArray("D");
228 dest.PushBackNumber(9); // jump to the tenth page. Note that Acrobat indexes pages from 0.
229 dest.PushBackName("Fit"); // Fit the page
230 }
231
232 doc.Save(Utils.CreateExternalFile("bookmark_remote.pdf"), SDFDoc.SaveOptions.e_linearized);
233 Console.WriteLine("Done. Result saved in bookmark_remote.pdf");
234 }
235 }
236 catch (PDFNetException e)
237 {
238 Console.WriteLine(e.Message);
239 Assert.True(false);
240 }
241 }
242 }
243}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales