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