Easily Create, Update, and Remove Bookmarks in PDFs Using C#
Tags: fileformats, csharp, pdf
Published: 2023-06-19 04:02:12

---
title: Easily Create, Update, and Remove Bookmarks in PDFs Using C#
published: true
date: 2023-06-15 10:45:10 UTC
tags: fileformats, csharp, pdf
canonical_url: https://www.syncfusion.com/blogs/post/bookmarks-in-pdf-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44tldeojrwd0hjq3pvks.png
---
Bookmarks in PDFs are links or pointers that function as a table of contents, providing an easy way to navigate to specific sections in a document.

In this article, we will use the [Syncfusion .NET PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library "Syncfusion .NET PDF Library") to create, update, and remove bookmarks in PDF documents using C#. With the help of the .NET PDF Library, we can efficiently manage and manipulate PDF documents to enhance the reading experience of our audience.

**Agenda:**

- [Add bookmarks to a PDF document.](#add)
- [Customize the bookmark style.](#customize)
- [Insert bookmarks in an existing PDF document.](#insert)
- [Modify bookmarks.](#modify)
- [Remove bookmarks from a PDF document.](#remove)
- [Get bookmarks and their page indices from a PDF.](#get)

## Getting started with app creation

1.First, create a .NET console application using [Visual Studio](https://visualstudio.microsoft.com/ "Visual Studio").

![Select the Console App option](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Select-the-Console-App-option.png)

2.Then, navigate to **Tools** > **NuGet Package Manager** > **Package Manager Console**.
3.Execute the following command to install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core "Syncfusion.Pdf.Net.Core NuGet package") NuGet package.
```
Install-Package Syncfusion.Pdf.Net.Core
```

## <a name="add">Add bookmarks to the PDF document</a>

With the help of our .NET PDF Library, you can easily add parent and child bookmarks to both new and existing PDF documents.

Follow these steps to add bookmarks to an existing PDF document:

1.Use the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class to load the PDF document.
2.Create a bookmark using the [PdfBookmark](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfBookmark.html "PdfBookmark Class of .NET PDF Library") class.
3.Set the bookmark destination using the [PdfDestination](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfDestination.html "PdfDestination Class of .NET PDF Library") class. This will represent a specific target location within a PDF file where we need to add the bookmark, such as a specific area on a page.
4.Finally, save the PDF document to the disk using the **Save** method.

The following code example shows how to add bookmarks in a PDF document using C#.

```csharp
//Load an existing PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//Creates parent bookmark.
PdfBookmark bookmark = document.Bookmarks.Add("Chapter 1");
//Sets the destination page.
bookmark.Destination = new PdfDestination(document.Pages[1]);
//Sets the text style and color for the parent bookmark. 
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Red;
//Sets the destination location for the parent bookmark. 
bookmark.Destination.Location = new PointF(20, 20);

//Adds the child bookmark.
PdfBookmark childBookmark = bookmark.Insert(0, “Section 1”);
//Sets the destination location for the child bookmark. 
childBookmark.Destination = new PdfDestination(document.Pages[1]);
childBookmark.Destination.Location = new PointF(0, 200);

//Save the document to the memory stream. 
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document. 
document.Close(true);

```

By executing this code example, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Bookmarks-in-a-PDF-document.png" alt="Bookmarks in a PDF document" style="width:100%">

  <figcaption>Bookmarks in a PDF document</figcaption>

</figure>

## <a name="customize">Customize the bookmark style</a>

Users can also create customized bookmarks for their PDF documents. Follow these steps:

1.Use the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class to load a PDF document.
2.Create a bookmark using the [PdfBookmark](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfBookmark.html "PdfBookmark Class of .NET PDF Library") class.
3.Then, set the bookmark destination using the [PdfDestination](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfDestination.html "PdfDestination Class of .NET PDF Library") class.
4.Set the color and text style for the bookmark using the **Color** and **TextStyle** properties of **PdfBookmark** class.
5.Finally, save the PDF document to disk using the **Save** method.

The following code example shows how to customize bookmarks in a PDF document using C#.

```csharp
//Load an existing PDF document. 
FileStream docStream = new FileStream(“Input.pdf”, FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
 
//Creates parent bookmark. 
PdfBookmark bookmark = document.Bookmarks.Add("Chapter 1");
//Sets the destination page.
bookmark.Destination = new PdfDestination(document.Pages[1]);
//Sets the text style and color for the parent bookmark.  
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.DarkBlue;
//Sets the destination location for the parent bookmark.  
bookmark.Destination.Location = new PointF(20, 20);
 
//Adds the child bookmark. 
PdfBookmark childBookmark = bookmark.Insert(0, "Section 1");
//Sets the text style and color for the child bookmark.  
childBookmark.TextStyle = PdfTextStyle.Italic;
childBookmark.Color = Color.Orange;
//Sets the destination location for the child bookmark.  
childBookmark.Destination = new PdfDestination(document.Pages[1]);
childBookmark.Destination.Location = new PointF(0, 200);
 
//Creates parent bookmark. 
PdfBookmark bookmark2 = document.Bookmarks.Add(“Chapter 2”);
//Sets the destination page. 
bookmark2.Destination = new PdfDestination(document.Pages[1]);
//Sets the text style and color for parent bookmark.  
bookmark2.TextStyle = PdfTextStyle.Bold;
bookmark2.Color = Color.DarkBlue;
//Sets the destination location for parent bookmark.  
bookmark2.Destination.Location = new PointF(0, 500);
 
//Adds the child bookmark. 
PdfBookmark childBookmark2 = bookmark2.Insert(0, "Section 1");
//Sets the text style and color for the child bookmark.  
childBookmark2.TextStyle = PdfTextStyle.Italic;
childBookmark2.Color = Color.Orange;
//Sets the destination location for the child bookmark.  
childBookmark2.Destination = new PdfDestination(document.Pages[1]);
childBookmark2.Destination.Location = new PointF(0, 500);

//Save the document to memory stream. 
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document. 
document.Close(true);
```

By executing the previous code example, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Customizing-the-bookmark-style-in-a-PDF.png" alt="Customizing the bookmark style in a PDF" style="width:100%">

  <figcaption>Customizing the bookmark style in a PDF</figcaption>

</figure>

## <a name="insert">Inserting bookmarks into the existing bookmark collection</a>

The .NET PDF Library enables us to insert bookmarks at any location based on the bookmark collection. Each loaded bookmark is represented by the [PdfLoadedBookmark](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfLoadedBookmark.html "PdfLoadedBookmark Class of .NET PDF Library") class.

Follow these simple steps to add bookmarks to a PDF document:

1.Load the PDF document using the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class.
2.Insert a new bookmark in the existing bookmark collection using the **Insert** method of the [PdfBookmarkBase](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfBookmarkBase.html "File Formats PdfBookmarkBase Class") class.
3.Set the bookmark destination using the [PdfDestination](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfDestination.html "PdfDestination Class of .NET PDF Library") class.
4.Finally, save the modified PDF document to the disk using the **Save** method.

The following code example shows inserting a bookmark into a PDF document using C#.

```csharp
//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//Inserts a new bookmark in the existing bookmark collection.
PdfBookmark bookmark = document.Bookmarks.Insert(0, "Title Page");
//Sets the destination page and location.
bookmark.Destination = new PdfDestination(document.Pages[0]);
bookmark.Destination.Location = new PointF(0, 0);
//Sets the text style and color.
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Color = Color.Green;

//Save the document to memory stream. 
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Closes the document. 
document.Close(true);
```

By executing this code example, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Add-bookmarks-to-an-existing-PDF-document-at-any-location-1.png" alt="Add bookmarks to an existing PDF document at any location" style="width:100%">

  <figcaption>Add bookmarks to an existing PDF document at any location</figcaption>

</figure>

## <a name="modify">Modify bookmarks</a>

Syncfusion’s .NET PDF Library supports modifying bookmarks in PDF documents. You can perform the following modifications to the bookmarks in a PDF document using our .NET PDF Library:

- Modify the bookmark style, color, title, and destination.
- Add new bookmarks to the existing bookmark collection.
- Add new bookmarks as children of another bookmark.
- Assign the destination of the added bookmarks to an existing page or a new document page.

The following code example shows how to modify bookmarks in a PDF document using C#.

```csharp
//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//Gets all the bookmarks.
PdfBookmarkBase bookmarks = document.Bookmarks;
//Gets the first bookmark and changes the properties of the bookmark.
PdfLoadedBookmark bookmark = bookmarks[0] as PdfLoadedBookmark;
bookmark.Destination = new PdfDestination(document.Pages[2]);
bookmark.Color = Color.Green;
bookmark.TextStyle = PdfTextStyle.Bold;
bookmark.Title = "Chapter 2";

//Save the document to memory stream. 
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document. 
document.Close(true);
```

By executing this code example, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Modify-and-update-the-bookmarks-in-this-PDF-document.png" alt="Modify and update the bookmarks in this PDF document" style="width:100%">

  <figcaption>Updating a bookmark in a PDF</figcaption>

</figure>

## <a name="remove">Removing bookmarks from a PDF document</a>

To remove any parent or child bookmark from a PDF document, please follow these steps:

1.Load the existing PDF document using the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class.
2.Obtain the bookmark collection from the PDF document using the [PdfBookmarkBase](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfBookmarkBase.html "PdfBookmarkBase Class of .NET PDF Library") class.
3.Use the **Remove (string bookmarkName)** or **RemoveAt (int bookmarkIndex)** method to remove the bookmark.
4.Finally, save the modified PDF document to the disk using the **Save** method.

Refer to the following code example to remove bookmarks from a PDF document using C#.

```csharp
//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);

//Gets all the bookmarks.
PdfBookmarkBase bookmarks = document.Bookmarks;

//Remove parent bookmark using index.
bookmarks.RemoveAt(1);

//Remove child bookmark with bookmark name. 
PdfLoadedBookmark parentBookmark = bookmarks[0] as PdfLoadedBookmark;
parentBookmark.Remove("Section 4");

//Save the document to memory stream. 
MemoryStream stream = new MemoryStream();
document.Save(stream);
//Close the document. 
document.Close(true);
```

By executing the previous code example, you will get a PDF like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Remove-bookmarks-from-the-existing-PDF-document-using-C.png" alt="Remove bookmarks from the existing PDF document using C#" style="width:100%">

  <figcaption>Removing a bookmark from a PDF</figcaption>

</figure>

## <a name="get">Get bookmarks and their page indices from a PDF document
</a>

You can also get the bookmarks and their page indices by following these steps:

1.First, load the existing PDF document using the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument Class of .NET PDF Library") class.
2.Now, retrieve the bookmark collection from the document using the [PdfBookmarkBase](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfBookmarkBase.html "PdfBookmarkBase Class of .NET PDF Library") class.
3.Get the bookmark page index by accessing the **PageIndex** property of the [PdfDestination](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfDestination.html "PdfDestination Class of .NET PDF Library") class.
4.Finally, save the modified PDF document using the **Save** method.

Refer to the following code example to get bookmarks and their page indices from a PDF document using C#.

```csharp
//Load the PDF document.
FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read);
PdfLoadedDocument document = new PdfLoadedDocument(docStream);
//Gets all the bookmarks.
PdfBookmarkBase bookmark = document.Bookmarks;
//Get the bookmark page index.
int index = bookmark[0].Destination.PageIndex;
//Close the document.
document.Close(true);
```

By executing this code example, you will get output like in the following screenshot.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Getting-bookmarks-page-index-from-a-PDF.png" alt="Getting bookmark’s page index from a PDF" style="width:100%">

  <figcaption>Getting bookmark’s page index from a PDF</figcaption>

</figure>

## GitHub reference

For more details, refer to the examples in this [GitHub repository](https://github.com/SyncfusionExamples/Create-update-and-remove-bookmarks-in-PDF-document "Create, update and remove bookmarks in PDF using C# GitHub demo").

## Conclusion

Thanks for reading! In this blog, we’ve seen how to create, update, and remove bookmarks in PDF documents using [Syncfusion’s .NET PDF Library](https://www.syncfusion.com/document-processing/pdf-framework/net "Syncfusion’s .NET PDF Library"). Please let us know in the comments below if you have any questions about these features.

Take a moment to look at the [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-bookmarks "Working with bookmarks in .NET PDF Library"), where you will find other options and features, all with accompanying code samples.

You can also contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/pdf "Syncfusion Feedback Portal"). We are always happy to assist you!

## Related blogs

- [Creating an ASP.NET Minimal Web API to Generate PDF Documents from an HTML Template Dynamically](https://www.syncfusion.com/blogs/post/asp-net-minimal-web-api-to-generate-pdf-documents-from-html-template.aspx "Blog: Creating an ASP.NET Minimal Web API to Generate PDF Documents from an HTML Template Dynamically")
- [Generate Dynamic PDF Reports from an HTML Template Using C#](https://www.syncfusion.com/blogs/post/generate-dynamic-pdf-reports-from-html-template-using-csharp.aspx "Blog: Generate Dynamic PDF Reports from an HTML Template Using C#")
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx "Blog: 7 Ways to Compress PDF Files in C#, VB.NET")
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx "Blog: Easy Ways to Redact PDFs Using C#")