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:13:20 UTC

svn commit: r1678063 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java

Author: tilman
Date: Wed May  6 19:13:19 2015
New Revision: 1678063

URL: http://svn.apache.org/r1678063
Log:
PDFBOX-2576: use COSName constants
PDFBOX-2783: Use getCOSObject() instead of getCOSArray()
PDFBOX-2786: deprecate findPageNumber(), new method retrievePageNumber()

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java?rev=1678063&r1=1678062&r2=1678063&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/documentnavigation/destination/PDPageDestination.java Wed May  6 19:13:19 2015
@@ -21,7 +21,6 @@ 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;
 import org.apache.pdfbox.pdmodel.PDPageTree;
 
@@ -57,10 +56,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.
      */
@@ -89,10 +88,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.
      */
@@ -111,13 +110,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;
@@ -144,23 +147,45 @@ public abstract class PDPageDestination
     }
 
     /**
-     * Set the page number for this destination.
+     * Returns the page number for this destination, regardless of whether this is a page number or
+     * a reference to a page.
      *
-     * @param pageNumber The page for the destination.
+     * @see org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem
+     * @return the 0-based page number, or -1 if the destination type is unknown.
      */
-    public void setPageNumber( int pageNumber )
+    public int retrievePageNumber()
     {
-        array.set( 0, pageNumber );
+        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
+                PDPageTree pages = new PDPageTree((COSDictionary) parent);
+                return pages.indexOf(new PDPage((COSDictionary) page));
+            }
+        }
+        return retval;
     }
 
     /**
-     * Convert this standard java object to a COS object.
+     * Set the page number for this destination.
      *
-     * @return The cos object that matches this Java object.
+     * @param pageNumber The page for the destination.
      */
-    public COSBase getCOSObject()
+    public void setPageNumber( int pageNumber )
     {
-        return array;
+        array.set( 0, pageNumber );
     }
 
     /**
@@ -168,8 +193,10 @@ public abstract class PDPageDestination
      *
      * @return The cos object that matches this Java object.
      */
-    public COSArray getCOSArray()
+    @Override
+    public COSArray getCOSObject()
     {
         return array;
     }
+
 }