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:
- How to add bookmarks to PDF file.
- How to expand and collapse the PDF bookmarks.
- How to get the PDF bookmark information.
- 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
- using System.Drawing;
- 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:
-
- PdfDocument pdf = new PdfDocument();
- pdf.LoadFromFile("Sample.pdf");
-
- for (int i = 0; i < pdf.Pages.Count; i++)
- {
-
- PdfDestination destination = new PdfDestination(pdf.Pages[i], new PointF(0, 0));
- PdfBookmark bookmark = pdf.Bookmarks.Add(string.Format("Bookmark-{0}", i));
- bookmark.Color = Color.SaddleBrown;
- bookmark.DisplayStyle = PdfTextStyle.Bold;
- bookmark.Action = new PdfGoToAction(destination);
- }
-
- PdfDestination childDestination = new PdfDestination(pdf.Pages[i], new PointF(0, pdf.Pages[i].Size.Height / 2));
- PdfBookmark childBookmark = bookmark.Add(string.Format("ChildBookmark-{0}", i + 1));
- childBookmark.Color = Color.Coral;
- childBookmark.DisplayStyle = PdfTextStyle.Italic;
- 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.
-
- 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.
-
- PdfBookmarkCollection bookmarks = pdf.Bookmarks;
- if (bookmarks.Count > 0)
- {
- foreach (PdfBookmark bookmark in bookmarks)
- {
- Console.WriteLine(bookmark.Title);
- InputBookmarkAllTitle(bookmark);
- }
- }
- Console.ReadLine();
-
- static void InputBookmarkAllTitle(PdfBookmark parentBookmark)
- {
- if (parentBookmark.Count > 0)
- {
- foreach (PdfBookmark childBookmark in parentBookmark)
- {
- Console.WriteLine(childBookmark.Title);
- InputBookmarkAllTitle(childBookmark);
- }
- }
- }
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.
-
- pdf.Bookmarks.RemoveAt(0);
-
-
- 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.