Index: pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java
===================================================================
--- pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java	(revisione 1665874)
+++ pdfbox/src/main/java/org/apache/pdfbox/pdmodel/PDPageTree.java	(copia locale)
@@ -275,36 +275,57 @@
     }
 
     /**
-     * Returns the index of the given page, or -1 if it does not exist.
+     * @return the zero-based index of the given page, or -1 if the page is not found.
      */
     public int indexOf(PDPage page)
     {
-        int num = 0;
-        COSDictionary node = page.getCOSObject();
-        do
+        SearchContext context = new SearchContext(page);
+        if (findPage(context, root))
         {
-            if (isPageTreeNode(node))
+            return context.index;
+        }
+        return -1;
+    }
+
+    private boolean findPage(SearchContext context, COSDictionary node)
+    {
+        for (COSDictionary kid : getKids(node))
+        {
+            if (context.found)
             {
-                // count kids up until this node
-                for (COSDictionary kid : getKids(node))
-                {
-                    if (kid == node)
-                    {
-                        break;
-                    }
-                    num++;
-                }
+                break;
             }
+            if (isPageTreeNode(kid))
+            {
+                findPage(context, kid);
+            }
             else
             {
-                num++;
+                context.visitPage(kid);
             }
-            node = (COSDictionary) node.getDictionaryObject(COSName.PARENT, COSName.P);
         }
-        while (node != null);
-        return num - 1;
+        return context.found;
     }
 
+    private static class SearchContext
+    {
+        private COSDictionary searched;
+        private int index = -1;
+        private boolean found;
+
+        private SearchContext(PDPage page)
+        {
+            this.searched = page.getCOSObject();
+        }
+
+        private void visitPage(COSDictionary current)
+        {
+            index++;
+            found = searched.equals(current);
+        }
+
+    }
+
     /**
      * Returns the number of leaf nodes (page objects) that are descendants of this root within the
      * page tree.
Index: pdfbox/src/test/java/org/apache/pdfbox/pdmodel/PDPageTreeTest.java
===================================================================
--- pdfbox/src/test/java/org/apache/pdfbox/pdmodel/PDPageTreeTest.java	(revisione 0)
+++ pdfbox/src/test/java/org/apache/pdfbox/pdmodel/PDPageTreeTest.java	(copia locale)
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.pdfbox.pdmodel;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline;
+import org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem;
+import org.junit.After;
+import org.junit.Test;
+
+/**
+ * @author Andrea Vacondio
+ *
+ */
+public class PDPageTreeTest
+{
+    private PDDocument doc;
+
+    @After
+    public void tearDown() throws IOException
+    {
+        if (doc != null)
+        {
+            doc.close();
+        }
+    }
+    @Test
+    public void indexOfPageFromOutlineDestination() throws IOException
+    {
+        doc = PDDocument.load(getClass().getClassLoader().getResourceAsStream(
+                "org/apache/pdfbox/pdmodel/with_outline.pdf"));
+        PDDocumentOutline outline = doc.getDocumentCatalog().getDocumentOutline();
+        for (PDOutlineItem current : outline.children())
+        {
+            if (current.getTitle().contains("Second"))
+            {
+                assertEquals(2, doc.getPages().indexOf(current.findDestinationPage(doc)));
+            }
+        }
+    }
+
+    @Test
+    public void positiveSingleLevel() throws IOException
+    {
+        doc = PDDocument.load(getClass().getClassLoader().getResourceAsStream(
+                "org/apache/pdfbox/pdmodel/with_outline.pdf"));
+        for (int i = 0; i < doc.getNumberOfPages(); i++)
+        {
+            assertEquals(i, doc.getPages().indexOf(doc.getPage(i)));
+        }
+    }
+
+    @Test
+    public void positiveMultipleLevel() throws IOException
+    {
+        doc = PDDocument.load(getClass().getClassLoader().getResourceAsStream(
+                "org/apache/pdfbox/pdmodel/page_tree_multiple_levels.pdf"));
+        for (int i = 0; i < doc.getNumberOfPages(); i++)
+        {
+            assertEquals(i, doc.getPages().indexOf(doc.getPage(i)));
+        }
+    }
+
+    @Test
+    public void negative() throws IOException
+    {
+        doc = PDDocument.load(getClass().getClassLoader().getResourceAsStream(
+                "org/apache/pdfbox/pdmodel/with_outline.pdf"));
+        assertEquals(-1, doc.getPages().indexOf(new PDPage()));
+    }
+}

Proprietà modificate su: pdfbox/src/test/java/org/apache/pdfbox/pdmodel/PDPageTreeTest.java
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIteratorTest.java
===================================================================
--- pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIteratorTest.java	(revisione 1665874)
+++ pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIteratorTest.java	(copia locale)

Proprietà modificate su: pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItemIteratorTest.java
___________________________________________________________________
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
