Get, create or edit outlines (bookmarks) in C# and VB.NET

With GemBox.Pdf, you can get, create or edit PDF document outlines (bookmarks) in your C# or VB.NET application.

The following example demonstrates how to remove existing outlines (bookmarks) and create new ones in a PDF document.

Screenshot
PDF Library for C# / VB.NET Screenshot

See the full code below, use Run Example to execute.

Upload your file(Drag files here)

Download an input file

using System;
using System.Text;
using GemBox.Pdf;
using GemBox.Pdf.Content;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        PdfDocument document = PdfDocument.Load("LoremIpsum.pdf");

        // 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.
            PdfOutline 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.Save("Outlines.pdf");
        document.Close();
    }
}
Imports System.Text
Imports GemBox.Pdf
Imports GemBox.Pdf.Content

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As PdfDocument = PdfDocument.Load("LoremIpsum.pdf")

        ' Remove all bookmarks.
        document.Outlines.Clear()

        ' Get the number of pages.
        Dim numberOfPages As Integer = document.Pages.Count

        For i As Integer = 0 To numberOfPages - 1 Step 10
            ' Add a new outline item (bookmark) at the end of the document outline collection.
            Dim bookmark As PdfOutline = 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 j As Integer = 0 To Math.Min(10, numberOfPages - i) - 1
                ' 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)
            Next
        Next

        document.Save("Outlines.pdf")
        document.Close()

    End Sub

End Module