Getting started

keyboard_arrow_down

Basics

keyboard_arrow_down

Fundamentals

keyboard_arrow_down
Best practicesWebViewer HTML structureConfig filesUnderstanding ReaderControlMobile viewerThe streaming optionThe useDownloader optionUnderstanding coordinates

Components

keyboard_arrow_down

Annotations

keyboard_arrow_down

Advanced

keyboard_arrow_down

WebViewer UI (beta)

keyboard_arrow_down

PDFNetJS Full

keyboard_arrow_down

PDF Processing API

keyboard_arrow_down
What is PDFNet?Opening a documentSerializing (saving) a documentWorking with pagesWorking with Page ContentWorking with BookmarksWorking with Interactive Forms (AcroForms)PDF SecurityLow-level PDF APIError handling
Working with Bookmarkskeyboard_arrow_down

Working with Bookmarks

A PDF document may display a document outline on the screen, allowing the user to navigate interactively from one part of the document to another. The outline consists of a tree-structured hierarchy of Bookmarks (sometimes called outline items), which serve as a "visual table of contents" to display the document's structure to the user.

Each Bookmark has a title that appears on screen, and an Action that specifies what happens when a user clicks on the Bookmark. The typical Action for a user-created Bookmark is to move to another location in the current document — although any Action can be specified.

While it's possible to work with outline items using the SDF API (See section 8.2.2, "Document Outline", in the PDF Reference Manual for more details), PDFNet simplifies the process by providing the high-level utility class PDF.Bookmark.

You can use Bookmark.GetNext(), Bookmark.GetPrev(), Bookmark.GetFirstChild() and Bookmark.GetLastChild() to navigate the whole outline tree, as the following shows:

// Prints out the outline tree to the standard output
            
void PrintIdent(Bookmark item) 
{
  int ident = item.GetIdent() - 1;
  for (int i=0; i < ident; ++i) 
  Console.Write("  ");
}

void PrintOutlineTree(Bookmark item)
{
  for (; item.IsValid(); item=item.GetNext())
  {
    PrintIdent(item);
    Console.WriteLine("{0:s}{1:s}", 
      (item.IsOpen() ? "- " : "+ "), item.GetTitle());
    if (item.HasChildren())     
    {
      // Recursively print child subtrees
      PrintOutlineTree(item.GetFirstChild());
    }
  }
}

static void Main(string[] args)
{
  PDFDoc doc = new PDFDoc("../../../Data/out1.pdf");
  doc.InitSecurityHandler();

  Bookmark root = doc.GetFirstBookmark();
  PrintOutlineTree(root);
}

Note that we obtain the root Bookmark using GetFirstBookmark(). If GetFirstBookmark() returns an invalid Bookmark (where GetFirstBookmark().IsValid() is false), the document has no outline tree. A new outline three can be created as follows:

PDFDoc doc = new PDFDoc("../Data/in.pdf");
doc.InitSecurityHandler();

Bookmark myitem = Bookmark.Create(doc, "My Item");
doc.AddRootBookmark(myitem);

Sub-items can be added using the Bookmark.AddChild() method:

Bookmark sub_item = myitem.AddChild("My Sub-Item");
myitem.AddChild("My Sub-Item 2");

Note that a Bookmark can be associated with different kinds of Actions. The most common action is to move to another location in the current document. This type of Action is called a Destination Action (See section 8.2.1, "Destinations", in the PDF Reference Manual for more details). The following sample creates a new page Destination and sets the Bookmark's action:

// The following example creates an 'explicit' destination
Destination dest = Destination.CreateFit(doc.GetPage(1));
Action action = Action.CreateGoto(dest);
myitem.SetAction(action);

Using PDFNet, it is also possible to quickly create named destinations (see section 8.2.1 "Destinations" in the PDF Reference for more details). Named destinations have an advantage over explicit destinations — they allow the location of the destination to change without invalidating existing links.

To create a named destination, pass the key (under which the destination will be stored) to the Action.Create() method:

Action blue_action 
  = Action.CreateGoto(
      "blue1", 
      Destination.CreateFit(doc.GetPage(1));

The Bookmarks class also allows you to quickly find Bookmarks based on the title text. For example, the following code snippet looks for a Bookmark called foo and then removes it from the outline tree:

Bookmark foo = doc.GetFirstBookmark().Find("foo");
if (foo.IsValid()) 
{
  foo.Delete();
}

The Bookmark API allows you to change any property on an outline item, including title text, action, color, and formatting. Color and other formatting can help readers navigate large PDF documents more easily. The following code adjusts color and formatting properties on three Bookmark items:

red.SetColor(1, 0, 0);
green.SetColor(0, 1, 0);

// use bold font for green title text
green.SetFlags(2);
blue.SetColor(0, 0, 1);

// use bold and italic font for blue title text
blue.SetFlags(3);