This sample uses QPDFNotesView, Qoppa’s Android PDF viewer & annotator. It loops through the bookmarks contained in a PDF document, finds the first GotoPage action contained in the document’s bookmark tree and if any and go to that page.

private void goToFirstBookmark()
{
 PDFDocument doc = notes.getDocument();
 if (doc != null)
 {
   try
   {
     // loop through the bookmarks
     Bookmark root = doc.getRootBookmark();
 
     GotoPageAction action = findGotoPageAction(root);
     if (action != null)
     {
        // Instruct Android PDF Viewer QPDFNOtesView to handle the action
        // which in this case will go to the page contained in the action
	notes.handleAction(action);
       // Another way to go to that page would be to call
       // notes.goToPage(action.getPage().getPageIndex() + 1);
     }
   }
   catch (PDFException e)
   {
     e.printStackTrace();
   }
  }		
}
 
// Find the first GotoPageAction in the Bookmark's children
private GotoPageAction findGotoPageAction(Bookmark parent)
{
  if (parent == null)
  { 
    return null;
  }
 
  // Look for a GotoPageAction in each child bookmark
  Vector<Bookmark> children = parent.getChildren();
  if (children != null)
  {
    // loop through the child bookmarks
    for (Bookmark child : children)
    {
      // look through the bookmark's actions
      Vector<Action> actions = child.getActions();
      if (actions != null)
      {
	for (Action action : actions)
	{
           if (action instanceof GotoPageAction)
	   {
             return (GotoPageAction) action;
	   }
	}
   }
   // Not found in this bookmark's actions, so look through its child bookmarks
   GotoPageAction action = findGotoPageAction(child);
   if (action != null)
   {
          return action;
   }
 }
}
return null;
}

Download Full Java Sample GotoFirstSample.java

Tagged: