Add PDF Outline Bookmarks

A PDF outline, or bookmark, allows you to navigate to key pages of a PDF, similar to a Table of Contents. Including these in your C# project can be essential for usability and UX design.


Step 1

1. Use IronPDF for C#

To easily add outlines and bookmarks to your PDF, first you should install the IronPDF software library (free for development with this tutorial). You can access it through DLL download as well as learn more and install using the latest NuGet package. Open in Visual Studio and start the tutorial below.


 PM > Install-Package IronPdf

How to Tutorial

2. Add Outlines & Bookmarks

In Adobe Acrobat Reader, the outlines, or bookmarks are shown in the left sidebar.

IronPDF imports existing outlines from existing PDF documents and allows them to be added, re-ordered, and edited.

2.1 Add single layer of bookmarks

Adding a bookmark with IronPDF is as simple as the following. First, install IronPDF as outlined in the previous steps. Then add the following code:

/**
Add Outline Bookmark
anchor-add-outlines-bookmarks
**/

using IronPdf;

// Create a new PDF or edit an existing document
using PdfDocument pdf = PdfDocument.FromFile("existing.pdf");

// Add a bookmark
pdf.Bookmarks.AddBookMarkAtEnd("NameOfBookmark", 0);

// Add a sub-bookmark within the previous
pdf.Bookmarks.AddBookMarkAtEnd("NameOfSubBookmark", 1);
/**
Add Outline Bookmark
anchor-add-outlines-bookmarks
**/

using IronPdf;

// Create a new PDF or edit an existing document
using PdfDocument pdf = PdfDocument.FromFile("existing.pdf");

// Add a bookmark
pdf.Bookmarks.AddBookMarkAtEnd("NameOfBookmark", 0);

// Add a sub-bookmark within the previous
pdf.Bookmarks.AddBookMarkAtEnd("NameOfSubBookmark", 1);
'''
'''Add Outline Bookmark
'''anchor-add-outlines-bookmarks
'''*

Imports IronPdf

' Create a new PDF or edit an existing document
Private PdfDocument As using

' Add a bookmark
pdf.Bookmarks.AddBookMarkAtEnd("NameOfBookmark", 0)

' Add a sub-bookmark within the previous
pdf.Bookmarks.AddBookMarkAtEnd("NameOfSubBookmark", 1)
VB   C#

2.2 Add multiple layers of bookmarks

IronPDF also enable the ability to add bookmarks in tree structure. It is very useful in keeping the bookmarks navigable even with huge PDF document that contains all examination papers, sale reports, or receipt records from different date and location. The AddBookMarkAtEnd("Examination", 0) method actually return IPdfBookMark object that can then be used to add children bookmarks with Children.AddBookMarkAtStart("Date1", 0) or Children.AddBookMarkAtEnd("Date1", 0). The idea is clearly demonstrated in the code below:

/**
Add Outline Bookmark
anchor-add-outlines-bookmarks
**/

using IronPdf;

// Create a new PDF or edit an existing document
PdfDocument pdf = PdfDocument.FromFile("examination.pdf");

// Assign IPdfBookMark object to a variable
var mainBookmark = pdf.Bookmarks.AddBookMarkAtEnd("Examination", 0);

// Add bookmark for days
var date1Bookmark = mainBookmark.Children.AddBookMarkAtStart("Date1", 1);

// Add bookmark for type of test
var paperBookmark = date1Bookmark.Children.AddBookMarkAtStart("Paper", 1);
paperBookmark.Children.AddBookMarkAtStart("PersonA", 3);
paperBookmark.Children.AddBookMarkAtStart("PersonB", 4);

// Add bookmark for days
var date2Bookmark = mainBookmark.Children.AddBookMarkAtEnd("Date2", 5);

// Add bookmark for type of test
var computerBookmark = date2Bookmark.Children.AddBookMarkAtStart("Computer", 5);
computerBookmark.Children.AddBookMarkAtStart("PersonC", 6);
computerBookmark.Children.AddBookMarkAtStart("PersonD", 7);
/**
Add Outline Bookmark
anchor-add-outlines-bookmarks
**/

using IronPdf;

// Create a new PDF or edit an existing document
PdfDocument pdf = PdfDocument.FromFile("examination.pdf");

// Assign IPdfBookMark object to a variable
var mainBookmark = pdf.Bookmarks.AddBookMarkAtEnd("Examination", 0);

