Add Bookmark to PDF via C#

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

How to work with Bookmarks in PDF Document with C# Library

Use the Aspose.PDF for .NET 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 in PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for net platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console

PM > Install-Package Aspose.PDF

Steps to work with Bookmarks via C#


You need Aspose.PDF for .NET 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 C#. 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 - C#

This sample code shows how to add bookmark to PDF - C#


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

// Create a bookmark object
OutlineItemCollection pdfOutline = new OutlineItemCollection(pdfDocument.Outlines);
pdfOutline.Title = "Test Outline";
pdfOutline.Italic = true;
pdfOutline.Bold = true;
// Set the destination page number
pdfOutline.Action = new GoToAction(pdfDocument.Pages[1]);
// Add bookmark in the document's outline collection.
pdfDocument.Outlines.Add(pdfOutline);

dataDir = dataDir + "AddBookmark_out.pdf";
// Save output
pdfDocument.Save(dataDir);