Add a bookmark to PDFs on Server/Desktop
To add a bookmark or a new outline item in an existing document.
PDFDoc doc = new PDFDoc(filename);
Bookmark red = Bookmark.Create(doc, "My Item");
doc.AddRootBookmark(red);PDFDoc doc(filename);
Bookmark myitem = Bookmark::Create(doc, "My Item");
doc.AddRootBookmark(myitem);doc := NewPDFDoc(filename)
myItem := BookmarkCreate(doc, "My Item")
doc.AddRootBookmark(myItem)PDFDoc doc = new PDFDoc(filename);
Bookmark myitem = Bookmark.create(doc, "My Item");
doc.addRootBookmark(myitem);async function main() {
const doc = await PDFNet.PDFDoc.createFromURL(filename);
const myitem = await PDFNet.Bookmark.create(doc, 'My Item');
doc.addRootBookmark(myitem);
}
PDFNet.runWithCleanup(main);val doc = PDFDoc(filename)
val myitem = Bookmark.create(doc, "My Item")
doc.addRootBookmark(myitem)Read, add, edit PDF outlines and bookmarks Full code sample which illustrates how to read and edit existing outline items and create new bookmarks using the high-level API. Code sample is available in C++, C#, Java, Python, Go, PHP, Ruby & VB.
About adding a bookmark
While it's possible to work with outline items or Bookmarks using the SDF API (See section 8.2.2, "Document Outline", in the PDF Reference Manual for more details), Apryse SDK simplifies the process by providing the high-level utility class PDF.Bookmark.
Note that we obtain the root Bookmark using GetFirstBookmark(). If GetFirstBookmark() returns an invalid Bookmark (where GetFirstBookmark().IsValid() is false), the document has no outline tree.
Sub-items can be added using the Bookmark.AddChild() method:
Bookmark sub_item = myitem.AddChild("My Sub-Item");Bookmark sub_item = myitem.AddChild("My Sub-Item");subItem = myitem.AddChild("My Sub-Item")Bookmark sub_item = myitem.addChild("My Sub-Item");async function main() {
const sub_item = await myitem.addNewChild('My Sub-Item');
}
PDFNet.runWithCleanup(main);val sub_item = myitem.addChild("My Sub-Item")PTBookmark *sub_item = [myitem AddChildWithTitle: @"My Sub-Item"];let sub_item: PTBookmark = myitem.addChild(withTitle: "My Sub-Item")$sub_item = $myitem->AddChild("My Sub-Item");sub_item = myitem.AddChild("My Sub-Item")sub_item = myitem.AddChild("My Sub-Item")Dim sub_item As Bookmark = myitem.AddChild("My Sub-Item")Note that a Bookmark can be associated with different kinds of Actions. The most common action is to move to another location in the current document. This type of Action is called a Go-To action. The following sample creates a new page Destination and sets the Bookmark's action:
myitem.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))));myitem.SetAction(Action::CreateGoto(Destination::CreateFit(doc.GetPage(1))));myItem.SetAction(ActionCreateGoto(DestinationCreateFit(doc.GetPage(1))))myitem.setAction(Action.createGoto(Destination.createFit(doc.getPage(1))));async function main() {
myitem.setAction(await PDFNet.Action.createGoto(await PDFNet.Destination.createFit(await doc.getPage(1))));
}
PDFNet.runWithCleanup(main);myitem.action = Action.createGoto(Destination.createFit(doc.getPage(1)))[myitem SetAction: [PTAction CreateGoto: [PTDestination CreateFit: [doc GetPage: 1]]]];myitem.setAction(PTAction.createGoto(PTDestination.createFit(doc.getPage(1))))$myitem->SetAction(Action::CreateGoto(Destination::CreateFit($doc->GetPage(1))));myitem.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))))myitem.SetAction(Action.CreateGoto(Destination.CreateFit(doc.GetPage(1))))myitem.SetAction(pdftron.PDF.Action.CreateGoto(Destination.CreateFit(doc1.GetPage(1))))Using Apryse SDK, it is also possible to quickly create named destinations. Named destinations have an advantage over explicit destinations — they allow the location of the destination to change without invalidating existing links.
To create a named destination, pass the key (under which the destination will be stored) to the Action.Create() method:
Last updated
Was this helpful?