Articles in this section
Category / Section

How to display both page and bookmarks in the PDF document using C# and VB.NET

6 mins read

Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can display both pages and bookmarks in PDF documents.

Steps to display both pages and bookmarks in the PDF documents programmatically

  1. Create a new C# console application project.

Create console application

  1. Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from the NuGet.org.

NuGet package reference

  1. Include the following namespaces in the Program.cs file.

C#

using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Interactive;
using System.Drawing;

 

VB.NET

Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Interactive
Imports System.Drawing

 

  1. Use the following code snippet to display both page and bookmarks in the PDF document.

C#

//Creates a new document

PdfDocument document = new PdfDocument();

 

//Set to open bookmarks

document.ViewerPreferences.PageMode = PdfPageMode.UseOutlines;

 

//Add a page

PdfPage page = document.Pages.Add();

 

//Create parent bookmark

PdfBookmark bookmark = document.Bookmarks.Add("Page 1");

bookmark.Destination = new PdfDestination(page);

bookmark.Destination.Location = new PointF(20, 20);

 

//Add the child bookmark

PdfBookmark childBookmark = bookmark.Insert(0, "Heading 1");

childBookmark.Destination = new PdfDestination(page);

childBookmark.Destination.Location = new PointF(0, 300);

childBookmark.Destination.Zoom = 2F;

 

//Draw string

page.Graphics.DrawString("Hello World", new PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Black, new PointF(0, 0));         

 

//Sets the text style and color

bookmark.TextStyle = PdfTextStyle.Bold;

bookmark.Color = System.Drawing.Color.Red;

 

//Saves and closes the PDF document

document.Save("Output.pdf");

document.Close(true);

 

//This will open the PDF file so, the result will be seen in the default PDF viewer

System.Diagnostics.Process.Start("Output.pdf");

 

VB.NET

'Creates a New document
Dim document As PdfDocument = New PdfDocument()
 
'Set to open bookmarks
document.ViewerPreferences.PageMode = PdfPageMode.UseOutlines
 
'Add a page
Dim page As PdfPage = document.Pages.Add()
 
'Create parent bookmark 
Dim bookmark As PdfBookmark = document.Bookmarks.Add("Page 1")
bookmark.Destination = New PdfDestination(page)
bookmark.Destination.Location = New PointF(20, 20)
 
'Adds the child bookmark
Dim childBookmark As PdfBookmark = bookmark.Insert(0, "Heading 1")
childBookmark.Destination = New PdfDestination(page)
childBookmark.Destination.Location = New PointF(0, 300)
childBookmark.Destination.Zoom = 2.0F
 
'Draw String 
page.Graphics.DrawString("Hello World", New PdfStandardFont(PdfFontFamily.Helvetica, 20), PdfBrushes.Black, New PointF(0, 0))
 
'Sets the text style And color
bookmark.TextStyle = PdfTextStyle.Bold
bookmark.Color = System.Drawing.Color.Red
 
'Saves And closes the PDF document
document.Save("Output.pdf")
document.Close(True)
 
'This will open the PDF file so, the result will be seen in the default PDF viewer
System.Diagnostics.Process.Start("Output.pdf")

 

A complete working sample can be downloaded from BookmarkSample.zip.

 

By executing the program, you will get the PDF document as follows,

 

Output document screenshot

Take a moment to peruse the documentation, where you can find other options like adding child bookmarks, inserting bookmarks, removing, and modifying bookmarks in the PDF document with code example.

 

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 the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without trail message.

 

See Also:

Add bookmarks in PDF document

 

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