Bookmarks are an essential feature in PDF documents, providing a convenient way to navigate through the content. They enhance the user experience by allowing quick access to specific sections of a document. In this article, we will explore how to incorporate bookmarks in PDF documents using C# and .NET, leveraging the capabilities of the SautinSoft PDF.Net library.
Here are a few ways bookmarks can help in the user experience:
Step-by-step guide:
Complete code
using System;
using SautinSoft.Pdf;
using System.IO;
class Program
{
/// <summary>
/// Bookmarks.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/bookmarks.php
/// </remarks>
static void Main()
{
// Before starting this example, please get a free 100-day trial key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// PdfDocument.SetLicense("...");
using (var document = new PdfDocument())
{
document.Pages.Add();
// Remove all bookmarks.
document.Outlines.Clear();
// Get the number of pages.
int numberOfPages = document.Pages.Count;
for (int i = 0; i < numberOfPages; i += 10)
{
// Add a new outline item (bookmark) at the end of the document outline collection.
var bookmark = document.Outlines.AddLast(string.Format("PAGES {0}-{1}", i + 1, Math.Min(i + 10, numberOfPages)));
// Set the explicit destination on the new outline item (bookmark).
bookmark.SetDestination(document.Pages[i], PdfDestinationViewType.FitRectangle, 0, 0, 100, 100);
for (int j = 0; j < Math.Min(10, numberOfPages - i); j++)
// Add a new outline item (bookmark) at the end of parent outline item (bookmark) and set the explicit destination.
bookmark.Outlines.AddLast(string.Format("PAGE {0}", i + j + 1)).SetDestination(document.Pages[i + j], PdfDestinationViewType.FitPage);
}
document.PageMode = PdfPageMode.UseOutlines;
document.Save("Bookmarks.pdf");
}
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("Bookmarks.pdf") { UseShellExecute = true });
}
}
Option Infer On
Imports System
Imports SautinSoft.Pdf
Imports System.IO
Friend Class Program
''' <summary>
''' Bookmarks.
''' </summary>
''' <remarks>
''' Details: https://sautinsoft.com/products/pdf/help/net/developer-guide/bookmarks.php
''' </remarks>
Shared Sub Main()
' Before starting this example, please get a free 100-day trial key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' PdfDocument.SetLicense("...");
Using document = New PdfDocument()
document.Pages.Add()
' Remove all bookmarks.
document.Outlines.Clear()
' Get the number of pages.
Dim numberOfPages As Integer = document.Pages.Count
Dim i As Integer = 0
Do While i < numberOfPages
' Add a new outline item (bookmark) at the end of the document outline collection.
Dim bookmark = document.Outlines.AddLast(String.Format("PAGES {0}-{1}", i + 1, Math.Min(i + 10, numberOfPages)))
' Set the explicit destination on the new outline item (bookmark).
bookmark.SetDestination(document.Pages(i), PdfDestinationViewType.FitRectangle, 0, 0, 100, 100)
Dim j As Integer = 0
Do While j < Math.Min(10, numberOfPages - i)
' Add a new outline item (bookmark) at the end of parent outline item (bookmark) and set the explicit destination.
bookmark.Outlines.AddLast(String.Format("PAGE {0}", i + j + 1)).SetDestination(document.Pages(i + j), PdfDestinationViewType.FitPage)
j += 1
Loop
i += 10
Loop
document.PageMode = PdfPageMode.UseOutlines
document.Save("Bookmarks.pdf")
End Using
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo("Bookmarks.pdf") With {.UseShellExecute = True})
End Sub
End Class
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: