PDF Bookmarks and Outlines - Add/Edit/Read - PHP Sample Code

Sample code to use Apryse SDK for programmatically reading and editing existing outline items, and for creating new PDF bookmarks using the high-level API. Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our Server SDK and PDF Editing & Manipulation Library.

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?>

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales