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.COSInteger; 37 import org.pdfbox.cos.COSName; 38 import org.pdfbox.cos.COSNumber; 39 import org.pdfbox.cos.COSStream; 40 41 import org.pdfbox.pdfviewer.PageDrawer; 42 import org.pdfbox.pdmodel.common.COSArrayList; 43 import org.pdfbox.pdmodel.common.COSObjectable; 44 import org.pdfbox.pdmodel.common.PDMetadata; 45 import org.pdfbox.pdmodel.common.PDRectangle; 46 import org.pdfbox.pdmodel.common.PDStream; 47 import org.pdfbox.pdmodel.interactive.action.PDPageAdditionalActions; 48 import org.pdfbox.pdmodel.interactive.annotation.PDAnnotation; 49 import org.pdfbox.pdmodel.interactive.pagenavigation.PDThreadBead; 50 51 import java.awt.Color ; 52 import java.awt.Dimension ; 53 import java.awt.Graphics ; 54 import java.awt.Graphics2D ; 55 import java.awt.image.BufferedImage ; 56 import java.awt.print.PageFormat ; 57 import java.awt.print.Printable ; 58 import java.awt.print.PrinterException ; 59 import java.awt.print.PrinterIOException ; 60 import java.io.IOException ; 61 62 import java.util.ArrayList ; 63 import java.util.Calendar ; 64 import java.util.GregorianCalendar ; 65 import java.util.List ; 66 67 73 public class PDPage implements COSObjectable, Printable 74 { 75 private COSDictionary page; 76 77 80 public static final PDRectangle PAGE_SIZE_LETTER = new PDRectangle( 612, 792 ); 81 82 83 86 public PDPage() 87 { 88 page = new COSDictionary(); 89 page.setItem( COSName.TYPE, COSName.PAGE ); 90 setMediaBox( PAGE_SIZE_LETTER ); 91 } 92 93 98 public PDPage( COSDictionary pageDic ) 99 { 100 page = pageDic; 101 } 102 103 108 public COSBase getCOSObject() 109 { 110 return page; 111 } 112 113 118 public COSDictionary getCOSDictionary() 119 { 120 return page; 121 } 122 123 124 130 public PDPageNode getParent() 131 { 132 PDPageNode parent = null; 133 COSDictionary parentDic = (COSDictionary)page.getDictionaryObject( "Parent", "P" ); 134 if( parentDic != null ) 135 { 136 parent = new PDPageNode( parentDic ); 137 } 138 return parent; 139 } 140 141 146 public void setParent( PDPageNode parent ) 147 { 148 page.setItem( COSName.PARENT, parent.getDictionary() ); 149 } 150 151 154 public void updateLastModified() 155 { 156 page.setDate( "LastModified", new GregorianCalendar () ); 157 } 158 159 167 public Calendar getLastModified() throws IOException 168 { 169 return page.getDate( "LastModified" ); 170 } 171 172 179 public PDResources getResources() 180 { 181 PDResources retval = null; 182 COSDictionary resources = (COSDictionary)page.getDictionaryObject( COSName.RESOURCES ); 183 if( resources != null ) 184 { 185 retval = new PDResources( resources ); 186 } 187 return retval; 188 } 189 190 196 public PDResources findResources() 197 { 198 PDResources retval = getResources(); 199 PDPageNode parent = getParent(); 200 if( retval == null && parent != null ) 201 { 202 retval = parent.findResources(); 203 } 204 return retval; 205 } 206 207 212 public void setResources( PDResources resources ) 213 { 214 page.setItem( COSName.RESOURCES, resources ); 215 } 216 217 228 public PDRectangle getMediaBox() 229 { 230 PDRectangle retval = null; 231 COSArray array = (COSArray)page.getDictionaryObject( COSName.MEDIA_BOX ); 232 if( array != null ) 233 { 234 retval = new PDRectangle( array ); 235 } 236 return retval; 237 } 238 239 245 public PDRectangle findMediaBox() 246 { 247 PDRectangle retval = getMediaBox(); 248 PDPageNode parent = getParent(); 249 if( retval == null && parent != null ) 250 { 251 retval = parent.findMediaBox(); 252 } 253 return retval; 254 } 255 256 261 public void setMediaBox( PDRectangle mediaBox ) 262 { 263 if( mediaBox == null ) 264 { 265 page.removeItem( COSName.MEDIA_BOX ); 266 } 267 else 268 { 269 page.setItem( COSName.MEDIA_BOX, mediaBox.getCOSArray() ); 270 } 271 } 272 273 286 public PDRectangle getCropBox() 287 { 288 PDRectangle retval = null; 289 COSArray array = (COSArray)page.getDictionaryObject( COSName.CROP_BOX); 290 if( array != null ) 291 { 292 retval = new PDRectangle( array ); 293 } 294 return retval; 295 } 296 297 303 public PDRectangle findCropBox() 304 { 305 PDRectangle retval = getCropBox(); 306 PDPageNode parent = getParent(); 307 if( retval == null && parent != null ) 308 { 309 retval = findParentCropBox( parent ); 310 } 311 312 if( retval == null ) 314 { 315 retval = findMediaBox(); 316 } 317 return retval; 318 } 319 320 326 private PDRectangle findParentCropBox( PDPageNode node ) 327 { 328 PDRectangle rect = node.getCropBox(); 329 PDPageNode parent = node.getParent(); 330 if( rect == null && parent != null ) 331 { 332 rect = findParentCropBox( parent ); 333 } 334 return rect; 335 } 336 337 342 public void setCropBox( PDRectangle cropBox ) 343 { 344 if( cropBox == null ) 345 { 346 page.removeItem( COSName.CROP_BOX ); 347 } 348 else 349 { 350 page.setItem( COSName.CROP_BOX, cropBox.getCOSArray() ); 351 } 352 } 353 354 361 public PDRectangle getBleedBox() 362 { 363 PDRectangle retval = null; 364 COSArray array = (COSArray)page.getDictionaryObject( COSName.BLEED_BOX ); 365 if( array != null ) 366 { 367 retval = new PDRectangle( array ); 368 } 369 else 370 { 371 retval = findCropBox(); 372 } 373 return retval; 374 } 375 376 381 public void setBleedBox( PDRectangle bleedBox ) 382 { 383 if( bleedBox == null ) 384 { 385 page.removeItem( COSName.BLEED_BOX ); 386 } 387 else 388 { 389 page.setItem( COSName.BLEED_BOX, bleedBox.getCOSArray() ); 390 } 391 } 392 393 400 public PDRectangle getTrimBox() 401 { 402 PDRectangle retval = null; 403 COSArray array = (COSArray)page.getDictionaryObject( COSName.TRIM_BOX ); 404 if( array != null ) 405 { 406 retval = new PDRectangle( array ); 407 } 408 else 409 { 410 retval = findCropBox(); 411 } 412 return retval; 413 } 414 415 420 public void setTrimBox( PDRectangle trimBox ) 421 { 422 if( trimBox == null ) 423 { 424 page.removeItem( COSName.TRIM_BOX ); 425 } 426 else 427 { 428 page.setItem( COSName.TRIM_BOX, trimBox.getCOSArray() ); 429 } 430 } 431 432 439 public PDRectangle getArtBox() 440 { 441 PDRectangle retval = null; 442 COSArray array = (COSArray)page.getDictionaryObject( COSName.ART_BOX ); 443 if( array != null ) 444 { 445 retval = new PDRectangle( array ); 446 } 447 else 448 { 449 retval = findCropBox(); 450 } 451 return retval; 452 } 453 454 459 public void setArtBox( PDRectangle artBox ) 460 { 461 if( artBox == null ) 462 { 463 page.removeItem( COSName.ART_BOX ); 464 } 465 else 466 { 467 page.setItem( COSName.ART_BOX, artBox.getCOSArray() ); 468 } 469 } 470 471 472 475 487 public Integer getRotation() 488 { 489 Integer retval = null; 490 COSNumber value = (COSNumber)page.getDictionaryObject( COSName.ROTATE ); 491 if( value != null ) 492 { 493 retval = new Integer ( value.intValue() ); 494 } 495 return retval; 496 } 497 498 504 public int findRotation() 505 { 506 int retval = 0; 507 Integer rotation = getRotation(); 508 if( rotation != null ) 509 { 510 retval = rotation.intValue(); 511 } 512 else 513 { 514 PDPageNode parent = getParent(); 515 if( parent != null ) 516 { 517 retval = parent.findRotation(); 518 } 519 } 520 521 return retval; 522 } 523 524 529 public void setRotation( int rotation ) 530 { 531 page.setItem( COSName.ROTATE, new COSInteger( rotation ) ); 532 } 533 534 543 public PDStream getContents() throws IOException 544 { 545 return PDStream.createFromCOS( page.getDictionaryObject( COSName.CONTENTS ) ); 546 } 547 548 553 public void setContents( PDStream contents ) 554 { 555 page.setItem( COSName.CONTENTS, contents ); 556 } 557 558 564 public List getThreadBeads() 565 { 566 COSArray beads = (COSArray)page.getDictionaryObject( COSName.B ); 567 if( beads == null ) 568 { 569 beads = new COSArray(); 570 } 571 List pdObjects = new ArrayList(); 572 for( int i=0; i<beads.size(); i++) 573 { 574 COSDictionary beadDic = (COSDictionary)beads.getObject( i ); 575 PDThreadBead bead = null; 576 if( beadDic != null ) 578 { 579 bead = new PDThreadBead( beadDic ); 580 } 581 pdObjects.add( bead ); 582 } 583 return new COSArrayList(pdObjects, beads); 584 585 } 586 587 592 public void setThreadBeads( List beads ) 593 { 594 page.setItem( COSName.B, COSArrayList.converterToCOSArray( beads ) ); 595 } 596 597 603 public PDMetadata getMetadata() 604 { 605 PDMetadata retval = null; 606 COSStream stream = (COSStream)page.getDictionaryObject( COSName.METADATA ); 607 if( stream != null ) 608 { 609 retval = new PDMetadata( stream ); 610 } 611 return retval; 612 } 613 614 619 public void setMetadata( PDMetadata meta ) 620 { 621 page.setItem( COSName.METADATA, meta ); 622 } 623 624 631 public BufferedImage convertToImage() throws IOException 632 { 633 int scaling = 2; 634 int rotation = findRotation(); 635 PDRectangle mBox = findMediaBox(); 636 int width = (int)(mBox.getWidth()); int height = (int)(mBox.getHeight()); if( rotation == 90 || rotation == 270 ) 639 { 640 int tmp = width; 641 width = height; 642 height = tmp; 643 } 644 Dimension pageDimension = new Dimension ( width, height ); 645 646 BufferedImage retval = 651 new BufferedImage ( width*scaling, height*scaling, BufferedImage.TYPE_BYTE_INDEXED ); 652 Graphics2D graphics = (Graphics2D )retval.getGraphics(); 653 graphics.setColor( Color.WHITE ); 654 graphics.fillRect(0,0,width*scaling, height*scaling); 655 graphics.scale( scaling, scaling ); 656 PageDrawer drawer = new PageDrawer(); 657 drawer.drawPage( graphics, this, pageDimension ); 658 659 660 return retval; 661 } 662 663 668 public PDPageAdditionalActions getActions() 669 { 670 COSDictionary addAct = (COSDictionary) page.getDictionaryObject(COSName.AA); 671 if (addAct == null) 672 { 673 addAct = new COSDictionary(); 674 page.setItem(COSName.AA, addAct); 675 } 676 return new PDPageAdditionalActions(addAct); 677 } 678 679 684 public void setActions( PDPageAdditionalActions actions ) 685 { 686 page.setItem( COSName.AA, actions ); 687 } 688 689 696 public List getAnnotations() throws IOException 697 { 698 COSArrayList retval = null; 699 COSArray annots = (COSArray)page.getDictionaryObject(COSName.ANNOTS); 700 if (annots == null) 701 { 702 annots = new COSArray(); 703 page.setItem(COSName.ANNOTS, annots); 704 retval = new COSArrayList(new ArrayList(), annots); 705 } 706 else 707 { 708 List actuals = new ArrayList(); 709 710 for (int i=0; i < annots.size(); i++) 711 { 712 COSBase item = annots.getObject(i); 713 actuals.add( PDAnnotation.createAnnotation( item ) ); 714 } 715 retval = new COSArrayList(actuals, annots); 716 } 717 return retval; 718 } 719 720 725 public void setAnnotations( List annots ) 726 { 727 page.setItem( COSName.ANNOTS, COSArrayList.converterToCOSArray( annots ) ); 728 } 729 730 733 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) 734 throws PrinterException 735 { 736 int retval = Printable.PAGE_EXISTS; 737 try 738 { 739 PageDrawer drawer = new PageDrawer(); 740 PDRectangle pageSize = findMediaBox(); 741 drawer.drawPage( graphics, this, pageSize.createDimension() ); 742 } 743 catch( IOException io ) 744 { 745 throw new PrinterIOException ( io ); 746 } 747 return retval; 748 } 749 750 753 public boolean equals( Object other ) 754 { 755 return other instanceof PDPage && ((PDPage)other).getCOSObject() == this.getCOSObject(); 756 } 757 758 761 public int hashCode() 762 { 763 return this.getCOSDictionary().hashCode(); 764 } 765 } | Popular Tags |