Working with Bookmarks

21 Jul 202618 minutes to read

Essential® PDF provides support to insert, remove, and modify the bookmarks in a PDF document.

To quickly get started with adding, modifying, and removing PDF bookmarks in .NET using the Syncfusion® PDF library, refer to this video tutorial:

Adding Bookmarks in a PDF

The PdfBookmarkBase collection represents the bookmarks in a PDF document. You can add a bookmark in a new PDF document using PdfBookmark class. Please refer the following code example.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create the document bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(page);
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create the document bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(page);
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

// Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive

' Create a new PDF document
Dim document As New PdfDocument()
' Add a page to the document
Dim page As PdfPage = document.Pages.Add()
' Create the document bookmark
Dim bookmark As PdfBookmark = document.Bookmarks.Add("Page 1")
' Set the destination page
bookmark.Destination = New PdfDestination(page)
' Set the destination location
bookmark.Destination.Location = New PointF(20, 20)
' Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Color = Color.Red

' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Adding bookmarks in an existing PDF document

To add bookmarks to an existing PDF document, use the following code example.

using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Create the document bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(document.Pages[0]);
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using System.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Create the document bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(document.Pages[0]);
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);

// Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the PDF document
Dim document As New PdfLoadedDocument("Input.pdf")
' Create the document bookmark
Dim bookmark As PdfBookmark = document.Bookmarks.Add("Page 1")
' Set the destination page
bookmark.Destination = New PdfDestination(document.Pages(0))
' Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Color = Color.Red
' Set the destination location
bookmark.Destination.Location = New PointF(20, 20)

' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Adding a child to the bookmarks

You can add a child bookmark by using the Insert method of the PdfBookmarkBase class. The following code example illustrates this.

using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create the parent bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(page);
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);
// Add a child bookmark to the parent
PdfBookmark childBookmark = bookmark.Insert(0, "heading 1");
childBookmark.Destination = new PdfDestination(page);
childBookmark.Destination.Location = new PointF(400, 300);
childBookmark.Destination.Zoom = 2F;
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using System.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;

// Create a new PDF document
PdfDocument document = new PdfDocument();
// Add a page to the document
PdfPage page = document.Pages.Add();
// Create the parent bookmark
PdfBookmark bookmark = document.Bookmarks.Add("Page 1");
// Set the destination page
bookmark.Destination = new PdfDestination(page);
// Set the destination location
bookmark.Destination.Location = new PointF(20, 20);
// Add a child bookmark to the parent
PdfBookmark childBookmark = bookmark.Insert(0, "heading 1");
childBookmark.Destination = new PdfDestination(page);
childBookmark.Destination.Location = new PointF(400, 300);
childBookmark.Destination.Zoom = 2F;
// Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;

// Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive

' Create a new PDF document
Dim document As New PdfDocument()
' Add a page to the document
Dim page As PdfPage = document.Pages.Add()
' Create the parent bookmark
Dim bookmark As PdfBookmark = document.Bookmarks.Add("Page 1")
' Set the destination page
bookmark.Destination = New PdfDestination(page)
' Set the destination location
bookmark.Destination.Location = New PointF(20, 20)
' Add a child bookmark to the parent
Dim childBookmark As PdfBookmark = bookmark.Insert(0, "heading 1")
childBookmark.Destination = New PdfDestination(page)
childBookmark.Destination.Location = New PointF(400, 300)
childBookmark.Destination.Zoom = 2.0F
' Set the text style and color
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Color = Color.Red

' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Inserting bookmarks in an existing PDF

When loading an existing document, Essential® PDF loads all bookmarks of the document.

Each loaded bookmark is represented by the PdfLoadedBookmark object. The following code example illustrates how to insert new bookmarks in an existing PDF document.

using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Insert a new bookmark into the existing bookmark collection
PdfBookmark bookmark = document.Bookmarks.Insert(1, "New Page 2");
// Set the destination page and location
bookmark.Destination = new PdfDestination(document.Pages[1]);
bookmark.Destination.Location = new PointF(0, 300);

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using System.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Insert a new bookmark into the existing bookmark collection
PdfBookmark bookmark = document.Bookmarks.Insert(1, "New Page 2");
// Set the destination page and location
bookmark.Destination = new PdfDestination(document.Pages[1]);
bookmark.Destination.Location = new PointF(0, 300);
// Save and close the PDF document
document.Save("Output.pdf");
document.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the PDF document
Dim document As New PdfLoadedDocument("Input.pdf")
' Insert a new bookmark into the existing bookmark collection
Dim bookmark As PdfBookmark = document.Bookmarks.Insert(1, "New Page 2")
' Set the destination page and location
bookmark.Destination = New PdfDestination(document.Pages(1))
bookmark.Destination.Location = New PointF(0, 300)
' Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Removing bookmarks from an existing PDF

You can remove bookmarks from an existing PDF document by using the Remove method of the PdfBookmarkBase class. The following code example illustrates this.

using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = document.Bookmarks;
// Remove a bookmark by its name
bookmarks.Remove("Page 1");
// Remove a bookmark by its index
bookmarks.RemoveAt(1);

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = document.Bookmarks;
// Remove a bookmark by its name
bookmarks.Remove("Page 1");
// Remove a bookmark by its index
bookmarks.RemoveAt(1);

// Save and close the document
document.Save("Output.pdf");
document.Close(true);
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the PDF document
Dim document As New PdfLoadedDocument("Input.pdf")
' Get the bookmark collection
Dim bookmarks As PdfBookmarkBase = document.Bookmarks
' Remove a bookmark by its name
bookmarks.Remove("Page 1")
' Remove a bookmark by its index
bookmarks.RemoveAt(1)

' Save and close the document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Modifying the bookmarks

Essential® PDF allows you to modify the bookmarks in an existing PDF document. The following modifications can be made to bookmarks in an existing document:

  • Modify the bookmark style, color, title, and destination.
  • Add or insert new bookmarks into the root collection.
  • Add or insert new bookmarks as a child of another bookmark.
  • Assign the destination of the added bookmarks to a loaded page or a new page of the document.

The following code example shows how to modify the Destination, Color, TextStyle, and Title of an existing bookmark.

using Syncfusion.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = document.Bookmarks;
// Get the first bookmark and modify its properties
PdfLoadedBookmark bookmark = bookmarks[0] as PdfLoadedBookmark;
bookmark.Destination = new PdfDestination(document.Pages[0]);
bookmark.Color = Color.Green;
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Title = "Changed title";

// Save the document
document.Save("Output.pdf");
// Close the document
document.Close(true);
using System.Drawing;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = document.Bookmarks;
// Get the first bookmark and modify its properties
PdfLoadedBookmark bookmark = bookmarks[0] as PdfLoadedBookmark;
bookmark.Destination = new PdfDestination(document.Pages[0]);
bookmark.Color = Color.Green;
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Title = "Changed title";

// Save and close the document
document.Save("Output.pdf");
document.Close(true);
Imports System.Drawing
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the PDF document
Dim document As New PdfLoadedDocument("Input.pdf")
' Get the bookmark collection
Dim bookmarks As PdfBookmarkBase = document.Bookmarks
' Get the first bookmark and modify its properties
Dim bookmark As PdfLoadedBookmark = TryCast(bookmarks(0), PdfLoadedBookmark)
bookmark.Destination = New PdfDestination(document.Pages(0))
bookmark.Color = Color.Green
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Title = "Changed title"

' Save and close the document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Getting the bookmark page index from an existing PDF document

You can get the page index of a bookmark from an existing PDF document using the PageIndex property of the PdfDestination class, as shown in the following code example.

using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
// Get the page index of the first bookmark
int index = bookmarks[0].Destination.PageIndex;

// Save the document
loadedDocument.Save("Output.pdf");
// Close the document
loadedDocument.Close(true);
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

// Load the PDF document
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Get the bookmark collection
PdfBookmarkBase bookmarks = loadedDocument.Bookmarks;
// Get the page index of the first bookmark
int index = bookmarks[0].Destination.PageIndex;

// Save the document
loadedDocument.Save("Output.pdf");
// Close the document
loadedDocument.Close(true);
Imports Syncfusion.Pdf.Interactive
Imports Syncfusion.Pdf.Parsing

' Load the PDF document
Dim loadedDocument As New PdfLoadedDocument("Input.pdf")
' Get the bookmark collection
Dim bookmarks As PdfBookmarkBase = loadedDocument.Bookmarks
' Get the page index of the first bookmark
Dim index As Integer = bookmarks(0).Destination.PageIndex

' Save and close the document
loadedDocument.Save("Output.pdf")
loadedDocument.Close(True)

You can download a complete working sample from GitHub.