// Add bookmark for days
var date1Bookmark = mainBookmark.Children.AddBookMarkAtStart("Date1", 1);

// Add bookmark for type of test
var paperBookmark = date1Bookmark.Children.AddBookMarkAtStart("Paper", 1);
paperBookmark.Children.AddBookMarkAtStart("PersonA", 3);
paperBookmark.Children.AddBookMarkAtStart("PersonB", 4);

// Add bookmark for days
var date2Bookmark = mainBookmark.Children.AddBookMarkAtEnd("Date2", 5);

// Add bookmark for type of test
var computerBookmark = date2Bookmark.Children.AddBookMarkAtStart("Computer", 5);
computerBookmark.Children.AddBookMarkAtStart("PersonC", 6);
computerBookmark.Children.AddBookMarkAtStart("PersonD", 7);
'''
'''Add Outline Bookmark
'''anchor-add-outlines-bookmarks
'''*

Imports IronPdf

' Create a new PDF or edit an existing document
Private pdf As PdfDocument = PdfDocument.FromFile("examination.pdf")

' Assign IPdfBookMark object to a variable
Private mainBookmark = pdf.Bookmarks.AddBookMarkAtEnd("Examination", 0)

' Add bookmark for days
Private date1Bookmark = mainBookmark.Children.AddBookMarkAtStart("Date1", 1)

' Add bookmark for type of test
Private paperBookmark = date1Bookmark.Children.AddBookMarkAtStart("Paper", 1)
paperBookmark.Children.AddBookMarkAtStart("PersonA", 3)
paperBookmark.Children.AddBookMarkAtStart("PersonB", 4)

' Add bookmark for days
Dim date2Bookmark = mainBookmark.Children.AddBookMarkAtEnd("Date2", 5)

' Add bookmark for type of test
Dim computerBookmark = date2Bookmark.Children.AddBookMarkAtStart("Computer", 5)
computerBookmark.Children.AddBookMarkAtStart("PersonC", 6)
computerBookmark.Children.AddBookMarkAtStart("PersonD", 7)
VB   C#

3. Extract and Search Text & Images

Part of navigation may include searching for text, and is therefore relevant to outlines and bookmarks.

Let’s presume you have extracted text from a PDF document, and you still need to be able to search and find the extracted text. The finding is easy. The extraction of the heading tags, however, is trickier, as a PDF document contains no reference to the content it was generated from.

To extract text from a PDF document, make use of the ExtractTextFromPage method from the PDF document object along with the ExtractAllText method.

Here is a quick example on how to extract text from a PDF document:

/**
Extract and Search Text
anchor-extract-and-search-text-images
**/

using IronPdf;

// Create a new PDF or edit an existing document
PdfDocument PDF = PdfDocument.FromFile("file.pdf");

// Extract text from all pages of PDF document
string AllText = PDF.ExtractAllText();

for (var index = 0; index < PDF.PageCount; index++)
{
    int PageNumber = index + 1;

    // Extract text from specified page
    string Text = PDF.ExtractTextFromPage(index);

    //Search here!
}
/**
Extract and Search Text
anchor-extract-and-search-text-images
**/

using IronPdf;

// Create a new PDF or edit an existing document
PdfDocument PDF = PdfDocument.FromFile("file.pdf");

// Extract text from all pages of PDF document
string AllText = PDF.ExtractAllText();

for (var index = 0; index < PDF.PageCount; index++)
{
    int PageNumber = index + 1;

    // Extract text from specified page
    string Text = PDF.ExtractTextFromPage(index);

    //Search here!
}
'''
'''Extract and Search Text
'''anchor-extract-and-search-text-images
'''*

Imports IronPdf

' Create a new PDF or edit an existing document
Private PDF As PdfDocument = PdfDocument.FromFile("file.pdf")

' Extract text from all pages of PDF document
Private AllText As String = PDF.ExtractAllText()

For index = 0 To PDF.PageCount - 1
	Dim PageNumber As Integer = index + 1

	' Extract text from specified page
	Dim Text As String = PDF.ExtractTextFromPage(index)

	'Search here!
Next index
VB   C#

Now you can add search text easily with the various string methods provided in .NET

As a bonus, to extract Images, all you would need to use is the ExtractImagesFromPage method from the PDF document, object along with the ExtractAllImages method. And there you go!