How to Modify Bookmarks in Existing PDF in C#, VB.NET

In order to distinguish nested bookmarks more clearly, you can set different font style and font color for parent and child bookmarks respectively. In this article, I’ll introduce how to modify bookmarks in the terms of font style, font color and text using Spire.PDF in C#, VB.NET.

Main Steps:

Step 1: Initialize a new object of PdfDocument and load the existing PDF file.

PdfDocument doc = new PdfDocument("Bookmark.Pdf");

Step 2: Get all bookmarks from the test file.

PdfBookmarkCollection bookmarks = doc.Bookmarks;

Step 3: Traverse very first-level bookmark in the bookmark tree. Change the properties of selected or all bookmarks, including title, color and display style.

foreach (PdfBookmark parentBookmark in bookmarks)
{
    //change "Word Bookmarks" to "M BookMark"
    bookmarks[0].Title = "Modified BookMarks";      
    //set the color of the bookmark
    parentBookmark.Color = Color.Black;
    parentBookmark.DisplayStyle = PdfTextStyle.Bold;
    //edit child bookmark of parent bookmark
    EditChildBookmark(parentBookmark);
}

Step 4: In step 3, there is a nested method predefined as below. By invoking this method, we are able to modify child bookmarks of the upper level bookmark. In this example, we have two nested level bookmarks under the first-level bookmark.

static void EditChildBookmark(PdfBookmark parentBookmark)
{
   foreach (PdfBookmark childBookmark in parentBookmark)
   {
        childBookmark.Color = Color.Brown;
        childBookmark.DisplayStyle = PdfTextStyle.Regular;
        EditChild2Bookmark(childBookmark);
    }
}
static void EditChild2Bookmark(PdfBookmark childBookmark)
{
    foreach (PdfBookmark child2Bookmark in childBookmark)
    {           
        child2Bookmark.Color = Color.LightSalmon;
        child2Bookmark.DisplayStyle = PdfTextStyle.Italic; 
    }
}

Step 5: Save and launch the file.

doc.SaveToFile("Result.Pdf");
System.Diagnostics.Process.Start("Result.Pdf");

Screenshot of Effect:

How to Modify Bookmarks in Existing PDF in C#, VB.NET

Entire Code:

[C#]
static void Main(string[] args)
{
    PdfDocument doc = new PdfDocument("Bookmark.Pdf");
    PdfBookmarkCollection bookmarks = doc.Bookmarks;       
    foreach (PdfBookmark parentBookmark in bookmarks)
    {
        //change "Word Bookmarks" to "M BookMark"
        bookmarks[0].Title = "Modified BookMarks";           
        //set the color of the bookmark
        parentBookmark.Color = Color.Black;
        parentBookmark.DisplayStyle = PdfTextStyle.Bold;     
        //edit child bookmark of parent bookmark
        EditChildBookmark(parentBookmark);
    }               
    doc.SaveToFile("Result.Pdf");
    System.Diagnostics.Process.Start("Result.Pdf");
}
static void EditChildBookmark(PdfBookmark parentBookmark)
{
   foreach (PdfBookmark childBookmark in parentBookmark)
   {
        childBookmark.Color = Color.Brown;
        childBookmark.DisplayStyle = PdfTextStyle.Regular;
        EditChild2Bookmark(childBookmark);
    }
}
static void EditChild2Bookmark(PdfBookmark childBookmark)
{
    foreach (PdfBookmark child2Bookmark in childBookmark)
    {           
        child2Bookmark.Color = Color.LightSalmon;
        child2Bookmark.DisplayStyle = PdfTextStyle.Italic;   
    }
}
[VB.NET]
Private Shared Sub Main(args As String())
	Dim doc As New PdfDocument("Bookmark.Pdf")
	Dim bookmarks As PdfBookmarkCollection = doc.Bookmarks
	For Each parentBookmark As PdfBookmark In bookmarks
		'change "Word Bookmarks" to "M BookMark"
		bookmarks(0).Title = "Modified BookMarks"
		'set the color of the bookmark
		parentBookmark.Color = Color.Black
		parentBookmark.DisplayStyle = PdfTextStyle.Bold
		'edit child bookmark of parent bookmark
		EditChildBookmark(parentBookmark)
	Next
	doc.SaveToFile("Result.Pdf")
	System.Diagnostics.Process.Start("Result.Pdf")
End Sub
Private Shared Sub EditChildBookmark(parentBookmark As PdfBookmark)
	For Each childBookmark As PdfBookmark In parentBookmark
		childBookmark.Color = Color.Brown
		childBookmark.DisplayStyle = PdfTextStyle.Regular
		EditChild2Bookmark(childBookmark)
	Next
End Sub
Private Shared Sub EditChild2Bookmark(childBookmark As PdfBookmark)
	For Each child2Bookmark As PdfBookmark In childBookmark
		child2Bookmark.Color = Color.LightSalmon
		child2Bookmark.DisplayStyle = PdfTextStyle.Italic
	Next
End Sub