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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
17 package org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination;
\r
19 import java.util.ArrayList;
\r
20 import java.util.List;
\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
27 import org.apache.pdfbox.pdmodel.PDPage;
\r
28 import org.apache.pdfbox.pdmodel.PDPageNode;
\r
31 * This represents a destination to a page, see subclasses for specific parameters.
\r
33 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a>
\r
34 * @version $Revision: 1.2 $
\r
36 public abstract class PDPageDestination extends PDDestination
\r
39 * Storage for the page destination.
\r
41 protected COSArray array;
\r
44 * Constructor to create empty page destination.
\r
47 protected PDPageDestination()
\r
49 array = new COSArray();
\r
53 * Constructor to create empty page destination.
\r
55 * @param arr A page destination array.
\r
57 protected PDPageDestination( COSArray arr )
\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
68 * @return The page for this destination.
\r
70 public PDPage getPage()
\r
72 PDPage retval = null;
\r
73 if( array.size() > 0 )
\r
75 COSBase page = array.getObject( 0 );
\r
76 if( page instanceof COSDictionary )
\r
78 retval = new PDPage( (COSDictionary)page );
\r
85 * Set the page for this destination.
\r
87 * @param page The page for the destination.
\r
89 public void setPage( PDPage page )
\r
91 array.set( 0, page );
\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
100 * @return The page number for this destination.
\r
102 public int getPageNumber()
\r
105 if( array.size() > 0 )
\r
107 COSBase page = array.getObject( 0 );
\r
108 if( page instanceof COSNumber )
\r
110 retval = ((COSNumber)page).intValue();
\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
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
124 public int findPageNumber() {
\r
126 if( array.size() > 0 )
\r
128 COSBase page = array.getObject( 0 );
\r
129 if( page instanceof COSNumber )
\r
131 retval = ((COSNumber)page).intValue();
\r
133 else if (page instanceof COSDictionary)
\r
135 COSBase parent = page;
\r
136 while (((COSDictionary) parent).getDictionaryObject("Parent", "P") != null) {
\r
137 parent = ((COSDictionary) parent).getDictionaryObject( "Parent", "P" );
\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
150 * Set the page number for this destination.
\r
152 * @param pageNumber The page for the destination.
\r
154 public void setPageNumber( int pageNumber )
\r
156 array.set( 0, pageNumber );
\r
160 * Convert this standard java object to a COS object.
\r
162 * @return The cos object that matches this Java object.
\r
164 public COSBase getCOSObject()
\r
170 * Convert this standard java object to a COS object.
\r
172 * @return The cos object that matches this Java object.
\r
174 public COSArray getCOSArray()
\r