|Developer Tools||Office Productivity Applications
Bookmarks in a PDF document offer an interactive way for users to navigate to different parts of the document. When displayed in the bookmark panel of a viewer application such as Adobe® Reader, bookmarks in a PDF document can provide a visual representation of the table of contents.
In PDFOne Java, a bookmark is represented by the
PdfBookmark class. A
PdfBookmark object cannot be created with a normal constructor. We can however
In creation mode, you need to first access the root bookmark, which is a hidden bookmark, using the
PdfDocument.getBookmarkRoot() method. This method returns the root bookmark and you can use the method
PdfDocument.addBookmark(String title, PdfBookmark parent, int pageNo) to add all normal (visible) bookmarks under the root.
Apart from adding a bookmark under a specified parent, the method also returns the new bookmark object. We can then use this bookmark object to add other new bookmarks above or below or under it, as shown in the code snippet below.
// Create a blank document PdfDocument document = new PdfDocument(); // Set the document to display the bookmark // panel of viewer application by default document.setPageMode(PdfPageMode.USEOUTLINES); // Create three pages PdfPage page1 = new PdfPage(); PdfPage page2 = new PdfPage(); PdfPage page3 = new PdfPage(); // Write identifying text identifying the pages page1.writeText("This is first page. "); page2.writeText("This is the middle page. "); page3.writeText("This is the last page. "); // Add the pages to the document document.add(page1); document.add(page2); document.add(page3); // Obtain root bookmark PdfBookmark bmRoot = document.getBookmarkRoot(); // Create a branch of bookmarks under the root PdfBookmark bm1 = document.addBookmark("Page 1", bmRoot, 1); PdfBookmark bm11 = document.addBookmark("Page 2", bm1, 2); document.addBookmark("Page 3", bm11, 3); // Add a second bookmark under the root PdfBookmark bm3 = bm1.addNext("Page 3", 3); // Add a third bookmark under the root bm3.addPrevious("Page 2", 2); // Set the document to be displayed after // it is written to document.setOpenAfterSave(true); // Write the document to disk document.save("bookmark_doc1.pdf"); // Close all I/O streams document.close();
If we need to delete bookmarks in an existing document, we need to access the first bookmark in the document using the
PdfDocument.getFirstBookmark() method. After we do that, we can use the following methods from the
PdfBookmark to parse the document tree.
|getNext()||Get the next bookmark in that level.|
|getPrevious()||Get the previous bookmark in that level.|
|getFirstChild()||Get the first child bookmark. (There can be multiple child bookmarks.)|
|getParentNode()||Get the parent bookmark (There can be only one paernt.)|
If we are able to parse the bookmark tree, then we can use the following methods to delete bookmarks. Here, we need to bear in mind that we cannot destroy a self-destroy a bookmark nor have a parent bookmark deleted using a child bookmark.
|removeNext()||Remove the next bookmark in that level.|
|removePrevious()||Remove the previous bookmark in that level.|
|removeFirstChild()||Delete the first child bookmark.|
The code snippet below shows how to delete bookmarks.
// Create a document with bookmarks createPDFBookmarks(false); // Read the document PdfDocument document = new PdfDocument(); document.load("bookmark_doc1.pdf"); // Obtain the first visible bookmark // in the document PdfBookmark bm1 = document.getFirstBookmark(); // Delete children bookmarks of the first bookmark PdfBookmark bm11 = bm1.getFirstChild(); bm11.removeFirstchild(); bm1.removeFirstchild(); // Obtain the third bookmark, which comes // after the second bookmark PdfBookmark bm2 = bm1.getNext(); // Delete third bookmark under root bm2.removeNext(); // Delete the first bookmark under root bm2.removePrevious(); // Delete the second bookmark, which is the // only bookmark left in the document (document.getBookmarkRoot()).removeFirstchild(); document.setOpenAfterSave(true); // Write document to file document.save(); document.close();
With individual bookmarks, we can change their text, color, and style with which they are displayed in the bookmark panel.
|Our .NET Developer Tools|
Multi-format document-processing component suite for .NET developers.
A .NET PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, and bookmark PDF documents in .NET applications.
|Our Delphi/C++Builder developer tools|
Multi-format document-processing component suite for Delphi/C++Builder developers, covering both VCL and FireMonkey platforms.
A Delphi/C++Builder component suite for creating documents in over 20 formats and also export reports from popular Delphi reporting tools.
A Delphi/C++Builder component suite to edit, enhance, view, print, merge, split, encrypt, annotate, and bookmark PDF documents.
|Our Java developer tools|
Multi-format document-processing component suite for Java developers.
A Java PDF component suite to create, edit, view, print, reorganize, encrypt, annotate, bookmark PDF documents in Java applications.
|Our Platform-Agnostic Cloud and On-Premises APIs|
Cloud-hosted and On-Premises REST-based document-processing and document-viewing APIs
|Privacy | Legal | Feedback | Newsletter | Blog | Resellers||© 2002-2017 Gnostice Information Technologies Private Limited. All rights reserved.|