Add Bookmark to PDF via Java

Manipulate Bookmarks in PDF document. Use Aspose.PDF for Java to modify PDF documents programmatically

How to work with Bookmarks in PDF Document with Java Library

Use the Aspose.PDF for Java for working with bookmarks. With bookmarks in PDF, you can quickly navigate to specific sections or pages, making it easier to search and access relevant content. Bookmarks in PDF improve the user interface by allowing you to navigate long or complex documents efficiently. Well-structured bookmarks in PDF can improve the appearance of a professional document. This is especially useful for textbooks, manuals, scientific papers, and reports. In presentations, bookmarks allow the user to seamlessly navigate between slides or sections. This feature can be useful for interactive and non-linear presentations. Working with bookmarks in PDF documents improves navigation, accessibility, and overall user experience. Use this feature in educational materials, scientific papers, manuals, or presentations. Bookmarks in PDF improve the quality, efficiency, and browsing experience of your documents. In order to add bookmarks to PDF, we’ll use Aspose.PDF for Java API which is a feature-rich, powerful, and easy-to-use conversion API for the Java platform. You can download its latest version directly from Maven and install it within your Maven-based project by adding the following configurations to the pom.xml.

Repository

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java AP</name>
    <url>https://releases.aspose.com/java/repo/</url>
</repository>

Dependency

<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-pdf</artifactId>
<version>version of aspose-pdf API</version>
</dependency>

Steps to work with Bookmarks via Java


You need Aspose.PDF for Java to try the code in your environment.

  1. Open a PDF document using Document object.
  2. Create a bookmark and define its properties.
  3. Add the OutlineItemCollection collection to the Outlines collection.
  4. Save the file again

This example opens a PDF document and creates a bookmark with certain properties via Java. It sets the target page for the bookmark using a ‘GoToAction’ object. After, add it to the document structure using ‘OutlineItemCollection’, and then save the modified document bookmark to a new PDF file. This is the best solution for improving the documents navigation functions.

Add a Bookmark to PDF Document - Java

This sample code shows how to add bookmark to PDF - Java


    Document pdfDocument = new Document(GetDataDir() + "AddBookmark.pdf");

    // Create a bookmark object
    OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.getOutlines());
    pdfOutline.setTitle("Test Outline");
    pdfOutline.setItalic(true);
    pdfOutline.setBold(true);

    // Set the destination page number
    pdfOutline.setAction(new GoToAction(pdfDocument.getPages().get_Item(2)));

    // Add a bookmark in the document's outline collection.
    pdfDocument.getOutlines().add(pdfOutline);

    // Save the update document
    pdfDocument.save(_dataDir + "AddBookmark_out.pdf");