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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6#include <PDF/PDFNet.h>
7#include <PDF/PDFDoc.h>
8#include <iostream>
9#include <assert.h>
10#include "../../LicenseKey/CPP/LicenseKey.h"
11
12using namespace std;
13using namespace pdftron;
14using namespace SDF;
15using namespace PDF;
16
17//-----------------------------------------------------------------------------------------
18// The sample code illustrates how to read and edit existing outline items and create
19// new bookmarks using the high-level API.
20//-----------------------------------------------------------------------------------------
21
22void PrintIndent(Bookmark item)
23{
24 int ident = item.GetIndent() - 1;
25 for (int i=0; i<ident; ++i) cout << " ";
26}
27
28// Prints out the outline tree to the standard output
29void PrintOutlineTree(Bookmark item)
30{
31 for (; item.IsValid(); item=item.GetNext())
32 {
33 PrintIndent(item);
34 cout << (item.IsOpen() ? "- " : "+ ") << item.GetTitle() << " ACTION -> ";
35
36 // Print Action
37 Action action = item.GetAction();
38 if (action.IsValid()) {
39 if (action.GetType() == Action::e_GoTo) {
40 Destination dest = action.GetDest();
41 if (dest.IsValid()) {
42 Page page = dest.GetPage();
43 cout << "GoTo Page #" << page.GetIndex() << endl;
44 }
45 }
46 else {
47 cout << "Not a 'GoTo' action" << endl;
48 }
49 } else {
50 cout << "NULL" << endl;
51 }
52
53 if (item.HasChildren()) // Recursively print children sub-trees
54 {
55 PrintOutlineTree(item.GetFirstChild());
56 }
57 }
58}
59
60int main(int argc, char *argv[])
61{
62 int ret = 0;
63 PDFNet::Initialize(LicenseKey);
64
65 // Relative path to the folder containing test files.
66 string input_path = "../../TestFiles/";
67 string output_path = "../../TestFiles/Output/";
68
69 // The following example illustrates how to create and edit the outline tree
70 // using high-level Bookmark methods.
71 try
72 {
73 PDFDoc doc((input_path + "numbered.pdf").c_str());
74 doc.InitSecurityHandler();
75
76 // Lets first create the root bookmark items.
77 Bookmark red = Bookmark::Create(doc, "Red");
78 Bookmark green = Bookmark::Create(doc, "Green");
79 Bookmark blue = Bookmark::Create(doc, "Blue");
80
81 doc.AddRootBookmark(red);
82 doc.AddRootBookmark(green);
83 doc.AddRootBookmark(blue);
84
85 // You can also add new root bookmarks using Bookmark.AddNext("...")
86 blue.AddNext("foo");
87 blue.AddNext("bar");
88
89 // We can now associate new bookmarks with page destinations:
90
91 // The following example creates an 'explicit' destination (see
92 // section '8.2.1 Destinations' in PDF Reference for more details)
93 Destination red_dest = Destination::CreateFit(doc.GetPageIterator().Current());
94 red.SetAction(Action::CreateGoto(red_dest));
95
96 // Create an explicit destination to the first green page in the document
97 green.SetAction(Action::CreateGoto(
98 Destination::CreateFit(doc.GetPage(10)) ));
99
100 // The following example creates a 'named' destination (see
101 // section '8.2.1 Destinations' in PDF Reference for more details)
102 // Named destinations have certain advantages over explicit destinations.
103 const char* key = "blue1";
104 Action blue_action = Action::CreateGoto((UChar*) key, UInt32(strlen(key)),
105 Destination::CreateFit(doc.GetPage(19)) );
106
107 blue.SetAction(blue_action);
108
109 // We can now add children Bookmarks
110 Bookmark sub_red1 = red.AddChild("Red - Page 1");
111 sub_red1.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(1))));
112 Bookmark sub_red2 = red.AddChild("Red - Page 2");
113 sub_red2.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(2))));
114 Bookmark sub_red3 = red.AddChild("Red - Page 3");
115 sub_red3.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(3))));
116 Bookmark sub_red4 = sub_red3.AddChild("Red - Page 4");
117 sub_red4.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(4))));
118 Bookmark sub_red5 = sub_red3.AddChild("Red - Page 5");
119 sub_red5.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(5))));
120 Bookmark sub_red6 = sub_red3.AddChild("Red - Page 6");
121 sub_red6.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(6))));
122
123 // Example of how to find and delete a bookmark by title text.
124 Bookmark foo = doc.GetFirstBookmark().Find("foo");
125 if (foo.IsValid())
126 {
127 foo.Delete();
128 }
129 else
130 {
131 assert(false);
132 }
133
134 Bookmark bar = doc.GetFirstBookmark().Find("bar");
135 if (bar.IsValid())
136 {
137 bar.Delete();
138 }
139 else
140 {
141 assert(false);
142 }
143
144 // Adding color to Bookmarks. Color and other formatting can help readers
145 // get around more easily in large PDF documents.
146 red.SetColor(1, 0, 0);
147 green.SetColor(0, 1, 0);
148 green.SetFlags(2); // set bold font
149 blue.SetColor(0, 0, 1);
150 blue.SetFlags(3); // set bold and italic
151
152 doc.Save((output_path + "bookmark.pdf").c_str(), 0, 0);
153 cout << "Done. Result saved in bookmark.pdf" << endl;
154 }
155 catch(Common::Exception& e)
156 {
157 cout << e << endl;
158 ret = 1;
159 }
160 catch(...)
161 {
162 cout << "Unknown Exception" << endl;
163 ret = 1;
164 }
165
166
167 // The following example illustrates how to traverse the outline tree using
168 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
169 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
170 try
171 {
172 // Open the document that was saved in the previous code sample
173 PDFDoc doc((output_path + "bookmark.pdf").c_str());
174 doc.InitSecurityHandler();
175
176 Bookmark root = doc.GetFirstBookmark();
177 PrintOutlineTree(root);
178
179 cout << "Done." << endl;
180 }
181 catch(Common::Exception& e)
182 {
183 cout << e << endl;
184 ret = 1;
185 }
186 catch(...)
187 {
188 cout << "Unknown Exception" << endl;
189 ret = 1;
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 // Open the document that was saved in the previous code sample
200 PDFDoc doc((output_path + "bookmark.pdf").c_str());
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 FileSpec spec(file_spec);
208 Action goto_remote = Action::CreateGotoRemote(spec, 5, true);
209
210 Bookmark remoteBookmark1 = Bookmark::Create(doc, "REMOTE BOOKMARK 1");
211 remoteBookmark1.SetAction(goto_remote);
212 doc.AddRootBookmark(remoteBookmark1);
213
214 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
215 // Create a remote action
216 Bookmark remoteBookmark2 = Bookmark::Create(doc, "REMOTE BOOKMARK 2");
217 doc.AddRootBookmark(remoteBookmark2);
218
219 Obj gotoR = remoteBookmark2.GetSDFObj().PutDict("A");
220 {
221 gotoR.PutName("S","GoToR"); // Set action type
222 gotoR.PutBool("NewWindow", true);
223
224 // Set the file specification
225 gotoR.Put("F", file_spec);
226
227 // jump to the first page. Note that pages are indexed from 0.
228 Obj dest = gotoR.PutArray("D"); // Set the destination
229 dest.PushBackNumber(9);
230 dest.PushBackName("Fit");
231 }
232
233 doc.Save((output_path + "bookmark_remote.pdf").c_str(), SDFDoc::e_linearized, 0);
234
235 cout << "Done. Result saved in bookmark_remote.pdf" << endl;
236 }
237 catch(Common::Exception& e)
238 {
239 cout << e << endl;
240 ret = 1;
241 }
242 catch(...)
243 {
244 cout << "Unknown Exception" << endl;
245 ret = 1;
246 }
247
248 PDFNet::Terminate();
249 return ret;
250}
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}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6import com.pdftron.common.PDFNetException;
7import com.pdftron.pdf.*;
8import com.pdftron.sdf.Obj;
9import com.pdftron.sdf.SDFDoc;
10
11
12public class BookmarkTest {
13
14 public static void main(String[] args) {
15 PDFNet.initialize(PDFTronLicense.Key());
16
17 // Relative path to the folder containing test files.
18 String input_path = "../../TestFiles/";
19 String output_path = "../../TestFiles/Output/";
20
21 // The following example illustrates how to create and edit the outline tree
22 // using high-level Bookmark methods.
23 try (PDFDoc doc = new PDFDoc((input_path + "numbered.pdf"))) {
24 doc.initSecurityHandler();
25
26 // Lets first create the root bookmark items.
27 Bookmark red = Bookmark.create(doc, "Red");
28 Bookmark green = Bookmark.create(doc, "Green");
29 Bookmark blue = Bookmark.create(doc, "Blue");
30
31 doc.addRootBookmark(red);
32 doc.addRootBookmark(green);
33 doc.addRootBookmark(blue);
34
35 // You can also add new root bookmarks using Bookmark.AddNext("...")
36 blue.addNext("foo");
37 blue.addNext("bar");
38
39 // We can now associate new bookmarks with page destinations:
40
41 // The following example creates an 'explicit' destination (see
42 // section '8.2.1 Destinations' in PDF Reference for more details)
43 Destination red_dest = Destination.createFit(doc.getPageIterator().next());
44 red.setAction(Action.createGoto(red_dest));
45
46 // Create an explicit destination to the first green page in the document
47 green.setAction(Action.createGoto(
48 Destination.createFit(doc.getPage(10))));
49
50 // The following example creates a 'named' destination (see
51 // section '8.2.1 Destinations' in PDF Reference for more details)
52 // Named destinations have certain advantages over explicit destinations.
53 byte[] key = {'b', 'l', 'u', 'e', '1'};
54 Action blue_action = Action.createGoto(key,
55 Destination.createFit(doc.getPage(19)));
56
57 blue.setAction(blue_action);
58
59 // We can now add children Bookmarks
60 Bookmark sub_red1 = red.addChild("Red - Page 1");
61 sub_red1.setAction(Action.createGoto(Destination.createFit(doc.getPage(1))));
62 Bookmark sub_red2 = red.addChild("Red - Page 2");
63 sub_red2.setAction(Action.createGoto(Destination.createFit(doc.getPage(2))));
64 Bookmark sub_red3 = red.addChild("Red - Page 3");
65 sub_red3.setAction(Action.createGoto(Destination.createFit(doc.getPage(3))));
66 Bookmark sub_red4 = sub_red3.addChild("Red - Page 4");
67 sub_red4.setAction(Action.createGoto(Destination.createFit(doc.getPage(4))));
68 Bookmark sub_red5 = sub_red3.addChild("Red - Page 5");
69 sub_red5.setAction(Action.createGoto(Destination.createFit(doc.getPage(5))));
70 Bookmark sub_red6 = sub_red3.addChild("Red - Page 6");
71 sub_red6.setAction(Action.createGoto(Destination.createFit(doc.getPage(6))));
72
73 // Example of how to find and delete a bookmark by title text.
74 Bookmark foo = doc.getFirstBookmark().find("foo");
75 if (foo.isValid()) {
76 foo.delete();
77 } else {
78 throw new Exception("Foo is not Valid");
79 }
80
81 Bookmark bar = doc.getFirstBookmark().find("bar");
82 if (bar.isValid()) {
83 bar.delete();
84 } else {
85 throw new Exception("Bar is not Valid");
86 }
87
88 // Adding color to Bookmarks. Color and other formatting can help readers
89 // get around more easily in large PDF documents.
90 red.setColor(1, 0, 0);
91 green.setColor(0, 1, 0);
92 green.setFlags(2); // set bold font
93 blue.setColor(0, 0, 1);
94 blue.setFlags(3); // set bold and itallic
95
96 doc.save((output_path + "bookmark.pdf"), SDFDoc.SaveMode.NO_FLAGS, null);
97 System.out.println("Done. Result saved in bookmark.pdf");
98 } catch (Exception e) {
99 System.out.println(e);
100 }
101
102 // The following example illustrates how to traverse the outline tree using
103 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
104 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
105 // Open the document that was saved in the previous code sample
106 try (PDFDoc doc = new PDFDoc((output_path + "bookmark.pdf"))) {
107 doc.initSecurityHandler();
108
109 Bookmark root = doc.getFirstBookmark();
110 PrintOutlineTree(root);
111 System.out.println("Done.");
112 } catch (Exception e) {
113 System.out.println(e);
114 }
115
116 // The following example illustrates how to create a Bookmark to a page
117 // in a remote document. A remote go-to action is similar to an ordinary
118 // go-to action, but jumps to a destination in another PDF file instead
119 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
120 // Reference Manual for details.
121 // Open the document that was saved in the previous code sample
122 try (PDFDoc doc = new PDFDoc((output_path + "bookmark.pdf"))) {
123 doc.initSecurityHandler();
124
125 // Create file specification (the file reffered to by the remote bookmark)
126 Obj file_spec = doc.createIndirectDict();
127 file_spec.putName("Type", "Filespec");
128 file_spec.putString("F", "bookmark.pdf");
129 FileSpec spec = new FileSpec(file_spec);
130 Action goto_remote = Action.createGotoRemote(spec, 5, true);
131
132 Bookmark remoteBookmark1 = Bookmark.create(doc, "REMOTE BOOKMARK 1");
133 remoteBookmark1.setAction(goto_remote);
134 doc.addRootBookmark(remoteBookmark1);
135
136 // Create another remote bootmark, but this time using the low-level SDF/Cos API.
137 // Create a remote action
138 Bookmark remoteBookmark2 = Bookmark.create(doc, "REMOTE BOOKMARK 2");
139 doc.addRootBookmark(remoteBookmark2);
140
141 Obj gotoR = remoteBookmark2.getSDFObj().putDict("A");
142 {
143 gotoR.putName("S", "GoToR"); // Set action type
144 gotoR.putBool("NewWindow", true);
145
146 // Set the file specification
147 gotoR.put("F", file_spec);
148
149 // jump to the first page. Note that pages are indexed from 0.
150 Obj dest = gotoR.putArray("D"); // Set the destination
151 dest.pushBackNumber(9);
152 dest.pushBackName("Fit");
153 }
154
155 doc.save((output_path + "bookmark_remote.pdf"), SDFDoc.SaveMode.LINEARIZED, null);
156 System.out.println("Done. Result saved in bookmark_remote.pdf");
157 } catch (Exception e) {
158 System.out.println(e);
159 }
160
161 PDFNet.terminate();
162 }
163
164 static void PrintIndent(Bookmark item) throws PDFNetException {
165 int ident = item.getIndent() - 1;
166 for (int i = 0; i < ident; ++i) System.out.print(" ");
167 }
168
169 // Prints out the outline tree to the standard output
170 static void PrintOutlineTree(Bookmark item) throws PDFNetException {
171 for (; item.isValid(); item = item.getNext()) {
172 PrintIndent(item);
173 System.out.print((item.isOpen() ? "- " : "+ ") + item.getTitle() + " ACTION -> ");
174
175 // Print Action
176 Action action = item.getAction();
177 if (action.isValid()) {
178 if (action.getType() == Action.e_GoTo) {
179 Destination dest = action.getDest();
180 if (dest.isValid()) {
181 Page page = dest.getPage();
182 System.out.println("GoTo Page #" + page.getIndex());
183 }
184 } else {
185 System.out.println("Not a 'GoTo' action");
186 }
187 } else {
188 System.out.println("NULL");
189 }
190
191 if (item.hasChildren()) // Recursively print children sub-trees
192 {
193 PrintOutlineTree(item.getFirstChild());
194 }
195 }
196 }
197}
1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3// Consult legal.txt regarding legal and license information.
4//---------------------------------------------------------------------------------------
5
6//-----------------------------------------------------------------------------------------
7// The sample code illustrates how to read and edit existing outline items and create
8// new bookmarks using the high-level API.
9//-----------------------------------------------------------------------------------------
10
11const { PDFNet } = require('@pdftron/pdfnet-node');
12const PDFTronLicense = require('../LicenseKey/LicenseKey');
13
14((exports) => {
15
16 exports.runBookmarkTest = () => {
17
18 const getIndent = async (item) => {
19 const ident = (await item.getIndent()) - 1;
20 let str = '';
21 for (let i = 0; i < ident; ++i) {
22 str += ' ';
23 }
24 return str;
25 };
26
27 // Prints out the outline tree to the standard output
28 const printOutlineTree = async (item) => {
29 for (; item != null; item = await item.getNext()) {
30 const indentString = await getIndent(item);
31 const titleString = await item.getTitle();
32
33 const actionString = indentString + (await item.isOpen() ? '- ' : '+ ') + titleString + ' ACTION -> ';
34
35 // Print Action
36 const action = await item.getAction();
37 if (await action.isValid()) {
38 const actionType = await action.getType();
39 if (actionType === PDFNet.Action.Type.e_GoTo) {
40 const dest = await action.getDest();
41 if (await dest.isValid()) {
42 const page = await dest.getPage();
43 console.log(actionString + 'GoTo Page #' + (await page.getIndex()));
44 }
45 } else {
46 console.log(actionString + 'Not a "GoTo" action');
47 }
48 } else {
49 console.log(actionString + 'NULL');
50 }
51
52 if (await item.hasChildren()) {
53 await printOutlineTree(await item.getFirstChild());
54 }
55 }
56 };
57
58 const main = async () => {
59 // Relative path to the folder containing test files.
60 const inputPath = '../TestFiles/';
61 const outputPath = inputPath + 'Output/';
62
63 // The following example illustrates how to create and edit the outline tree
64 // using high-level Bookmark methods.
65 try {
66 let doc = await PDFNet.PDFDoc.createFromFilePath(inputPath + 'numbered.pdf');
67 doc.initSecurityHandler();
68
69 // Lets first create the root bookmark items.
70 const red = await PDFNet.Bookmark.create(doc, 'Red');
71 const green = await PDFNet.Bookmark.create(doc, 'Green');
72 const blue = await PDFNet.Bookmark.create(doc, 'Blue');
73
74 doc.addRootBookmark(red);
75 doc.addRootBookmark(green);
76 doc.addRootBookmark(blue);
77
78 // You can also add new root bookmarks using Bookmark.addNext("...")
79 blue.addNewNext('foo');
80 blue.addNewNext('bar');
81
82 // We can now associate new bookmarks with page destinations:
83
84 // The following example creates an 'explicit' destination (see
85 // section '8.2.1 Destinations' in PDF Reference for more details)
86 const redIter = await doc.getPageIterator(1);
87 const redCurrpage = await redIter.current();
88 const redDest = await PDFNet.Destination.createFit(redCurrpage);
89 red.setAction(await PDFNet.Action.createGoto(redDest));
90
91 // Create an explicit destination to the first green page in the document
92 const tenthPage = await doc.getPage(10);
93 const greenDest = await PDFNet.Destination.createFit(tenthPage);
94 green.setAction(await PDFNet.Action.createGoto(greenDest));
95
96 // The following example creates a 'named' destination (see
97 // section '8.2.1 Destinations' in PDF Reference for more details)
98 // Named destinations have certain advantages over explicit destinations.
99 const key = 'blue1';
100 const nineteenthPage = await doc.getPage(19);
101 const blueDest = await PDFNet.Destination.createFit(nineteenthPage);
102 const blueAction = await PDFNet.Action.createGotoWithKey(key, blueDest); // TODO FIND FIX
103
104 blue.setAction(blueAction);
105
106 // We can now add children Bookmarks subRed1 instanceof Promise
107 const subRed1 = await red.addNewChild('Red - Page 1');
108 subRed1.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(1))));
109 const subRed2 = await red.addNewChild('Red - Page 2');
110 subRed2.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(2))));
111 const subRed3 = await red.addNewChild('Red - Page 3');
112 subRed3.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(3))));
113 const subRed4 = await subRed3.addNewChild('Red - Page 4');
114 subRed4.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(4))));
115 const subRed5 = await subRed3.addNewChild('Red - Page 5');
116 subRed5.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(5))));
117 const subRed6 = await subRed3.addNewChild('Red - Page 6');
118 subRed6.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(6))));
119
120 // Example of how to find and delete a bookmark by title text.
121 const firstbookmark = await doc.getFirstBookmark();
122 const foo = await firstbookmark.find('foo');
123 if (await foo.isValid()) {
124 foo.delete();
125 } else {
126 console.log('Bookmark foo is invalid');
127 }
128 const bar = await firstbookmark.find('bar');
129 if (await bar.isValid()) {
130 bar.delete();
131 } else {
132 console.log('Bookmark bar is invalid');
133 }
134
135 // Adding color to Bookmarks. Color and other formatting can help readers
136 // get around more easily in large PDF documents.
137 red.setColor(1, 0, 0);
138 green.setColor(0, 1, 0);
139 green.setFlags(2); // set bold font
140 blue.setColor(0, 0, 1);
141 blue.setFlags(3); // set bold and italic
142
143 await doc.save(outputPath + 'bookmark.pdf', 0);
144 console.log('Done. Result saved in bookmark.pdf');
145 } catch (err) {
146 console.log(err);
147 }
148
149 // The following example illustrates how to traverse the outline tree using
150 // Bookmark navigation methods: Bookmark.getNext(), Bookmark.getPrev(),
151 // Bookmark.getFirstChild () and Bookmark.getLastChild ().
152 try {
153 // Open the document that was saved in the previous code sample
154 const doc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'bookmark.pdf');
155 doc.initSecurityHandler();
156
157 const root = await doc.getFirstBookmark();
158 await printOutlineTree(root);
159
160 console.log('Done.');
161 } catch (err) {
162 console.log(err);
163 }
164
165 // The following example illustrates how to create a Bookmark to a page
166 // in a remote document. A remote go-to action is similar to an ordinary
167 // go-to action, but jumps to a destination in another PDF file instead
168 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
169 // Reference Manual for details.
170
171 try {
172 // Open the document that was saved in the previous code sample
173 const doc = await PDFNet.PDFDoc.createFromFilePath(outputPath + 'bookmark.pdf');
174 doc.initSecurityHandler();
175
176 // Create file specification (the file referred to by the remote bookmark)
177 const fileSpec = await doc.createIndirectDict();
178 fileSpec.putName('Type', 'Filespec');
179 fileSpec.putString('F', 'bookmark.pdf');
180 const spec = await PDFNet.FileSpec.createFromObj(fileSpec);
181 const gotoRemote = await PDFNet.Action.createGotoRemoteSetNewWindow(spec, 5, true);
182
183 const remoteBookmark1 = await PDFNet.Bookmark.create(doc, 'REMOTE BOOKMARK 1');
184 remoteBookmark1.setAction(gotoRemote);
185 doc.addRootBookmark(remoteBookmark1);
186
187 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
188 // Create a remote action
189 const remoteBookmark2 = await PDFNet.Bookmark.create(doc, 'REMOTE BOOKMARK 2');
190 doc.addRootBookmark(remoteBookmark2);
191
192 const gotoR = await (await remoteBookmark2.getSDFObj()).putDict('A');
193 {
194 gotoR.putName('S', 'GoToR'); // Set action type
195 gotoR.putBool('NewWindow', true);
196
197 // Set the file specification
198 gotoR.put('F', fileSpec);
199
200 // jump to the first page. Note that pages are indexed from 0.
201 const dest = await gotoR.putArray('D');
202 dest.pushBackNumber(9);
203 dest.pushBackName('Fit');
204 }
205
206 await doc.save(inputPath + 'Output/bookmark_remote.pdf', PDFNet.SDFDoc.SaveOptions.e_linearized);
207
208 console.log('Done. Result saved in bookmark_remote.pdf');
209 } catch (err) {
210 console.log(err);
211 }
212 };
213 PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
214 console.log('Error: ' + JSON.stringify(error));
215 }).then(function () { return PDFNet.shutdown(); });
216 };
217 exports.runBookmarkTest();
218})(exports);
219// eslint-disable-next-line spaced-comment
220//# sourceURL=BookmarkTest.js
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14#-----------------------------------------------------------------------------------------
15# The sample code illustrates how to read and edit existing outline items and create
16# new bookmarks using the high-level API.
17#-----------------------------------------------------------------------------------------
18
19# Relattive path to the folder containing the test files.
20input_path = "../../TestFiles/"
21output_path = "../../TestFiles/Output/"
22
23def PrintIndent(item):
24 indent = item.GetIndent() - 1
25 i = 0
26 while i < indent:
27 sys.stdout.write(" ")
28 i = i + 1
29
30# Prints out the outline tree to the standard output
31def PrintOutlineTree (item):
32 while item.IsValid():
33 PrintIndent(item)
34 if item.IsOpen():
35 sys.stdout.write("- " + item.GetTitle() + " ACTION -> ")
36 else:
37 sys.stdout.write("+ " + item.GetTitle() + " ACTION -> ")
38
39 # Print Action
40 action = item.GetAction()
41 if action.IsValid():
42 if action.GetType() == Action.e_GoTo:
43 dest = action.GetDest()
44 if dest.IsValid():
45 page = dest.GetPage()
46 print("GoTo Page #" + str(page.GetIndex()))
47 else:
48 print("Not a 'GoTo' action")
49 else:
50 print("NULL")
51
52 # Recursively print children sub-trees
53 if item.HasChildren():
54 PrintOutlineTree(item.GetFirstChild())
55 item = item.GetNext()
56
57
58def main():
59 PDFNet.Initialize(LicenseKey)
60
61 # The following example illustrates how to create and edit the outline tree
62 # using high-level Bookmark methods.
63
64 doc = PDFDoc(input_path + "numbered.pdf")
65 doc.InitSecurityHandler()
66
67 # Lets first create the root bookmark items.
68 red = Bookmark.Create(doc, "Red")
69 green = Bookmark.Create(doc, "Green")
70 blue = Bookmark.Create(doc, "Blue")
71
72 doc.AddRootBookmark(red)
73 doc.AddRootBookmark(green)
74 doc.AddRootBookmark(blue)
75
76 # You can also add new root bookmarks using Bookmark.AddNext("...")
77 blue.AddNext("foo")
78 blue.AddNext("bar")
79
80 # We can now associate new bookmarks with page destinations:
81
82 # The following example creates an 'explicit' destination (see
83 # section '8.2.1 Destinations' in PDF Reference for more details)
84 itr = doc.GetPageIterator()
85 red_dest = Destination.CreateFit(itr.Current())
86 red.SetAction(Action.CreateGoto(red_dest))
87
88 # Create an explicit destination to the first green page in the document
89 green.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(10))))
90
91 # The following example creates a 'named' destination (see
92 # section '8.2.1 Destinations' in PDF Reference for more details)
93 # Named destinations have certain advantages over explicit destinations.
94 key = bytearray(b"blue1")
95 blue_action = Action.CreateGoto(key, len(key), Destination.CreateFit(doc.GetPage(19)))
96
97 blue.SetAction(blue_action)
98
99 # We can now add children Bookmarks
100 sub_red1 = red.AddChild("Red - Page 1")
101 sub_red1.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))))
102 sub_red2 = red.AddChild("Red - Page 2")
103 sub_red2.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(2))))
104 sub_red3 = red.AddChild("Red - Page 3")
105 sub_red3.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(3))))
106 sub_red4 = sub_red3.AddChild("Red - Page 4")
107 sub_red4.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(4))))
108 sub_red5 = sub_red3.AddChild("Red - Page 5")
109 sub_red5.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(5))))
110 sub_red6 = sub_red3.AddChild("Red - Page 6")
111 sub_red6.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(6))))
112
113 # Example of how to find and delete a bookmark by title text.
114 foo = doc.GetFirstBookmark().Find("foo")
115 if foo.IsValid():
116 foo.Delete()
117 else:
118 raise Exception("Foo is not Valid")
119
120 bar = doc.GetFirstBookmark().Find("bar")
121 if bar.IsValid():
122 bar.Delete()
123 else:
124 raise Exception("Bar is not Valid")
125
126 # Adding color to Bookmarks. Color and other formatting can help readers
127 # get around more easily in large PDF documents.
128 red.SetColor(1, 0, 0);
129 green.SetColor(0, 1, 0);
130 green.SetFlags(2); # set bold font
131 blue.SetColor(0, 0, 1);
132 blue.SetFlags(3); # set bold and itallic
133
134 doc.Save(output_path + "bookmark.pdf", 0)
135 doc.Close()
136 print("Done. Result saved in bookmark.pdf")
137
138 # The following example illustrates how to traverse the outline tree using
139 # Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
140 # Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
141
142 # Open the document that was saved in the previous code sample
143 doc = PDFDoc(output_path + "bookmark.pdf")
144 doc.InitSecurityHandler()
145
146 root = doc.GetFirstBookmark()
147 PrintOutlineTree(root)
148
149 doc.Close()
150 print("Done.")
151
152 # The following example illustrates how to create a Bookmark to a page
153 # in a remote document. A remote go-to action is similar to an ordinary
154 # go-to action, but jumps to a destination in another PDF file instead
155 # of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
156 # Reference Manual for details.
157
158 doc = PDFDoc(output_path + "bookmark.pdf")
159 doc.InitSecurityHandler()
160
161 # Create file specification (the file reffered to by the remote bookmark)
162 file_spec = doc.CreateIndirectDict()
163 file_spec.PutName("Type", "Filespec")
164 file_spec.PutString("F", "bookmark.pdf")
165 spec = FileSpec(file_spec)
166 goto_remote = Action.CreateGotoRemote(spec, 5, True)
167
168 remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1")
169 remoteBookmark1.SetAction(goto_remote)
170 doc.AddRootBookmark(remoteBookmark1)
171
172 # Create another remote bookmark, but this time using the low-level SDF/Cos API.
173 # Create a remote action
174 remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2")
175 doc.AddRootBookmark(remoteBookmark2)
176
177 gotoR = remoteBookmark2.GetSDFObj().PutDict("A")
178 gotoR.PutName("S","GoToR") # Set action type
179 gotoR.PutBool("NewWindow", True)
180
181 # Set the file specification
182 gotoR.Put("F", file_spec)
183
184 # jump to the first page. Note that pages are indexed from 0.
185 dest = gotoR.PutArray("D") # Set the destination
186 dest.PushBackNumber(9);
187 dest.PushBackName("Fit");
188
189 doc.Save(output_path + "bookmark_remote.pdf", SDFDoc.e_linearized)
190 doc.Close()
191 PDFNet.Terminate()
192 print("Done. Result saved in bookmark_remote.pdf")
193
194if __name__ == '__main__':
195 main()
1<?php
2//---------------------------------------------------------------------------------------
3// Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
4// Consult LICENSE.txt regarding license information.
5//---------------------------------------------------------------------------------------
6if(file_exists("../../../PDFNetC/Lib/PDFNetPHP.php"))
7
8include("../../../PDFNetC/Lib/PDFNetPHP.php");
9include("../../LicenseKey/PHP/LicenseKey.php");
10
11//---------------------------------------------------------------------------------------
12// The sample code illustrates how to read and edit existing outline items and create
13// new bookmarks using the high-level API.
14//---------------------------------------------------------------------------------------
15
16function PrintIndent ($item) {
17 $ident = $item->GetIndent() - 1;
18 for ($i=0; $i<$ident; ++$i) {
19 echo " ";
20 }
21}
22
23// Prints out the outline tree to the standard output
24function PrintOutlineTree($item) {
25 for (; $item->IsValid(); $item=$item->GetNext())
26 {
27 PrintIndent($item);
28 echo ($item->IsOpen() ? "- " : "+ ").$item->GetTitle()." ACTION -> ";
29
30 // Print Action
31 $action = $item->GetAction();
32 if ($action->IsValid()) {
33 if ($action->GetType() == Action::e_GoTo) {
34 $dest = $action->GetDest();
35 if ($dest->IsValid()) {
36 $page = $dest->GetPage();
37 echo nl2br("GoTo Page #".$page->GetIndex()."\n");
38 }
39 }
40 else {
41 echo nl2br("Not a 'GoTo' action\n");
42 }
43 } else {
44 echo nl2br("NULL\n");
45 }
46
47 if ($item->HasChildren()) // Recursively print children sub-trees
48 {
49 PrintOutlineTree($item->GetFirstChild());
50 }
51 }
52}
53 PDFNet::Initialize($LicenseKey);
54 PDFNet::GetSystemFontList(); // Wait for fonts to be loaded if they haven't already. This is done because PHP can run into errors when shutting down if font loading is still in progress.
55
56 // Relative path to the folder containing the test files.
57 $input_path = getcwd()."/../../TestFiles/";
58 $output_path = $input_path."Output/";
59
60 // The following example illustrates how to create and edit the outline tree
61 // using high-level Bookmark methods.
62 $doc = new PDFDoc($input_path."numbered.pdf");
63 $doc->InitSecurityHandler();
64
65 // Lets first create the root bookmark items.
66 $red = Bookmark::Create($doc, "Red");
67 $green = Bookmark::Create($doc, "Green");
68 $blue = Bookmark::Create($doc, "Blue");
69
70 $doc->AddRootBookmark($red);
71 $doc->AddRootBookmark($green);
72 $doc->AddRootBookmark($blue);
73
74 // You can also add new root bookmarks using $bookmark->AddNext("...")
75 $blue->AddNext("foo");
76 $blue->AddNext("bar");
77
78 // We can now associate new bookmarks with page destinations:
79
80 // The following example creates an 'explicit' destination (see
81 // section '8.2.1 Destinations' in PDF Reference for more details)
82 $ir= $doc->GetPageIterator();
83 $red_dest = Destination::CreateFit($ir->Current());
84 $red->SetAction(Action::CreateGoto($red_dest));
85
86 // Create an explicit destination to the first green page in the document
87 $green->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(10)) ));
88
89 // The following example creates a 'named' destination (see
90 // section '8.2.1 Destinations' in PDF Reference for more details)
91 // Named destinations have certain advantages over explicit destinations.
92 $key = "blue1";
93 $blue_action = Action::CreateGoto($key, strlen($key), Destination::CreateFit($doc->GetPage(19)));
94
95 $blue->SetAction($blue_action);
96
97 // We can now add children Bookmarks
98 $sub_red1 = $red->AddChild("Red - Page 1");
99 $sub_red1->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(1))));
100 $sub_red2 = $red->AddChild("Red - Page 2");
101 $sub_red2->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(2))));
102 $sub_red3 = $red->AddChild("Red - Page 3");
103 $sub_red3->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(3))));
104 $sub_red4 = $sub_red3->AddChild("Red - Page 4");
105 $sub_red4->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(4))));
106 $sub_red5 = $sub_red3->AddChild("Red - Page 5");
107 $sub_red5->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(5))));
108 $sub_red6 = $sub_red3->AddChild("Red - Page 6");
109 $sub_red6->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(6))));
110
111 // Example of how to find and delete a bookmark by title text.
112 $foo = $doc->GetFirstBookmark()->Find("foo");
113 if ($foo->IsValid())
114 {
115 $foo->Delete();
116 }
117 else
118 {
119 assert(false);
120 }
121
122 $bar = $doc->GetFirstBookmark()->Find("bar");
123 if ($bar->IsValid())
124 {
125 $bar->Delete();
126 }
127 else
128 {
129 assert(false);
130 }
131
132 // Adding color to Bookmarks. Color and other formatting can help readers
133 // get around more easily in large PDF documents.
134 $red->SetColor(1.0, 0.0, 0.0);
135 $green->SetColor(0.0, 1.0, 0.0);
136 $green->SetFlags(2); // set bold font
137 $blue->SetColor(0.0, 0.0, 1.0);
138 $blue->SetFlags(3); // set bold and italic
139
140 $doc->Save($output_path."bookmark.pdf", 0);
141 echo nl2br("Done. Result saved in bookmark.pdf\n");
142
143 // The following example illustrates how to traverse the outline tree using
144 // Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
145 // Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
146
147 // Open the document that was saved in the previous code sample
148 $doc = new PDFDoc($output_path."bookmark.pdf");
149 $doc->InitSecurityHandler();
150
151 $root = $doc->GetFirstBookmark();
152 PrintOutlineTree($root);
153 echo nl2br("Done.\n");
154
155 // The following example illustrates how to create a Bookmark to a page
156 // in a remote document. A remote go-to action is similar to an ordinary
157 // go-to action, but jumps to a destination in another PDF file instead
158 // of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
159 // Reference Manual for details.
160
161 // Open the document that was saved in the previous code sample
162 $doc = new PDFDoc($output_path."bookmark.pdf");
163 $doc->InitSecurityHandler();
164
165 // Create file specification (the file referred to by the remote bookmark)
166 $file_spec = $doc->CreateIndirectDict();
167 $file_spec->PutName("Type", "Filespec");
168 $file_spec->PutString("F", "bookmark.pdf");
169 $spec = new FileSpec($file_spec);
170 $goto_remote = Action::CreateGotoRemote($spec, 5, true);
171
172 $remoteBookmark1 = Bookmark::Create($doc, "REMOTE BOOKMARK 1");
173 $remoteBookmark1->SetAction($goto_remote);
174 $doc->AddRootBookmark($remoteBookmark1);
175
176 // Create another remote bookmark, but this time using the low-level SDF/Cos API.
177 // Create a remote action
178 $remoteBookmark2 = Bookmark::Create($doc, "REMOTE BOOKMARK 2");
179 $doc->AddRootBookmark($remoteBookmark2);
180
181 $gotoR = $remoteBookmark2->GetSDFObj()->PutDict("A");
182
183 $gotoR->PutName("S","GoToR"); // Set action type
184 $gotoR->PutBool("NewWindow", true);
185
186 // Set the file specification
187 $gotoR->Put("F", $file_spec);
188
189 // jump to the first page. Note that pages are indexed from 0.
190 $dest = $gotoR->PutArray("D"); // Set the destination
191 $dest->PushBackNumber(9);
192 $dest->PushBackName("Fit");
193
194 $doc->Save($output_path."bookmark_remote.pdf", SDFDoc::e_linearized);
195 PDFNet::Terminate();
196 echo nl2br("Done. Result saved in bookmark_remote.pdf\n");
197?>
1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#-----------------------------------------------------------------------------------------
13# The sample code illustrates how to read and edit existing outline items and create
14# new bookmarks using the high-level API.
15#-----------------------------------------------------------------------------------------
16
17# Relattive path to the folder containing the test files.
18input_path = "../../TestFiles/"
19output_path = "../../TestFiles/Output/"
20
21def PrintIndent(item)
22 indent = item.GetIndent() - 1
23 i = 0
24 while i < indent do
25 print " "
26 i = i + 1
27 end
28end
29
30# Prints out the outline tree to the standard output
31def PrintOutlineTree (item)
32 while item.IsValid() do
33 PrintIndent(item)
34 if item.IsOpen()
35 print( "- " + item.GetTitle() + " ACTION -> ")
36 else
37 print("+ " + item.GetTitle() + " ACTION -> ")
38 end
39
40 # Print Action
41 action = item.GetAction()
42 if action.IsValid()
43 if action.GetType() == Action::E_GoTo
44 dest = action.GetDest()
45 if dest.IsValid()
46 page = dest.GetPage()
47 puts "GoTo Page #" + page.GetIndex().to_s()
48 end
49 else
50 puts "Not a 'GoTo' action"
51 end
52 else
53 puts "NIL"
54 end
55
56 # Recursively print children sub-trees
57 if item.HasChildren()
58 PrintOutlineTree(item.GetFirstChild())
59 end
60 item = item.GetNext()
61 end
62end
63
64 PDFNet.Initialize(PDFTronLicense.Key)
65
66
67 # The following example illustrates how to create and edit the outline tree
68 # using high-level Bookmark methods.
69
70 doc = PDFDoc.new(input_path + "numbered.pdf")
71 doc.InitSecurityHandler()
72
73 # Lets first create the root bookmark items.
74 red = Bookmark.Create(doc, "Red")
75 green = Bookmark.Create(doc, "Green")
76 blue = Bookmark.Create(doc, "Blue")
77
78 doc.AddRootBookmark(red)
79 doc.AddRootBookmark(green)
80 doc.AddRootBookmark(blue)
81
82 # You can also add new root bookmarks using Bookmark.AddNext("...")
83 blue.AddNext("foo")
84 blue.AddNext("bar")
85
86 # We can now associate new bookmarks with page destinations:
87
88 # The following example creates an 'explicit' destination (see
89 # section '8.2.1 Destinations' in PDF Reference for more details)
90 itr = doc.GetPageIterator()
91 red_dest = Destination.CreateFit(itr.Current())
92 red.SetAction(Action.CreateGoto(red_dest))
93
94 # Create an explicit destination to the first green page in the document
95 green.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(10))))
96
97 # The following example creates a 'named' destination (see
98 # section '8.2.1 Destinations' in PDF Reference for more details)
99 # Named destinations have certain advantages over explicit destinations.
100 key = "blue1"
101 blue_action = Action.CreateGoto(key, key.length, Destination.CreateFit(doc.GetPage(19)))
102
103 blue.SetAction(blue_action)
104
105 # We can now add children Bookmarks
106 sub_red1 = red.AddChild("Red - Page 1")
107 sub_red1.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))))
108 sub_red2 = red.AddChild("Red - Page 2")
109 sub_red2.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(2))))
110 sub_red3 = red.AddChild("Red - Page 3")
111 sub_red3.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(3))))
112 sub_red4 = sub_red3.AddChild("Red - Page 4")
113 sub_red4.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(4))))
114 sub_red5 = sub_red3.AddChild("Red - Page 5")
115 sub_red5.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(5))))
116 sub_red6 = sub_red3.AddChild("Red - Page 6")
117 sub_red6.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(6))))
118
119 # Example of how to find and delete a bookmark by title text.
120 foo = doc.GetFirstBookmark().Find("foo")
121 if foo.IsValid()
122 foo.Delete()
123 else
124 raise "Foo is not Valid"
125 end
126
127 bar = doc.GetFirstBookmark().Find("bar")
128 if bar.IsValid()
129 bar.Delete()
130 else
131 raise "Bar is not Valid"
132 end
133
134 # Adding color to Bookmarks. Color and other formatting can help readers
135 # get around more easily in large PDF documents.
136 red.SetColor(1, 0, 0);
137 green.SetColor(0, 1, 0);
138 green.SetFlags(2); # set bold font
139 blue.SetColor(0, 0, 1);
140 blue.SetFlags(3); # set bold and itallic
141
142 doc.Save(output_path + "bookmark.pdf", 0)
143 doc.Close()
144 puts "Done. Result saved in bookmark.pdf"
145
146 # The following example illustrates how to traverse the outline tree using
147 # Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
148 # Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
149
150 # Open the document that was saved in the previous code sample
151 doc = PDFDoc.new(output_path + "bookmark.pdf")
152 doc.InitSecurityHandler()
153
154 root = doc.GetFirstBookmark()
155 PrintOutlineTree(root)
156
157 doc.Close()
158 puts "Done."
159
160 # The following example illustrates how to create a Bookmark to a page
161 # in a remote document. A remote go-to action is similar to an ordinary
162 # go-to action, but jumps to a destination in another PDF file instead
163 # of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
164 # Reference Manual for details.
165
166 doc = PDFDoc.new(output_path + "bookmark.pdf")
167 doc.InitSecurityHandler()
168
169 # Create file specification (the file reffered to by the remote bookmark)
170 file_spec = doc.CreateIndirectDict()
171 file_spec.PutName("Type", "Filespec")
172 file_spec.PutString("F", "bookmark.pdf")
173 spec = FileSpec.new(file_spec)
174 goto_remote = Action.CreateGotoRemote(spec, 5, true)
175
176 remoteBookmark1 = Bookmark.Create(doc, "REMOTE BOOKMARK 1")
177 remoteBookmark1.SetAction(goto_remote)
178 doc.AddRootBookmark(remoteBookmark1)
179
180 # Create another remote bookmark, but this time using the low-level SDF/Cos API.
181 # Create a remote action
182 remoteBookmark2 = Bookmark.Create(doc, "REMOTE BOOKMARK 2")
183 doc.AddRootBookmark(remoteBookmark2)
184
185 gotoR = remoteBookmark2.GetSDFObj().PutDict("A")
186 gotoR.PutName("S","GoToR") # Set action type
187 gotoR.PutBool("NewWindow", true)
188
189 # Set the file specification
190 gotoR.Put("F", file_spec)
191
192 # jump to the first page. Note that pages are indexed from 0.
193 dest = gotoR.PutArray("D") # Set the destination
194 dest.PushBackNumber(9);
195 dest.PushBackName("Fit");
196
197 doc.Save(output_path + "bookmark_remote.pdf", SDFDoc::E_linearized)
198 doc.Close()
199 PDFNet.Terminate
200 puts "Done. Result saved in bookmark_remote.pdf"
1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12
13Module BookmarkTestVB2003
14 Dim pdfNetLoader As PDFNetLoader
15 Sub New()
16 pdfNetLoader = pdftron.PDFNetLoader.Instance()
17 End Sub
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 Sub Main()
23 BookmarkTest.RunTest()
24 End Sub
25
26
27 Class BookmarkTest
28
29 Public Shared Sub PrintIndent(ByVal item As Bookmark)
30 Dim indent As Integer = item.GetIndent() - 1
31 Dim i As Integer
32 For i = 1 To indent
33 Console.Write(" ")
34 Next
35 End Sub
36
37 Public Shared Sub PrintOutlineTree(ByVal item As Bookmark)
38
39 Do While item.IsValid()
40
41 PrintIndent(item)
42 If item.IsOpen Then
43 Console.Write("- {0:s} ACTION -> ", item.GetTitle())
44 Else
45 Console.Write("+ {0:s} ACTION -> ", item.GetTitle())
46 End If
47
48
49 ' Print Action
50 Dim action As pdftron.PDF.Action = item.GetAction()
51 If action.IsValid() Then
52 If action.GetType() = pdftron.PDF.Action.Type.e_GoTo Then
53 Dim dest As Destination = action.GetDest()
54 If (dest.IsValid()) Then
55 Dim page As Page = dest.GetPage()
56 Console.WriteLine("GoTo Page #{0:d}", page.GetIndex())
57 End If
58 Else
59 Console.WriteLine("Not a 'GoTo' action")
60 End If
61 Else
62 Console.WriteLine("NULL")
63 End If
64
65 If item.HasChildren() Then ' Recursively print children sub-trees
66 PrintOutlineTree(item.GetFirstChild())
67 End If
68 item = item.GetNext()
69 Loop
70 End Sub
71
72
73 Shared Sub RunTest()
74
75 PDFNet.Initialize(PDFTronLicense.Key)
76
77 ' Relative path to the folder containing test files.
78 Dim input_path As String = "../../../../TestFiles/"
79 Dim output_path As String = "../../../../TestFiles/Output/"
80
81
82 ' The following example illustrates how to create and edit the outline tree
83 ' using high-level Bookmark methods.
84 Try
85 Using doc1 As PDFDoc = New PDFDoc(input_path + "numbered.pdf")
86 doc1.InitSecurityHandler()
87
88
89 ' Lets first create the root bookmark items.
90 Dim red As Bookmark = Bookmark.Create(doc1, "Red")
91 Dim green As Bookmark = Bookmark.Create(doc1, "Green")
92 Dim blue As Bookmark = Bookmark.Create(doc1, "Blue")
93
94 doc1.AddRootBookmark(red)
95 doc1.AddRootBookmark(green)
96 doc1.AddRootBookmark(blue)
97
98 ' You can also add new root bookmarks using Bookmark.AddNext("...")
99 blue.AddNext("foo")
100 blue.AddNext("bar")
101
102 ' We can now associate new bookmarks with page destinations:
103
104 ' The following example creates an 'explicit' destination (see
105 ' section '8.2.1 Destinations' in PDF Reference for more details)
106 Dim red_dest As Destination = Destination.CreateFit(doc1.GetPage(1))
107 red.SetAction(pdftron.PDF.Action.CreateGoto(red_dest))
108
109 ' Create an explicit destination to the first green page in the document
110 green.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(10))))
111
112 ' The following example creates a 'named' destination (see
113 ' section '8.2.1 Destinations' in PDF Reference for more details)
114 ' Named destinations have certain advantages over explicit destinations.
115 Dim key As String = "blue1"
116 Dim blue_action As pdftron.PDF.Action = pdftron.PDF.Action.CreateGoto(key, Destination.CreateFit(doc1.GetPage(19)))
117
118 blue.SetAction(blue_action)
119
120 ' We can now add children Bookmarks
121 Dim sub_red1 As Bookmark = red.AddChild("Red - Page 1")
122 sub_red1.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(1))))
123 Dim sub_red2 As Bookmark = red.AddChild("Red - Page 2")
124 sub_red2.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(2))))
125 Dim sub_red3 As Bookmark = red.AddChild("Red - Page 3")
126 sub_red3.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(3))))
127 Dim sub_red4 As Bookmark = sub_red3.AddChild("Red - Page 4")
128 sub_red4.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(4))))
129 Dim sub_red5 As Bookmark = sub_red3.AddChild("Red - Page 5")
130 sub_red5.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(5))))
131 Dim sub_red6 As Bookmark = sub_red3.AddChild("Red - Page 6")
132 sub_red6.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(6))))
133
134 ' Example of how to find and delete a bookmark by title text.
135 Dim foo As Bookmark = doc1.GetFirstBookmark().Find("foo")
136 If foo.IsValid() Then
137 foo.Delete()
138 End If
139 Dim bar As Bookmark = doc1.GetFirstBookmark().Find("bar")
140 If bar.IsValid() Then
141 bar.Delete()
142 End If
143
144 ' Adding color to Bookmarks. Color and other formatting can help readers
145 ' get around more easily in large PDF documents.
146 red.SetColor(1, 0, 0)
147 green.SetColor(0, 1, 0)
148 green.SetFlags(2) ' set bold font
149 blue.SetColor(0, 0, 1)
150 blue.SetFlags(3) ' set bold and italic
151
152 doc1.Save(output_path + "bookmark.pdf", 0)
153 End Using
154 Console.WriteLine("Done. Result saved in bookmark.pdf")
155 Catch e As PDFNetException
156 Console.WriteLine(e.Message)
157 End Try
158
159
160 ' The following example illustrates how to traverse the outline tree using
161 ' Bookmark navigation methods: Bookmark.GetNext(), Bookmark.GetPrev(),
162 ' Bookmark.GetFirstChild () and Bookmark.GetLastChild ().
163 Try
164 ' Open the document that was saved in the previous code sample
165 Using doc1 As PDFDoc = New PDFDoc(output_path + "bookmark.pdf")
166 doc1.InitSecurityHandler()
167
168 Dim root As Bookmark = doc1.GetFirstBookmark()
169 PrintOutlineTree(root)
170 Console.WriteLine("Done.")
171 End Using
172 Catch e As PDFNetException
173 Console.WriteLine(e.Message)
174 End Try
175
176 ' The following example illustrates how to create a Bookmark to a page
177 ' in a remote document. A remote go-to action is similar to an ordinary
178 ' go-to action, but jumps to a destination in another PDF file instead
179 ' of the current file. See Section 8.5.3 'Remote Go-To Actions' in PDF
180 ' Reference Manual for details.
181 Try
182 Using doc1 As PDFDoc = New PDFDoc(output_path + "bookmark.pdf")
183 doc1.InitSecurityHandler()
184
185 ' Create file specification (the file referred to by the remote bookmark)
186 Dim file_spec As Obj = doc1.CreateIndirectDict()
187 file_spec.PutName("Type", "Filespec")
188 file_spec.PutString("F", "bookmark.pdf")
189
190 Dim spec As FileSpec = New FileSpec(file_spec)
191 Dim goto_remote As pdftron.PDF.Action = pdftron.PDF.Action.CreateGotoRemote(spec, 5, True)
192
193 Dim remoteBookmark1 As Bookmark = Bookmark.Create(doc1, "REMOTE BOOKMARK 1")
194 remoteBookmark1.SetAction(goto_remote)
195 doc1.AddRootBookmark(remoteBookmark1)
196
197 ' Create another remote bookmark, but this time using the low-level SDF/Cos API.
198 Dim remoteBookmark2 As Bookmark = Bookmark.Create(doc1, "REMOTE BOOKMARK 2")
199 doc1.AddRootBookmark(remoteBookmark2)
200 Dim gotoR As Obj = remoteBookmark2.GetSDFObj().PutDict("A")
201
202 gotoR.PutName("S", "GoToR") ' Set action type
203 gotoR.PutBool("NewWindow", True)
204
205 ' Set the file specification
206 gotoR.Put("F", file_spec)
207
208 ' Set the destination
209 Dim dest As Obj = gotoR.PutArray("D")
210 dest.PushBackNumber(9) ' Jump to the tenth page. Note that Acrobat indexes pages from 0.
211 dest.PushBackName("Fit") ' Fit the page
212
213 doc1.Save(output_path + "bookmark_remote.pdf", SDFDoc.SaveOptions.e_linearized)
214 End Using
215 Console.WriteLine("Done. Result saved in bookmark_remote.pdf")
216 Catch e As PDFNetException
217 Console.WriteLine(e.Message)
218 End Try
219 PDFNet.Terminate()
220 End Sub
221
222 End Class
223
224End Module
Did you find this helpful?
Trial setup questions?
Ask experts on DiscordNeed other help?
Contact SupportPricing or product questions?
Contact Sales