)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to get the page number from a PDF bookmark using C# and VB.NET

Platform: WinForms |
Control: PDF |
Published Date: January 25, 2016 |
Last Revised Date: May 6, 2019
Tags: pdf, bookmark

Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can get the page number from a PDF bookmark using C# and VB.NET.

Steps to get the page number from a PDF bookmark programmatically:

  1. Create a new C# console application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference
  3. Use the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Parsing
Imports Syncfusion.Pdf.Interactive

 

  1. Use the following code snippet to get the page number from a PDF bookmark.

C#

//Load the PDF document
PdfLoadedDocument document = new PdfLoadedDocument("Input.pdf");
//Create the dictionary 
Dictionary<PdfPageBase, int> pages = new Dictionary<PdfPageBase, int>();
//Iterates through the pages
for (int i = 0; i < document.Pages.Count; i++)
{
    PdfPageBase page = document.Pages[i] as PdfPageBase;
    //Add the page and index to dictionary 
    pages.Add(page, i);
}
//Get all the bookmarks
PdfBookmarkBase bookmarks = document.Bookmarks;
//Iterates through bookmarks
foreach (PdfBookmark bookmark in bookmarks)
{
    //Get the bookmark destination page
    PdfPageBase page = bookmark.Destination.Page;
    if (pages.ContainsKey(page))
    {
        //Get the page number of the bookmark
        int index = pages[page];
        Console.WriteLine("Bookmark Name: "+bookmark.Title + ", Page no: " + (index + 1));
    }
}
//Close the document
document.Close(true);
Console.ReadLine();

 

VB.NET

'Load the PDF document
Dim document As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Create the dictionary 
Dim pages As Dictionary(Of PdfPageBase, Integer) = New Dictionary(Of PdfPageBase, Integer)()
'Iterates through the pages
For i As Integer = 0 To document.Pages.Count - 1
    Dim page As PdfPageBase = TryCast(document.Pages(i), PdfPageBase)
    'Add the page and index to dictionary 
    pages.Add(page, i)
Next
'Get all bookmarks
Dim bookmarks As PdfBookmarkBase = document.Bookmarks
 
'Iterates through bookmarks
For Each bookmark As PdfBookmark In bookmarks
    'Get the bookmark destination page
    Dim page As PdfPageBase = bookmark.Destination.Page
    If pages.ContainsKey(page) Then
        Dim index As Integer = pages(page)
        Console.WriteLine("Bookmark Name: " & bookmark.Title + ", Page no: " & (index + 1))
    End If
Next
'Close the document
document.Close(True)
Console.ReadLine()

 

You can download the working sample from PdfBookmarkSample.zip.

By executing the program, you will get the console window as follows. Console window screenshot

Take a moment to peruse the documentation, where you can find other options like adding, inserting, removing, and modifying bookmarks in an existing PDF document and features like named destination and Document Link Annotation with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon