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