Working with PDF bookmarks in C#

Introduction

Recently I often read PDF with huge pages in my daily work and study. It is very inconvenient to go back to the last place to read continually next time. I searched the internet and found that there are solutions to add bookmarks and processing bookmarks in PDF from code. Based on my tests, finally I chose Free Spire.PDF to help me to add bookmarks, for it is free and easy to use. The following article will give detailed information of my project. Wish it helps.

Application Overview

Firstly, the free PDF API is listed on Codeplex, downloading and installing for Free Spire.PDF is simple and clean. You can get the .msi file directly from there. When you finish the installing, you can go to "SampleCenter" and API Help, which give you brief and clear information for the usage of free Spire.PDF. In this tutorial, I am focus on introducing PDF bookmark with the following functions:

  1. How to add bookmarks to PDF file.
  2. How to expand and collapse the PDF bookmarks.
  3. How to get the PDF bookmark information.
  4. How to remove bookmarks from PDF file.

Tools we need

Visual Studio

Spire.PDF has a bin folder, which contains .NET2.0, .NET3.5, .NET4.0 and NET4.0 ClientProfile. Firstly, add the correct dll based on your own projects. I use .NET 4.0 for this project.

 

Namespaces to be used

  1. using System.Drawing;  
  2. using Spire.Pdf;   

Step 1: Adding Bookmarks to the PDF document

As known that there are two kinds of bookmarks in PDF files, parent bookmark and child bookmark. Free Spire.PDF supports to add both of them. You can also set the color and font for the bookmark format. Checked the bookmarks added by Free Spire.PDF as below:


  1. //Create PDf document and load from the file  
  2. PdfDocument pdf = new PdfDocument();  
  3. pdf.LoadFromFile("Sample.pdf");  
  4.   
  5. for (int i = 0; i < pdf.Pages.Count; i++)  
  6. {  
  7.     //Add parent bookmark to each PDF page at the location (0,0)  
  8.     PdfDestination destination = new PdfDestination(pdf.Pages[i], new PointF(0, 0));  
  9.     PdfBookmark bookmark = pdf.Bookmarks.Add(string.Format("Bookmark-{0}", i));  
  10.     bookmark.Color = Color.SaddleBrown;  
  11.     bookmark.DisplayStyle = PdfTextStyle.Bold;  
  12.     bookmark.Action = new PdfGoToAction(destination);  
  13. }    
  14.     //Add child bookmark at the location (0, pagesize.height/2) under each parent bookmark  
  15.     PdfDestination childDestination = new PdfDestination(pdf.Pages[i], new PointF(0, pdf.Pages[i].Size.Height / 2));  
  16.     PdfBookmark childBookmark = bookmark.Add(string.Format("ChildBookmark-{0}", i + 1));  
  17.     childBookmark.Color = Color.Coral;  
  18.     childBookmark.DisplayStyle = PdfTextStyle.Italic;  
  19.     childBookmark.Action = new PdfGoToAction(childDestination);   

Step 2: Expand and collapse the PDF bookmarks

Now there are only three bookmarks and it is easy to get all of them. But when the PDF page contain lots of bookmarks, the feature of collapse and expand the bookmarks will help you a lot. With them, the bookmarks will looks tidy and easy to find. By using only one line of code, you can set the collapse and expand bookmarks easily.

  1. //expand is true  
  2. pdf.ViewerPreferences.BookMarkExpandOrCollapse = false;  

Step 3: Get the PDF bookmark information

When we set the PDF bookmarks, we can get the title of the bookmarks. Free Spire.PDF offers a class called Spire.Pdf.Bookmarks.PdfBookmarkCollection. Using this class, we can collect all the parent bookmarks in PDF file. After that, we need to make clear about whether there are child bookmarks in PDF file. If it has bookmark, we can use the methods provided by Microsoft: System.Console.WriteLine(string value) to get the title of bookmarks.

  1. //Get the information from parent bookmarks   
  2. PdfBookmarkCollection bookmarks = pdf.Bookmarks;  
  3. if (bookmarks.Count > 0)  
  4. {  
  5.     foreach (PdfBookmark bookmark in bookmarks)  
  6.     {  
  7.         Console.WriteLine(bookmark.Title);  
  8.         InputBookmarkAllTitle(bookmark);  
  9.     }  
  10. }  
  11. Console.ReadLine();   
  12. //Get the information from child bookmarks  
  13. static void InputBookmarkAllTitle(PdfBookmark parentBookmark)  
  14. {  
  15.     if (parentBookmark.Count > 0)  
  16.     {  
  17.         foreach (PdfBookmark childBookmark in parentBookmark)  
  18.         {  
  19.             Console.WriteLine(childBookmark.Title);  
  20.             InputBookmarkAllTitle(childBookmark);    
  21.         }  
  22.     }  

 

Step 4: Delete bookmark from the PDF document

When we need to remove a particular bookmark or all the bookmarks in the PDF file, we can also do it by using it.

  1. //remove the first bookmark  
  2. pdf.Bookmarks.RemoveAt(0);  
  3.   
  4. //remove all bookmarks  
  5. pdf.Bookmarks.Clear(); 

Conclusion

Free Spire.PDF also offers many other powerful features, such as converting HTML, RTF, XPS, Text and Image to PDF documents with efficient performance, it also support to work with PDF annotations, comments, and hyperlinks, etc. It is well worth your try on it.