<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V2.0//EN" "http://forrest.apache.org/dtd/document-v20.dtd">
<document> 
  <header> 
    <title>PDFBox - PDF Bookmarks</title> 
  </header> 
  <body> 
    <section>
      <title>Accessing Bookmarks</title>
      <p>
      See package:<a href="../javadoc/org/pdfbox/pdmodel/interactive/documentnavigation/outline/package-summary.html">org.pdfbox.pdmodel.interactive.documentnavigation.outline</a> <br/>
      See example:<a href="../javadoc/org/pdfbox/examples/pdmodel/PrintBookmarks.html">PrintBookmarks</a>
      </p>
      <p>
      	A PDF can contain an outline of a document and jump to pages within a PDF document.  
      	An outline is a hierarchical tree structure of nodes that point to pages.
      </p>
      
      <p>
      To access the root of the outline you go through the 
      <a href="../javadoc/org/pdfbox/pdmodel/PDDocumentCatalog.html">PDDocumentOutline</a>
      </p>
      <source>
      PDDocument doc = PDDocument.load( ... );
      PDDocumentOutline root = doc.getDocumentCatalog().getDocumentOutline();
	  </source>
	  <p>
	  Now you can traverse the tree using the getFirstChild() and getNextSibling() functions.
	  </p>
      <source>
      PDOutlineItem item = root.getFirstChild();
      while( item != null )
      {
          System.out.println( "Item:" + item.getTitle() );
          PDOutlineItem child = item.getFirstChild();
          while( child != null )
          {
              System.out.println( "    Child:" + child.getTitle() );
              child = child.getNextSibling();
          }
          item = item.getNextSibling();
      }
      </source>
    </section>
    <section>
      <title>Creating Bookmarks</title>
      <p>
      <p>
      See example:<a href="../javadoc/org/pdfbox/examples/pdmodel/CreateBookmarks.html">CreateBookmarks</a>
      </p>
      Creating bookmarks is just as easy.  You first need to create the PDDocumentOutline and then
      add some PDOutlineItem objects to it.
      </p>
      <source>
      //first create the document outline and add it to the page
      PDDocumentOutline outline = new PDDocumentOutline();
      doc.getDocumentCatalog().setDocumentOutline( outline );
      
      //Create a root element to show in the tree
      PDOutlineItem root = new PDOutlineItem();
      root.setTitle( "Root of Document" );
      outline.appendChild( root)
      
      //Get the page to refer to
      PDPage firstPage = (PDPage)doc.getAllPages().get( 0 );
      
      //Create the outline item to refer to the first page.
      PDOutlineItem firstPageItem = new PDOutlineItem();
      firstPageItem.setTitle( "First Page of document" );
      firstPageItem.setDestination( firstPage );      
      root.appendChild( firstPageItem );
      
      </source>
      <note>
      By default all nodes in the outline tree are closed.  You need to call openNode() if you want
      the node to be open when the document is opened.
      </note>
    </section>
  </body>
</document>
