Create and Extract PDF Bookmarks in .NET

PDF bookmarks are extremely helpful when working with PDF files with a lot of content. Adding bookmarks provides a better user experience for the reader. No one wants to spend time searching through pages and pages of text looking for specific headers and sub-headers just so they can find what they’re looking for. The LEADTOOLS PDF SDK libraries give developers functions to create PDF bookmarks as well as extract PDF bookmark information.

The LEADTOOLS PDFBookmark structure contains the properties of a single PDF bookmark. The collection of bookmarks in the file is what makes the PDF’s table of contents and provides ease for the reader to navigate the document.

Bookmarks contain three properties that represent its appearance:

  • Title: This is the text to appear to the user
  • Level: The bookmark’s indentation level. Level 0 is a root bookmark, level 1 is a child of the previous bookmark, level 2 is a child of the child and so on.
  • BookmarkStyle: The bookmark’s font style


Create PDF Bookmarks

static void createBookmarks(string fileName)
{
    PDFFile file = new PDFFile(fileName);
    var bookmarks = new List<PDFBookmark>();

    file.Load();

    for (int i = 0; i < file.Pages.Count; i++)
    {
        PDFFilePage page = file.Pages[i];

        PDFBookmark bookmark = new PDFBookmark();
        bookmark.Title = "Goto page " + page.PageNumber.ToString();
        bookmark.BookmarkStyle = PDFBookmarkStyle.Plain;
        bookmark.Level = 2;
        bookmark.TargetPageNumber = page.PageNumber;
        bookmark.TargetPageFitType = PDFPageFitType.Default;
        bookmark.TargetPosition = new PDFPoint(0, page.Height);
        bookmark.TargetZoomPercent = 0;
        bookmarks.Add(bookmark);
    }
    file.WriteBookmarks(bookmarks, pdfFile2);
    Console.WriteLine("--- Created bookmarks ---" + Environment.NewLine);
}

Extract PDF Bookmark Information

To extract the bookmarks, you will need to use the PDFDocument.ParseDocumentStructure method and then access the PDFDocument.Bookmarks and PDFDocument.InternalLinks collections.

static void readBookmarks(string fileName)
{
    using (PDFDocument document = new PDFDocument(fileName))
    {        
        document.ParseDocumentStructure(PDFParseDocumentStructureOptions.Bookmarks);

        Console.WriteLine($"{document.Bookmarks.Count} bookmarks found:");
        foreach (PDFBookmark bookmark in document.Bookmarks)
        {
            Console.WriteLine($"Title: {bookmark.Title}, Level: {bookmark.Level}, Target page: {bookmark.TargetPageNumber}");
        }
    }
}

Try it!

To test this for yourself, you can download my C# .NET console project. Make sure to get the latest LEADTOOLS SDK code for free straight from our site if you have not already. This trial is good for 60 days and comes with unlimited chat and email support.

Support

Need help getting this sample up and going? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team (sales@leadtools.com) or call us at 704-332-5532.

About 

Technical Marketing Engineer

This entry was posted in PDF and tagged , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *