You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2015/05/06 21:27:45 UTC

svn commit: r1678070 - /pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java

Author: tilman
Date: Wed May  6 19:27:45 2015
New Revision: 1678070

URL: http://svn.apache.org/r1678070
Log:
PDFBOX-2576: use COSName constants
PDFBOX-2786: deprecate findPageNumber(), new method findDestPageNumber(), better javadoc

Modified:
    pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java

Modified: pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java?rev=1678070&r1=1678069&r2=1678070&view=diff
==============================================================================
--- pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java (original)
+++ pdfbox/branches/1.8/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java Wed May  6 19:27:45 2015
@@ -22,6 +22,7 @@ import java.util.List;
 import org.apache.pdfbox.cos.COSArray;
 import org.apache.pdfbox.cos.COSBase;
 import org.apache.pdfbox.cos.COSDictionary;
+import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.cos.COSNumber;
 
 import org.apache.pdfbox.pdmodel.PDPage;
@@ -60,10 +61,10 @@ public abstract class PDPageDestination
     }
 
     /**
-     * This will get the page for this destination.  A page destination
-     * can either reference a page or a page number(when doing a remote destination to
-     * another PDF).  If this object is referencing by page number then this method will
-     * return null and getPageNumber should be used.
+     * This will get the page for this destination. A page destination can either reference a page
+     * (for a local destination) or a page number (when doing a remote destination to another PDF).
+     * If this object is referencing by page number then this method will return null and
+     * {@link #getPageNumber()} should be used.
      *
      * @return The page for this destination.
      */
@@ -92,10 +93,10 @@ public abstract class PDPageDestination
     }
 
     /**
-     * This will get the page number for this destination.  A page destination
-     * can either reference a page or a page number(when doing a remote destination to
-     * another PDF).  If this object is referencing by page number then this method will
-     * return that number, otherwise -1 will be returned.
+     * This will get the page number for this destination. A page destination can either reference a
+     * page (for a local destination) or a page number (when doing a remote destination to another
+     * PDF). If this object is referencing by page number then this method will return that number,
+     * otherwise -1 will be returned.
      *
      * @return The page number for this destination.
      */
@@ -114,13 +115,17 @@ public abstract class PDPageDestination
     }
 
     /**
-     * Returns the page number for this destination, regardless of whether
-     * this is a page number or a reference to a page.
+     * Returns the page number for this destination, regardless of whether this is a page number or
+     * a reference to a page.
      *
      * @since Apache PDFBox 1.0.0
      * @see org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem
-     * @return page number, or -1 if the destination type is unknown
+     * @return page number, or -1 if the destination type is unknown. The page number is 0-based if
+     * it was in the dictionary (for remote destinations), and 1-based if it was computed from a
+     * page reference (for local destinations).
+     * @deprecated This method has inconsistent behavior (see returns), use {@link #retrievePageNumber()} instead.
      */
+    @Deprecated
     public int findPageNumber() {
         int retval = -1;
         if( array.size() > 0 )
@@ -133,8 +138,8 @@ public abstract class PDPageDestination
             else if (page instanceof COSDictionary)
             {
                 COSBase parent = page;
-                while (((COSDictionary) parent).getDictionaryObject("Parent", "P") != null) {
-                    parent = ((COSDictionary) parent).getDictionaryObject( "Parent", "P" );
+                while (((COSDictionary) parent).getDictionaryObject(COSName.PARENT, COSName.P) != null) {
+                    parent = ((COSDictionary) parent).getDictionaryObject(COSName.PARENT, COSName.P);
                 }
                 // now parent is the pages node
                 PDPageNode pages = new PDPageNode((COSDictionary) parent);
@@ -144,6 +149,40 @@ public abstract class PDPageDestination
             }
         }
         return retval;
+    }
+
+    /**
+     * Returns the page number for this destination, regardless of whether this is a page number or
+     * a reference to a page.
+     *
+     * @see org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem
+     * @return the 0-based page number, or -1 if the destination type is unknown.
+     */
+    public int retrieveDestPageNumber()
+    {
+        int retval = -1;
+        if (array.size() > 0)
+        {
+            COSBase page = array.getObject(0);
+            if (page instanceof COSNumber)
+            {
+                retval = ((COSNumber) page).intValue();
+            }
+            else if (page instanceof COSDictionary)
+            {
+                COSBase parent = page;
+                while (((COSDictionary) parent).getDictionaryObject(COSName.PARENT, COSName.P) != null)
+                {
+                    parent = ((COSDictionary) parent).getDictionaryObject(COSName.PARENT, COSName.P);
+                }
+                // now parent is the pages node
+                PDPageNode pages = new PDPageNode((COSDictionary) parent);
+                List<PDPage> allPages = new ArrayList<PDPage>();
+                pages.getAllKids(allPages);
+                retval = allPages.indexOf(new PDPage((COSDictionary) page));
+            }
+        }
+        return retval;
     }
 
     /**