Bookmark Navigation in WPF Pdf Viewer

18 Mar 20245 minutes to read

PDF Viewer control allows users to navigate to the bookmarks present in the loaded PDF document at UI level.

Steps to perform bookmark navigation in PdfViewerControl.

  1. Open the bookmarks contained PDF document to enable the bookmark button in PdfViewerControl.
  2. Clicking on the bookmark button from the left pane, will list the bookmarks present in the PDF document.
  3. To jump to a specific section, click its name in the bookmark pane.
  4. If the bookmark has some children, you can explore them by clicking on the “+” button to the left of it.

WPF PDF Viewer Bookmark Button

Programmatically navigate to a bookmark destination

The user can navigate to the desired bookmark destination using the GoToBookmark method of PDF Viewer after loading the document. You should provide the target/destination bookmark as the parameter to this method. Refer to the following code sample to retrieve the bookmarks collection in the document and navigate to a bookmark.

void GoToBookmark()
{
    //Get the loadedDocument object from PDF Viewer
    PdfLoadedDocument pdfLoadedDocument = pdfViewer.LoadedDocument;
    //Get the complete bookmarks in the PDF.
    PdfBookmarkBase bookmarks = pdfLoadedDocument.Bookmarks;
    //In this example, we get the first bookmark in the PDF bookmarks collection at the index of 0.
    PdfBookmark firstBookmark = bookmarks[0];

    //Navigates to the first bookmark present in the PDF.
    pdfViewer.GoToBookmark(firstBookmark);
}

The user can also navigate to the child bookmarks in the PDF Viewer. Please refer to the following code sample to retrieve the child bookmarks in the document and navigate to the bookmark.

void GoToChildBookmark()
{
    //Get the loadedDocument object from PDF Viewer
    PdfLoadedDocument pdfLoadedDocument = pdfViewer.LoadedDocument;
    //Get the complete bookmarks in the PDF.
    PdfBookmarkBase bookmarks = pdfLoadedDocument.Bookmarks;
    //Gets the fourth bookmark in the PDF bookmarks collection at the index of 3.
    PdfBookmark fourthBookmark = bookmarks[3];
    //Check whether it has child bookmarks.
    if (fourthBookmark.Count > 0)
    {
        //Navigates to the first child of the fourth bookmark in the PDF.
        pdfViewer.GoToBookmark(bookmarks[3][0]);
    }
}

The user needs to call the above methods after the document is loaded. You can wire the DocumentLoaded event of PDF Viewer to notify you when the document is loaded, as shown in the following code sample:

//Constructor
public MainWindow()
{
    InitializeComponent();
    //Wire the DocumentLoaded event of PDF Viewer
    pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
    //Load the PDF file in PDF Viewer.
    pdfViewer.Load("../../LayerAndBookmarks.pdf");
}

//Handle the DocumentLoaded event of PDF Viewer
private void PdfViewer_DocumentLoaded(object sender, System.EventArgs args)
{
    //Call the logic for bookmark navigation
    GoToBookmark();
}

Enabling and disabling bookmark feature

You can enable and disable the bookmark button from the built-in toolbar using the IsBookmarkEnabled property available in PdfViewerControl.

Property Action

IsBookmarkEnabled

Enables or disables the bookmark feature.

This property removes the bookmark button and disable the bookmark feature, when it is set to false and vice versa.

//Bookmark feature is disabled
pdfViewerControl.IsBookmarkEnabled = false;
'Bookmark feature is disabled
pdfViewerControl.IsBookmarkEnabled = false

The following screenshot shown the Bookmark navigation in PdfViewerControl,

WPF PDF Viewer Enabling and Disabling Bookmark Feature

Expand bookmark panel Programmatically

The bookmark are displayed by clicking the bookmark icon in the left panel.To display bookmark panel from code behind, use the following code example.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
   PdfLoadedDocument pdf = new PdfLoadedDocument("Input.pdf");
   pdfviewer.Load(pdf);
   //By default, the bookmark panel IsExpanded is false. 
   pdfviewer.BookMarkSettings.IsExpanded = true;
}
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
    Dim pdf As New PdfLoadedDocument(Input.pdf)
    pdfViewer.Load(pdf)
    'By default, the bookmark panel IsExpanded is false. 
	pdfviewer.BookMarkSettings.IsExpanded = true
End Sub

NOTE

You can refer to our WPF PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our WPF PDF Viewer example to know how to render and configure the pdfviewer.