Articles in this section
Category / Section

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

2 mins read

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 in .NET PDF library
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference in .NET PDF library
  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 in .NET PDF library

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.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied