1 31 package org.pdfbox.pdmodel; 32 33 import org.pdfbox.cos.COSArray; 34 import org.pdfbox.cos.COSBase; 35 import org.pdfbox.cos.COSDictionary; 36 import org.pdfbox.cos.COSName; 37 import org.pdfbox.cos.COSNumber; 38 import org.pdfbox.cos.COSInteger; 39 40 import org.pdfbox.pdmodel.common.COSArrayList; 41 import org.pdfbox.pdmodel.common.COSObjectable; 42 import org.pdfbox.pdmodel.common.PDRectangle; 43 44 import java.util.ArrayList ; 45 import java.util.Iterator ; 46 import java.util.List ; 47 48 54 public class PDPageNode implements COSObjectable 55 { 56 private COSDictionary page; 57 58 61 public PDPageNode() 62 { 63 page = new COSDictionary(); 64 page.setItem( COSName.TYPE, COSName.PAGES ); 65 page.setItem( COSName.KIDS, new COSArray() ); 66 page.setItem( COSName.COUNT, new COSInteger( 0 ) ); 67 } 68 69 74 public PDPageNode( COSDictionary pages ) 75 { 76 page = pages; 77 } 78 79 87 public long updateCount() 88 { 89 long totalCount = 0; 90 List kids = getKids(); 91 Iterator kidIter = kids.iterator(); 92 while( kidIter.hasNext() ) 93 { 94 Object next = kidIter.next(); 95 if( next instanceof PDPage ) 96 { 97 totalCount++; 98 } 99 else 100 { 101 PDPageNode node = (PDPageNode)next; 102 totalCount += node.updateCount(); 103 } 104 } 105 page.setItem( COSName.COUNT, new COSInteger( totalCount ) ); 106 return totalCount; 107 } 108 109 114 public long getCount() 115 { 116 return ((COSNumber)page.getDictionaryObject( COSName.COUNT )).intValue(); 117 } 118 119 124 public COSDictionary getDictionary() 125 { 126 return page; 127 } 128 129 134 public PDPageNode getParent() 135 { 136 PDPageNode parent = null; 137 COSDictionary parentDic = (COSDictionary)page.getDictionaryObject( "Parent", "P" ); 138 if( parentDic != null ) 139 { 140 parent = new PDPageNode( parentDic ); 141 } 142 return parent; 143 } 144 145 150 public void setParent( PDPageNode parent ) 151 { 152 page.setItem( COSName.PARENT, parent.getDictionary() ); 153 } 154 155 158 public COSBase getCOSObject() 159 { 160 return page; 161 } 162 163 168 public List getKids() 169 { 170 List actuals = new ArrayList(); 171 COSArray kids = getAllKids(actuals, page, false); 172 return new COSArrayList( actuals, kids ); 173 } 174 175 180 public void getAllKids(List result) 181 { 182 getAllKids(result, page, true); 183 } 184 185 192 private static COSArray getAllKids(List result, COSDictionary page, boolean recurse) 193 { 194 COSArray kids = (COSArray)page.getDictionaryObject( COSName.KIDS ); 195 196 for( int i=0; i<kids.size(); i++ ) 197 { 198 COSBase obj = kids.getObject( i ); 199 if (obj instanceof COSDictionary) 200 { 201 COSDictionary kid = (COSDictionary)obj; 202 if( COSName.PAGE.equals( kid.getDictionaryObject( COSName.TYPE ) ) ) 203 { 204 result.add( new PDPage( kid ) ); 205 } 206 else 207 { 208 if (recurse) 209 { 210 getAllKids(result, kid, recurse); 211 } 212 else 213 { 214 result.add( new PDPageNode( kid ) ); 215 } 216 } 217 } 218 } 219 return kids; 220 } 221 222 229 public PDResources getResources() 230 { 231 PDResources retval = null; 232 COSDictionary resources = (COSDictionary)page.getDictionaryObject( COSName.RESOURCES ); 233 if( resources != null ) 234 { 235 retval = new PDResources( resources ); 236 } 237 return retval; 238 } 239 240 246 public PDResources findResources() 247 { 248 PDResources retval = getResources(); 249 PDPageNode parent = getParent(); 250 if( retval == null && parent != null ) 251 { 252 retval = parent.findResources(); 253 } 254 return retval; 255 } 256 257 262 public void setResources( PDResources resources ) 263 { 264 if( resources == null ) 265 { 266 page.removeItem( COSName.RESOURCES ); 267 } 268 else 269 { 270 page.setItem( COSName.RESOURCES, resources.getCOSDictionary() ); 271 } 272 } 273 274 281 public PDRectangle getMediaBox() 282 { 283 PDRectangle retval = null; 284 COSArray array = (COSArray)page.getDictionaryObject( COSName.MEDIA_BOX ); 285 if( array != null ) 286 { 287 retval = new PDRectangle( array ); 288 } 289 return retval; 290 } 291 292 298 public PDRectangle findMediaBox() 299 { 300 PDRectangle retval = getMediaBox(); 301 PDPageNode parent = getParent(); 302 if( retval == null && parent != null ) 303 { 304 retval = parent.findMediaBox(); 305 } 306 return retval; 307 } 308 309 314 public void setMediaBox( PDRectangle mediaBox ) 315 { 316 if( mediaBox == null ) 317 { 318 page.removeItem( COSName.MEDIA_BOX ); 319 } 320 else 321 { 322 page.setItem( COSName.MEDIA_BOX , mediaBox.getCOSArray() ); 323 } 324 } 325 326 333 public PDRectangle getCropBox() 334 { 335 PDRectangle retval = null; 336 COSArray array = (COSArray)page.getDictionaryObject( COSName.CROP_BOX ); 337 if( array != null ) 338 { 339 retval = new PDRectangle( array ); 340 } 341 return retval; 342 } 343 344 350 public PDRectangle findCropBox() 351 { 352 PDRectangle retval = getCropBox(); 353 PDPageNode parent = getParent(); 354 if( retval == null && parent != null ) 355 { 356 retval = findParentCropBox( parent ); 357 } 358 359 if( retval == null ) 361 { 362 retval = findMediaBox(); 363 } 364 return retval; 365 } 366 367 373 private PDRectangle findParentCropBox( PDPageNode node ) 374 { 375 PDRectangle rect = node.getCropBox(); 376 PDPageNode parent = node.getParent(); 377 if( rect == null && parent != null ) 378 { 379 rect = findParentCropBox( node ); 380 } 381 return rect; 382 } 383 384 389 public void setCropBox( PDRectangle cropBox ) 390 { 391 if( cropBox == null ) 392 { 393 page.removeItem( COSName.CROP_BOX ); 394 } 395 else 396 { 397 page.setItem( COSName.CROP_BOX, cropBox.getCOSArray() ); 398 } 399 } 400 401 413 public Integer getRotation() 414 { 415 Integer retval = null; 416 COSNumber value = (COSNumber)page.getDictionaryObject( COSName.ROTATE ); 417 if( value != null ) 418 { 419 retval = new Integer ( value.intValue() ); 420 } 421 return retval; 422 } 423 424 430 public int findRotation() 431 { 432 int retval = 0; 433 Integer rotation = getRotation(); 434 if( rotation != null ) 435 { 436 retval = rotation.intValue(); 437 } 438 else 439 { 440 PDPageNode parent = getParent(); 441 if( parent != null ) 442 { 443 retval = parent.findRotation(); 444 } 445 } 446 447 return retval; 448 } 449 450 455 public void setRotation( int rotation ) 456 { 457 page.setItem( COSName.ROTATE, new COSInteger( rotation ) ); 458 } 459 } | Popular Tags |