Easily Create, Update, and Remove Bookmarks in PDFs Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Create, Update, and Remove Bookmarks in PDFs Using C#

Easily Create, Update, and Remove Bookmarks in PDFs Using C#

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 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:

Say goodbye to tedious PDF tasks and hello to effortless document processing with Syncfusion's PDF Library.

Getting started with app creation

  1. First, create a .NET console application using Visual Studio.

    Select the Console App option

  2. Then, navigate to Tools > NuGet Package Manager > Package Manager Console.
  3. Execute the following command to install the Syncfusion.Pdf.Net.Core NuGet package.
    Install-Package Syncfusion.Pdf.Net.Core

Add bookmarks to the PDF document

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 class to load the PDF document.
  2. Create a bookmark using the PdfBookmark class.
  3. Set the bookmark destination using the PdfDestination 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#.

//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.

Bookmarks in a PDF document
Bookmarks in a PDF document

Boost your productivity with Syncfusion's PDF Library! Gain access to invaluable insights and tutorials in our extensive documentation.

Customize the bookmark style

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

  1. Use the PdfLoadedDocument class to load a PDF document.
  2. Create a bookmark using the PdfBookmark class.
  3. Then, set the bookmark destination using the PdfDestination 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#.

//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.

Customizing the bookmark style in a PDF
Customizing the bookmark style in a PDF

Inserting bookmarks into the existing bookmark collection

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 class.

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

  1. Load the PDF document using the PdfLoadedDocument class.
  2. Insert a new bookmark in the existing bookmark collection using the Insert method of the PdfBookmarkBase class.
  3. Set the bookmark destination using the PdfDestination 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#.

//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.

Add bookmarks to an existing PDF document at any location
Add bookmarks to an existing bookmark collection in a PDF

Witness the advanced capabilities of Syncfusion's PDF Library with feature showcases.

Modify bookmarks

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#.

//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.

Modify and update the bookmarks in this PDF document
Updating a bookmark in a PDF

Removing bookmarks from a PDF document

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

  1. Load the existing PDF document using the PdfLoadedDocument class.
  2. Obtain the bookmark collection from the PDF document using the PdfBookmarkBase 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#.

//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.

Remove bookmarks from the existing PDF document using C#
Removing a bookmark from a PDF

Get bookmarks and their page indices from a PDF document

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

  1. First, load the existing PDF document using the PdfLoadedDocument class.
  2. Now, retrieve the bookmark collection from the document using the PdfBookmarkBase class.
  3. Get the bookmark page index by accessing the PageIndex property of the PdfDestination 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#.

//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.

Getting bookmark’s page index from a PDF
Getting bookmark’s page index from a PDF

GitHub reference

For more details, refer to the examples in this GitHub repository.

Syncfusion’s high-performance PDF Library allows you to create PDF documents from scratch without Adobe dependencies.

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. Please let us know in the comments below if you have any questions about these features.

Take a moment to look at the documentation, where you will find other options and features, all with accompanying code samples.

You can also contact us through our support forum, support portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed