Easily Create, Update, and Remove Bookmarks in PDFs Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (180).NET Core  (28).NET MAUI  (226)Angular  (113)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (41)Black Friday Deal  (1)Blazor  (236)BoldSign  (14)DocIO  (24)Essential JS 2  (110)Essential Studio  (199)File Formats  (72)Flutter  (136)JavaScript  (226)Microsoft  (121)PDF  (83)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (963)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (163)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (10)Business intelligence  (55)Button  (4)C#  (164)Chart  (147)Chart of the week  (58)Cloud  (15)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (73)Development  (679)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (21)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  (522)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (45)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (405)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (627)What's new  (340)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