How to display both page and bookmarks in the PDF document using C# and VB.NET?
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
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from the NuGet.org.
- 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
- 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,
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 the 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 a trial message.
See Also:
Conclusion:
I hope you enjoyed learning about how to display both page and bookmarks in the PDF document using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to explore our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!