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 Server SDK and PDF Editing & Manipulation Library.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software 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
13namespace BookmarkTestCS
14{
15 /// <summary>
16 //-----------------------------------------------------------------------------------------
17 // The sample code illustrates how to read, write, and edit existing outline items
18 // and create new bookmarks using both the high-level and the SDF/Cos API.
19 //-----------------------------------------------------------------------------------------
20 /// </summary>
21 class Class1
22 {
23 private static pdftron.PDFNetLoader pdfNetLoader = pdftron.PDFNetLoader.Instance();
24 static Class1() {}
25
26 static void PrintIndent(Bookmark item)
27 {
28 int indent = item.GetIndent() - 1;
29 for (int i = 0; i < indent; ++i)
30 Console.Write(" ");
31 }
32
33 // Prints out the outline tree to the standard output
34 static void PrintOutlineTree(Bookmark item)
35 {
36 for (; item.IsValid(); item = item.GetNext())
37 {
38 PrintIndent(item);
39 Console.Write("{0:s}{1:s} ACTION -> ", (item.IsOpen() ? "- " : "+ "), item.GetTitle());
40
41 // Print Action
42 pdftron.PDF.Action action = item.GetAction();
43 if (action.IsValid())
44 {
45 if (action.GetType() == pdftron.PDF.Action.Type.e_GoTo)
46 {
47 Destination dest = action.GetDest();
48 if (dest.IsValid())
49 {
50 Page page = dest.GetPage();
51 Console.WriteLine("GoTo Page #{0:d}", page.GetIndex());
52 }
53 }
54 else
55 {
56 Console.WriteLine("Not a 'GoTo' action");
57 }
58 }
59 else
60 {
61 Console.WriteLine("NULL");
62 }
63
64 if (item.HasChildren()) // Recursively print children sub-trees
65 {
66 PrintOutlineTree(item.GetFirstChild());
67 }
68 }
69 }
70
71 /// <summary>
72 /// The main entry point for the application.
73 /// </summary>
74 static void Main(string[] args)
75 {
76 PDFNet.Initialize(PDFTronLicense.Key);
77
78 // Relative path to the folder containing test files.
79 string input_path = "../../../../TestFiles/";
80 string output_path = "../../../../TestFiles/Output/";
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(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(output_path + "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 }
167
168
169 // The following example illustrates how to traverse the outline tree using
170 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
171 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
172 try
173 {
174 // Open the document that was saved in the previous code sample
175 using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf"))
176 {
177 doc.InitSecurityHandler();
178
179 Bookmark root = doc.GetFirstBookmark();
180 PrintOutlineTree(root);
181
182 Console.WriteLine("Done.");
183 }
184 }
185 catch (PDFNetException e)
186 {
187 Console.WriteLine(e.Message);
188 }
189
190 // The following example illustrates how to create a Bookmark to a page
191 // in a remote document. A remote go-to action is similar to an ordinary
192 // go-to action, but jumps to a destination in another PDF file instead
193 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
194 // Reference Manual for details.
195 try
196 {
197 using (PDFDoc doc = new PDFDoc(output_path + "bookmark.pdf"))
198 {
199 doc.InitSecurityHandler();
200
201 // Create file specification (the file referred to by the remote bookmark)
202 Obj file_spec = doc.CreateIndirectDict();
203 file_spec.PutName("Type", "Filespec");
204 file_spec.PutString("F", "bookmark.pdf");
205
206 FileSpec spec = new FileSpec(file_spec);
207 pdftron.PDF.Action goto_remote = pdftron.PDF.Action.CreateGotoRemote(spec, 5, true);
208
209 Bookmark remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1");
210 remoteBookmark1.SetAction(goto_remote);
211 doc.AddRootBookmark(remoteBookmark1);
212
213 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
214 Bookmark remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2");
215 doc.AddRootBookmark(remoteBookmark2);
216 Obj gotoR = remoteBookmark2.GetSDFObj().PutDict("A");
217 { // Create the 'Action' dictionary.
218 gotoR.PutName("S", "GoToR"); // Set action type
219 gotoR.PutBool("NewWindow", true);
220
221 // Set the file specification
222 gotoR.Put("F", file_spec);
223
224 // Set the destination.
225 Obj dest = gotoR.PutArray("D");
226 dest.PushBackNumber(9); // jump to the tenth page. Note that Acrobat indexes pages from 0.
227 dest.PushBackName("Fit"); // Fit the page
228 }
229
230 doc.Save(output_path + "bookmark_remote.pdf", SDFDoc.SaveOptions.e_linearized);
231 Console.WriteLine("Done. Result saved in bookmark_remote.pdf");
232 }
233 }
234 catch (PDFNetException e)
235 {
236 Console.WriteLine(e.Message);
237 }
238 PDFNet.Terminate();
239 }
240 }
241}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales