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.pagenavigation; 18 19 import org.apache.pdfbox.cos.COSArray; 20 import org.apache.pdfbox.cos.COSBase; 21 import org.apache.pdfbox.cos.COSDictionary; 22 import org.apache.pdfbox.cos.COSName; 23 24 import org.apache.pdfbox.pdmodel.PDPage; 25 import org.apache.pdfbox.pdmodel.common.COSObjectable; 26 import org.apache.pdfbox.pdmodel.common.PDRectangle; 27 28 /** 29 * This a single bead in a thread in a PDF document. 30 * 31 * @author <a href="mailto:ben@benlitchfield.com">Ben Litchfield</a> 32 * @version $Revision: 1.4 $ 33 */ 34 public class PDThreadBead implements COSObjectable 35 { 36 37 38 private COSDictionary bead; 39 40 /** 41 * Constructor that is used for a preexisting dictionary. 42 * 43 * @param b The underlying dictionary. 44 */ 45 public PDThreadBead( COSDictionary b ) 46 { 47 bead = b; 48 } 49 50 /** 51 * Default constructor. 52 * 53 */ 54 public PDThreadBead() 55 { 56 bead = new COSDictionary(); 57 bead.setName( "Type", "Bead" ); 58 setNextBead( this ); 59 setPreviousBead( this ); 60 } 61 62 /** 63 * This will get the underlying dictionary that this object wraps. 64 * 65 * @return The underlying info dictionary. 66 */ 67 public COSDictionary getDictionary() 68 { 69 return bead; 70 } 71 72 /** 73 * Convert this standard java object to a COS object. 74 * 75 * @return The cos object that matches this Java object. 76 */ 77 public COSBase getCOSObject() 78 { 79 return bead; 80 } 81 82 /** 83 * This will get the thread that this bead is part of. This is only required 84 * for the first bead in a thread, so other beads 'may' return null. 85 * 86 * @return The thread that this bead is part of. 87 */ 88 public PDThread getThread() 89 { 90 PDThread retval = null; 91 COSDictionary dic = (COSDictionary)bead.getDictionaryObject( "T" ); 92 if( dic != null ) 93 { 94 retval = new PDThread( dic ); 95 } 96 return retval; 97 } 98 99 /** 100 * Set the thread that this bead is part of. This is only required for the 101 * first bead in a thread. Note: This property is set for you by the PDThread.setFirstBead() method. 102 * 103 * @param thread The thread that this bead is part of. 104 */ 105 public void setThread( PDThread thread ) 106 { 107 bead.setItem( "T", thread ); 108 } 109 110 /** 111 * This will get the next bead. If this bead is the last bead in the list then this 112 * will return the first bead. 113 * 114 * @return The next bead in the list or the first bead if this is the last bead. 115 */ 116 public PDThreadBead getNextBead() 117 { 118 return new PDThreadBead( (COSDictionary) bead.getDictionaryObject( "N" ) ); 119 } 120 121 /** 122 * Set the next bead in the thread. 123 * 124 * @param next The next bead. 125 */ 126 protected void setNextBead( PDThreadBead next ) 127 { 128 bead.setItem( "N", next ); 129 } 130 131 /** 132 * This will get the previous bead. If this bead is the first bead in the list then this 133 * will return the last bead. 134 * 135 * @return The previous bead in the list or the last bead if this is the first bead. 136 */ 137 public PDThreadBead getPreviousBead() 138 { 139 return new PDThreadBead( (COSDictionary) bead.getDictionaryObject( "V" ) ); 140 } 141 142 /** 143 * Set the previous bead in the thread. 144 * 145 * @param previous The previous bead. 146 */ 147 protected void setPreviousBead( PDThreadBead previous ) 148 { 149 bead.setItem( "V", previous ); 150 } 151 152 /** 153 * Append a bead after this bead. This will correctly set the next/previous beads in the 154 * linked list. 155 * 156 * @param append The bead to insert. 157 */ 158 public void appendBead( PDThreadBead append ) 159 { 160 PDThreadBead nextBead = getNextBead(); 161 nextBead.setPreviousBead( append ); 162 append.setNextBead( nextBead ); 163 setNextBead( append ); 164 append.setPreviousBead( this ); 165 } 166 167 /** 168 * Get the page that this bead is part of. 169 * 170 * @return The page that this bead is part of. 171 */ 172 public PDPage getPage() 173 { 174 PDPage page = null; 175 COSDictionary dic = (COSDictionary)bead.getDictionaryObject( "P" ); 176 if( dic != null ) 177 { 178 page = new PDPage( dic ); 179 } 180 return page; 181 } 182 183 /** 184 * Set the page that this bead is part of. This is a required property and must be 185 * set when creating a new bead. The PDPage object also has a list of beads in the natural 186 * reading order. It is recommended that you add this object to that list as well. 187 * 188 * @param page The page that this bead is on. 189 */ 190 public void setPage( PDPage page ) 191 { 192 bead.setItem( "P", page ); 193 } 194 195 /** 196 * The rectangle on the page that this bead is part of. 197 * 198 * @return The part of the page that this bead covers. 199 */ 200 public PDRectangle getRectangle() 201 { 202 PDRectangle rect = null; 203 COSArray array = (COSArray)bead.getDictionaryObject( COSName.R ); 204 if( array != null ) 205 { 206 rect = new PDRectangle( array ); 207 } 208 return rect; 209 } 210 211 /** 212 * Set the rectangle on the page that this bead covers. 213 * 214 * @param rect The portion of the page that this bead covers. 215 */ 216 public void setRectangle( PDRectangle rect ) 217 { 218 bead.setItem( COSName.R, rect ); 219 } 220 }