Articles in this section
Category / Section

How to create hierarchical bookmarks in a PDF file using C# and VB.NET?

7 mins read

A bookmark is a type of link with representative text in the navigation panel. Each bookmark goes to a different view or page in the document. Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can create hierarchical bookmarks in a PDF file using C# and VB.NET.

Steps to create hierarchical bookmarks in PDF programmatically:

  1. Create a new C# Windows Forms application project. Create a console application project
  2. Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org. NuGet package reference screenshot
  3. Include the following namespaces in the Form1.Designer.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 create hierarchical bookmarks in PDF document.

C#

#region CreatePDF
    //Creates a new PDF document
    document = new PdfDocument();
    //Set the Font
    font = new PdfStandardFont(PdfFontFamily.Helvetica, 10f);
    for (int i = 1; i <= 3; i++)
    {
        PdfPage pages = document.Pages.Add();
 
        //Add bookmark in PDF document
        PdfBookmark bookmark = AddBookmark(pages, "Chapter " + i, new PointF(10, 10));
        //Add sections to bookmark
        PdfBookmark section1 = AddSection(bookmark, pages, "Section " + i + ".1", new PointF(30, 30), false);
        PdfBookmark section2 = AddSection(bookmark, pages, "Section " + i + ".2", new PointF(30, 400), false);
 
        //Add subsections to section
        PdfBookmark subsection1 = AddSection(section1, pages, "Paragraph " + i + ".1.1", new PointF(50, 50), true);
        PdfBookmark subsection2 = AddSection(section1, pages, "Paragraph " + i + ".1.2", new PointF(50, 150), true);
        PdfBookmark subsection3 = AddSection(section1, pages, "Paragraph " + i + ".1.3", new PointF(50, 250), true);
        PdfBookmark subsection4 = AddSection(section2, pages, "Paragraph " + i + ".2.1", new PointF(50, 420), true);
        PdfBookmark subsection5 = AddSection(section2, pages, "Paragraph " + i + ".2.2", new PointF(50, 560), true);
        PdfBookmark subsection6 = AddSection(section2, pages, "Paragraph " + i + ".2.3", new PointF(50, 680), true);
    }
    //Save the document
    document.Save("HierarchicalBookmarks.pdf");
    //Close the document
    document.Close(true);
    //This will open the PDF file so, the result will be seen in default PDF viewer 
    Process.Start("HierarchicalBookmarks.pdf");
#endregion
 
#region fields      
private PdfDocument document;
private PdfFont font;
#endregion

 

VB.NET

#Region "CreatePDF"
        'Creates a new PDF document
        document = New PdfDocument()
        'Set the Font
        font = New PdfStandardFont(PdfFontFamily.Helvetica, 10.0F)
        For i As Integer = 1 To 3
            Dim pages As PdfPage = document.Pages.Add()
            'Add bookmark in PDF document
            Dim bookmark As PdfBookmark = AddBookmark(pages, "Chapter " & i, New PointF(10, 10))
 
            'Add sections in PDF document
            Dim section1 As PdfBookmark = AddSection(bookmark, pages, "Section " & i & ".1", New PointF(30, 30), False)
            Dim section2 As PdfBookmark = AddSection(bookmark, pages, "Section " & i & ".2", New PointF(30, 400), False)
            'Add subsections in PDF document
            Dim subsection1 As PdfBookmark = AddSection(section1, pages, "Paragraph " & i & ".1.1", New PointF(50, 50), True)
            Dim subsection2 As PdfBookmark = AddSection(section1, pages, "Paragraph " & i & ".1.2", New PointF(50, 150), True)
            Dim subsection3 As PdfBookmark = AddSection(section1, pages, "Paragraph " & i & ".1.3", New PointF(50, 250), True)
            Dim subsection4 As PdfBookmark = AddSection(section2, pages, "Paragraph " & i & ".2.1", New PointF(50, 420), True)
            Dim subsection5 As PdfBookmark = AddSection(section2, pages, "Paragraph " & i & ".2.2", New PointF(50, 560), True)
            Dim subsection6 As PdfBookmark = AddSection(section2, pages, "Paragraph " & i & ".2.3", New PointF(50, 680), True)
        Next
        'Save the document
        document.Save("HierarchicalBookmarks.pdf")
        'Close the document
        document.Close(True)
        'This will open the PDF file so, the result will be seen in default PDF viewer 
        Process.Start("HierarchicalBookmarks.pdf")
#End Region
 
#Region "Fields"
    Private document As PdfDocument
    Private font As PdfFont
#End Region

 

  1. Add the following code in AddBookmark and AddSection method to add bookmark.

C#

private PdfBookmark AddBookmark(PdfPage page, string title, PointF point)
{
    PdfGraphics graphics = page.Graphics;
    //Add bookmark in PDF document
    PdfBookmark bookmarks = document.Bookmarks.Add(title);
    //Draw the content in the PDF page
    graphics.DrawString(title, font, PdfBrushes.Black, new PointF(point.X, point.Y));
    bookmarks.Destination = new PdfDestination(page);
    bookmarks.Destination.Location = point;
    return bookmarks;
}
 
public PdfBookmark AddSection(PdfBookmark bookmark, PdfPage page, string title, PointF point, bool isSubSection)
{
    PdfGraphics graphics = page.Graphics;
    //Add bookmark in PDF document
    PdfBookmark bookmarks = bookmark.Add(title);
    //Draw the content in the PDF page
    graphics.DrawString(title, font, PdfBrushes.Black, new PointF(point.X, point.Y));
    bookmarks.Destination = new PdfDestination(page);
    bookmarks.Destination.Location = point;
    return bookmarks;
}

 

VB.NET

Private Function AddBookmark(ByVal page As PdfPage, ByVal title As String, ByVal point As PointF) As PdfBookmark
        Dim graphics As PdfGraphics = page.Graphics
        'Add bookmark in PDF document
        Dim bookmarks As PdfBookmark = document.Bookmarks.Add(title)
        'Draw the content in the PDF page
        graphics.DrawString(title, font, PdfBrushes.Black, New PointF(point.X, point.Y))
        bookmarks.Destination = New PdfDestination(page)
        bookmarks.Destination.Location = point
        Return bookmarks
    End Function
    
    Public Function AddSection(ByVal bookmark As PdfBookmark, ByVal page As PdfPage, ByVal title As String, ByVal point As PointF, ByVal isSubSection As Boolean) As PdfBookmark
        Dim graphics As PdfGraphics = page.Graphics
        'Add bookmark in PDF document
        Dim bookmarks As PdfBookmark = bookmark.Add(title)
        'Draw the content in the PDF page
        graphics.DrawString(title, font, PdfBrushes.Black, New PointF(point.X, point.Y))
        bookmarks.Destination = New PdfDestination(page)
        bookmarks.Destination.Location = point
        Return bookmarks
    End Function

 

A complete working sample can be downloaded from HierarchicalBookmarkSample.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, inserting, removing, and modifying bookmarks in an existing PDF document and features like named destination and document Link Annotation with code examples.

Refer here to explore the rich set of Syncfusion Essential PDF features.

An online sample link to add bookmarks with different style in the PDF document.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.


Conclusion

I hope you enjoyed learning about how to create hierarchical bookmarks in a PDF file using C# and VB.NET.

You can refer to our .NET PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our .NET PDF example 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 check out 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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