IRONSOFTWAREHOME

Adding Bookmarks with ExtractTextFromPage

Curtis Chau
Curtis Chau
Updated: July 21, 2026

When you add bookmarks based on text content, searching with the Pages.Lines property can fail because the returned text comes back garbled or out of order, so a title-based search never matches. Extracting the page text with ExtractTextFromPage() reads each page in logical order and gives you a reliable string to search against.

Please note: On version 2026.5.x and above, use the LinkAnnotation class instead. It has built-in support for clickable links that navigate to a specific page and position, and is the recommended approach when available.

Pages.Lines reconstructs text from how it visually appears on the page. That breaks down when text is drawn in a non-linear order, when custom fonts or encodings are in play, or when the PDF was generated from HTML or a third-party tool with a complex layout. The result is incomplete or mixed-up text that you cannot search dependably.

Solution

1. Read each page with ExtractTextFromPage()

Pull the raw embedded text from every page rather than the visual reconstruction. ExtractTextFromPage() returns the page content in logical reading order, which makes a header search far more dependable across varying PDF structures.

2. Loop every page and bookmark each unique header

Walk all pages so no section is missed regardless of where it falls, and track which headers you have already seen so each one is bookmarked only once. A HashSet is a clean way to dedupe: given a document with sections titled "ARTICLE 1", "ARTICLE 2", and so on, it guarantees a single bookmark per article.

The output is a set of bookmarks pointing at each specified heading, even when the PDF came from a dynamic HTML source or carries complex formatting.

C# code adding PDF bookmarks per article header using ExtractTextFromPage and a HashSet

Curtis Chau
Technical Writer

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.

...
Read More

Ready to Get Started?

Nuget Downloads 21,105,021Version:2026.9just released

Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999

Key in blue circle

Get your free 30-day Trial Key instantly.

Your trial license will be sent to your email address

No limitations. 100% unlocked. No credit card.

OR
bullet_checkedNo credit card or account creation requiredNo limitations. 100% unlocked. No credit card.
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried Iron Suite
Book your free Live Demo
Booking Badge

Trusted by Millions of Engineers Worldwide

Iron Software's customer logos
Get Your No-Obligation Consult
Complete the form below or email sales@ironsoftware.com
Your details will always be kept confidential.
Trusted by Millions of Engineers Worldwide
Iron Software's customer logos
Get your free 30-day Trial Key instantly.
No credit card or account creation required
C# NuGet Library for PDF
Install with NuGet

Version: 2026.9

PM > Install-Package IronPdf
nuget.org/packages/IronPdf/
  1. In Solution Explorer, right-click References, Manage NuGet Packages
  2. Select Browse and search "IronPdf"
  3. Select the package and install
C# PDF DLL
Download DLL

Version: 2026.9

or download Windows Installer here.

  1. Download and unzip IronPDF to a location such as ~/Libs within your Solution directory
  2. In Visual Studio Solution Explorer, right click References. Select Browse, "IronPdf.dll"

Licenses from $999