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:
Install-Package Syncfusion.Pdf.Net.Core
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:
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.
Users can also create customized bookmarks for their PDF documents. Follow these steps:
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.
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:
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.
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:
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.
To remove any parent or child bookmark from a PDF document, please follow these steps:
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.
You can also get the bookmarks and their page indices by following these steps:
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.
For more details, refer to the examples in this GitHub repository.
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!