]> _ Git - cubeextranet.git/blob
aed658310ad9d37fcfe705bc6b9e755041636f32
[cubeextranet.git] /
1 /*\r
2  * Licensed to the Apache Software Foundation (ASF) under one or more\r
3  * contributor license agreements.  See the NOTICE file distributed with\r
4  * this work for additional information regarding copyright ownership.\r
5  * The ASF licenses this file to You under the Apache License, Version 2.0\r
6  * (the "License"); you may not use this file except in compliance with\r
7  * the License.  You may obtain a copy of the License at\r
8  *\r
9  *      http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  */\r
17 package org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination;\r
18 \r
19 import java.util.ArrayList;\r
20 import java.util.List;\r
21 \r
22 import org.apache.pdfbox.cos.COSArray;\r
23 import org.apache.pdfbox.cos.COSBase;\r
24 import org.apache.pdfbox.cos.COSDictionary;\r
25 import org.apache.pdfbox.cos.COSNumber;\r
26 \r
27 import org.apache.pdfbox.pdmodel.PDPage;\r
28 import org.apache.pdfbox.pdmodel.PDPageNode;\r
29 \r
30 /**\r
31  * This represents a destination to a page, see subclasses for specific parameters.\r
32  *\r
33  * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>\r
34  * @version $Revision: 1.2 $\r
35  */\r
36 public abstract class PDPageDestination extends PDDestination\r
37 {\r
38     /**\r
39      * Storage for the page destination.\r
40      */\r
41     protected COSArray array;\r
42 \r
43     /**\r
44      * Constructor to create empty page destination.\r
45      *\r
46      */\r
47     protected PDPageDestination()\r
48     {\r
49         array = new COSArray();\r
50     }\r
51 \r
52     /**\r
53      * Constructor to create empty page destination.\r
54      *\r
55      * @param arr A page destination array.\r
56      */\r
57     protected PDPageDestination( COSArray arr )\r
58     {\r
59         array = arr;\r
60     }\r
61 \r
62     /**\r
63      * This will get the page for this destination.  A page destination\r
64      * can either reference a page or a page number(when doing a remote destination to\r
65      * another PDF).  If this object is referencing by page number then this method will\r
66      * return null and getPageNumber should be used.\r
67      *\r
68      * @return The page for this destination.\r
69      */\r
70     public PDPage getPage()\r
71     {\r
72         PDPage retval = null;\r
73         if( array.size() > 0 )\r
74         {\r
75             COSBase page = array.getObject( 0 );\r
76             if( page instanceof COSDictionary )\r
77             {\r
78                 retval = new PDPage( (COSDictionary)page );\r
79             }\r
80         }\r
81         return retval;\r
82     }\r
83 \r
84     /**\r
85      * Set the page for this destination.\r
86      *\r
87      * @param page The page for the destination.\r
88      */\r
89     public void setPage( PDPage page )\r
90     {\r
91         array.set( 0, page );\r
92     }\r
93 \r
94     /**\r
95      * This will get the page number for this destination.  A page destination\r
96      * can either reference a page or a page number(when doing a remote destination to\r
97      * another PDF).  If this object is referencing by page number then this method will\r
98      * return that number, otherwise -1 will be returned.\r
99      *\r
100      * @return The page number for this destination.\r
101      */\r
102     public int getPageNumber()\r
103     {\r
104         int retval = -1;\r
105         if( array.size() > 0 )\r
106         {\r
107             COSBase page = array.getObject( 0 );\r
108             if( page instanceof COSNumber )\r
109             {\r
110                 retval = ((COSNumber)page).intValue();\r
111             }\r
112         }\r
113         return retval;\r
114     }\r
115 \r
116     /**\r
117      * Returns the page number for this destination, regardless of whether\r
118      * this is a page number or a reference to a page.\r
119      *\r
120      * @since Apache PDFBox 1.0.0\r
121      * @see PDOutlineItem\r
122      * @return page number, or -1 if the destination type is unknown\r
123      */\r
124     public int findPageNumber() {\r
125         int retval = -1;\r
126         if( array.size() > 0 )\r
127         {\r
128             COSBase page = array.getObject( 0 );\r
129             if( page instanceof COSNumber )\r
130             {\r
131                 retval = ((COSNumber)page).intValue();\r
132             }\r
133             else if (page instanceof COSDictionary)\r
134             {\r
135                 COSBase parent = page;\r
136                 while (((COSDictionary) parent).getDictionaryObject("Parent", "P") != null) {\r
137                     parent = ((COSDictionary) parent).getDictionaryObject( "Parent", "P" );\r
138                 }\r
139                 // now parent is the pages node\r
140                 PDPageNode pages = new PDPageNode((COSDictionary) parent);\r
141                 List<PDPage> allPages = new ArrayList<PDPage>();\r
142                 pages.getAllKids(allPages);\r
143                 retval = allPages.indexOf(new PDPage((COSDictionary) page)) + 1;\r
144             }\r
145         }\r
146         return retval;\r
147     }\r
148 \r
149     /**\r
150      * Set the page number for this destination.\r
151      *\r
152      * @param pageNumber The page for the destination.\r
153      */\r
154     public void setPageNumber( int pageNumber )\r
155     {\r
156         array.set( 0, pageNumber );\r
157     }\r
158 \r
159     /**\r
160      * Convert this standard java object to a COS object.\r
161      *\r
162      * @return The cos object that matches this Java object.\r
163      */\r
164     public COSBase getCOSObject()\r
165     {\r
166         return array;\r
167     }\r
168 \r
169     /**\r
170      * Convert this standard java object to a COS object.\r
171      *\r
172      * @return The cos object that matches this Java object.\r
173      */\r
174     public COSArray getCOSArray()\r
175     {\r
176         return array;\r
177     }\r
178 }\r