1 31 package org.pdfbox.pdmodel.interactive.annotation; 32 33 import java.io.IOException ; 34 35 import org.pdfbox.cos.COSArray; 36 import org.pdfbox.cos.COSDictionary; 37 import org.pdfbox.cos.COSName; 38 39 import org.pdfbox.pdmodel.common.COSObjectable; 40 import org.pdfbox.pdmodel.common.PDRectangle; 41 import org.pdfbox.pdmodel.graphics.color.PDGamma; 42 import org.pdfbox.pdmodel.interactive.action.PDActionFactory; 43 import org.pdfbox.pdmodel.interactive.action.PDAdditionalActions; 44 import org.pdfbox.pdmodel.interactive.action.type.PDAction; 45 import org.pdfbox.util.BitFlagHelper; 46 import org.pdfbox.cos.COSBase; 47 48 54 public abstract class PDAnnotation implements COSObjectable 55 { 56 59 public static final int FLAG_INVISIBLE = 1 << 0; 60 63 public static final int FLAG_HIDDEN = 1 << 1; 64 67 public static final int FLAG_PRINTED = 1 << 2; 68 71 public static final int FLAG_NO_ZOOM = 1 << 3; 72 75 public static final int FLAG_NO_ROTATE = 1 << 4; 76 79 public static final int FLAG_NO_VIEW = 1 << 5; 80 83 public static final int FLAG_READ_ONLY = 1 << 6; 84 87 public static final int FLAG_LOCKED = 1 << 7; 88 91 public static final int FLAG_TOGGLE_NO_VIEW = 1 << 8; 92 93 94 95 private COSDictionary dictionary; 96 97 104 public static PDAnnotation createAnnotation( COSBase base ) throws IOException 105 { 106 PDAnnotation annot = null; 107 if( base instanceof COSDictionary ) 108 { 109 COSDictionary annotDic = (COSDictionary)base; 110 String subtype = annotDic.getNameAsString( COSName.SUBTYPE ); 111 if( subtype.equals( PDAnnotationRubberStamp.SUB_TYPE ) ) 112 { 113 annot = new PDAnnotationRubberStamp(annotDic); 114 } 115 else if( subtype.equals( PDAnnotationLink.SUB_TYPE ) ) 116 { 117 annot = new PDAnnotationLink(annotDic); 118 } 119 else if( subtype.equals( PDAnnotationText.SUB_TYPE ) ) 120 { 121 annot = new PDAnnotationText( annotDic); 122 } 123 else if( subtype.equals( PDAnnotationLine.SUB_TYPE ) ) 124 { 125 annot = new PDAnnotationLine( annotDic ); 126 } 127 else if( subtype.equals( PDAnnotationSquareCircle.SUB_TYPE_SQUARE ) || 128 subtype.equals( PDAnnotationSquareCircle.SUB_TYPE_CIRCLE ) ) 129 { 130 annot = new PDAnnotationSquareCircle( annotDic ); 131 } 132 else if( subtype.equals( PDAnnotationLink.SUB_TYPE ) ) 133 { 134 annot = new PDAnnotationLink( annotDic ); 135 } 136 else if( subtype.equals( PDAnnotationFileAttachment.SUB_TYPE ) ) 137 { 138 annot = new PDAnnotationFileAttachment( annotDic ); 139 } 140 else if( subtype.equals( PDAnnotationTextMarkup.SUB_TYPE_HIGHLIGHT ) || 141 subtype.equals( PDAnnotationTextMarkup.SUB_TYPE_UNDERLINE ) || 142 subtype.equals( PDAnnotationTextMarkup.SUB_TYPE_SQUIGGLY ) || 143 subtype.equals( PDAnnotationTextMarkup.SUB_TYPE_STRIKEOUT )) 144 { 145 annot = new PDAnnotationTextMarkup( annotDic ); 146 } 147 else 148 { 149 annot = new PDAnnotationUnknown( annotDic ); 150 } 151 } 152 else 153 { 154 throw new IOException ( "Error: Unknown annotation type " + base ); 155 } 156 157 return annot; 158 } 159 160 163 public PDAnnotation() 164 { 165 dictionary = new COSDictionary(); 166 dictionary.setItem( COSName.TYPE, COSName.getPDFName( "Annot" ) ); 167 } 168 169 174 public PDAnnotation( COSDictionary dict ) 175 { 176 dictionary = dict; 177 } 178 179 183 public COSDictionary getDictionary() 184 { 185 return dictionary; 186 } 187 188 196 public PDRectangle getRectangle() 197 { 198 COSArray rectArray = (COSArray)dictionary.getDictionaryObject( COSName.getPDFName( "Rect" ) ); 199 PDRectangle rectangle = null; 200 if( rectArray != null ) 201 { 202 rectangle = new PDRectangle( rectArray ); 203 } 204 return rectangle; 205 } 206 207 212 public void setRectangle( PDRectangle rectangle ) 213 { 214 dictionary.setItem( COSName.getPDFName( "Rect" ), rectangle.getCOSArray() ); 215 } 216 217 222 public int getAnnotationFlags() 223 { 224 return getDictionary().getInt( "F", 0 ); 225 } 226 227 232 public void setAnnotationFlags( int flags ) 233 { 234 getDictionary().setInt( "F", flags ); 235 } 236 237 242 public COSBase getCOSObject() 243 { 244 return getDictionary(); 245 } 246 247 252 public String getAppearanceStream() 253 { 254 String retval = null; 255 COSName name = (COSName)getDictionary().getDictionaryObject( COSName.getPDFName( "AS" ) ); 256 if( name != null ) 257 { 258 retval = name.getName(); 259 } 260 return retval; 261 } 262 263 268 public void setAppearanceStream( String as ) 269 { 270 if( as == null ) 271 { 272 getDictionary().removeItem( COSName.getPDFName( "AS" ) ); 273 } 274 else 275 { 276 getDictionary().setItem( COSName.getPDFName( "AS" ), COSName.getPDFName( as ) ); 277 } 278 } 279 280 286 public PDAppearanceDictionary getAppearance() 287 { 288 PDAppearanceDictionary ap = null; 289 COSDictionary apDic = (COSDictionary)dictionary.getDictionaryObject( COSName.getPDFName( "AP" ) ); 290 if( apDic != null ) 291 { 292 ap = new PDAppearanceDictionary( apDic ); 293 } 294 return ap; 295 } 296 297 302 public void setAppearance( PDAppearanceDictionary appearance ) 303 { 304 COSDictionary ap = null; 305 if( appearance != null ) 306 { 307 ap = appearance.getDictionary(); 308 } 309 dictionary.setItem( COSName.getPDFName( "AP" ), ap ); 310 } 311 312 317 public boolean isInvisible() 318 { 319 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_INVISIBLE ); 320 } 321 322 327 public void setInvisible( boolean invisible ) 328 { 329 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_INVISIBLE, invisible ); 330 } 331 332 337 public boolean isHidden() 338 { 339 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_HIDDEN ); 340 } 341 342 347 public void setHidden( boolean hidden ) 348 { 349 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_HIDDEN, hidden ); 350 } 351 352 357 public boolean isPrinted() 358 { 359 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_PRINTED ); 360 } 361 362 367 public void setPrinted( boolean printed ) 368 { 369 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_PRINTED, printed ); 370 } 371 372 377 public boolean isNoZoom() 378 { 379 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_NO_ZOOM ); 380 } 381 382 387 public void setNoZoom( boolean noZoom ) 388 { 389 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_NO_ZOOM, noZoom ); 390 } 391 392 397 public boolean isNoRotate() 398 { 399 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_NO_ROTATE ); 400 } 401 402 407 public void setNoRotate( boolean noRotate ) 408 { 409 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_NO_ROTATE, noRotate ); 410 } 411 412 417 public boolean isNoView() 418 { 419 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_NO_VIEW ); 420 } 421 422 427 public void setNoView( boolean noView ) 428 { 429 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_NO_VIEW, noView ); 430 } 431 432 437 public boolean isReadOnly() 438 { 439 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_READ_ONLY ); 440 } 441 442 447 public void setReadOnly( boolean readOnly ) 448 { 449 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_READ_ONLY, readOnly ); 450 } 451 452 457 public boolean isLocked() 458 { 459 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_LOCKED ); 460 } 461 462 467 public void setLocked( boolean locked ) 468 { 469 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_LOCKED, locked ); 470 } 471 472 477 public boolean isToggleNoView() 478 { 479 return BitFlagHelper.getFlag( getDictionary(), "F", FLAG_TOGGLE_NO_VIEW ); 480 } 481 482 487 public void setToggleNoView( boolean toggleNoView ) 488 { 489 BitFlagHelper.setFlag( getDictionary(), "F", FLAG_TOGGLE_NO_VIEW, toggleNoView ); 490 } 491 492 499 public PDAction getAction() throws IOException 500 { 501 COSDictionary action = (COSDictionary) 502 getDictionary().getDictionaryObject( COSName.A ); 503 return PDActionFactory.createAction( action ); 504 } 505 506 511 public void setAction( PDAction action ) 512 { 513 getDictionary().setItem( COSName.A, action ); 514 } 515 516 523 public PDAdditionalActions getActions() 524 { 525 COSDictionary aa = (COSDictionary)dictionary.getDictionaryObject( "AA" ); 526 PDAdditionalActions retval = null; 527 if( aa != null ) 528 { 529 retval = new PDAdditionalActions( aa ); 530 } 531 return retval; 532 } 533 534 539 public void setActions( PDAdditionalActions actions ) 540 { 541 dictionary.setItem( "AA", actions ); 542 } 543 544 549 public String getContents() 550 { 551 return dictionary.getString(COSName.CONTENTS); 552 } 553 554 559 public void setContents( String value) 560 { 561 dictionary.setString(COSName.CONTENTS, value); 562 } 563 564 571 public void setBorderStyle( PDBorderStyleDictionary bs ) 572 { 573 getDictionary().setItem( "BS", bs); 574 } 575 576 582 public PDBorderStyleDictionary getBoderStyle() 583 { 584 COSDictionary bs = (COSDictionary) getDictionary().getItem( 585 COSName.getPDFName( "BS" ) ); 586 if (bs != null) 587 { 588 return new PDBorderStyleDictionary( bs ); 589 } 590 else 591 { 592 return null; 593 } 594 } 595 596 601 public String getModifiedDate() 602 { 603 return getDictionary().getString( "M" ); 604 } 605 606 612 public void setModifiedDate( String m ) 613 { 614 getDictionary().setString( "M", m ); 615 } 616 617 624 public String getAnnotationName() 625 { 626 return getDictionary().getString( "NM" ); 627 } 628 629 636 public void setAnnotationName( String nm ) 637 { 638 getDictionary().setString( "NM", nm ); 639 } 640 641 653 public void setColour( PDGamma c ) 654 { 655 getDictionary().setItem( "C", c ); 656 } 657 658 669 public PDGamma getColour() 670 { 671 COSArray c = (COSArray) getDictionary().getItem(COSName.getPDFName( "C" ) ); 672 if (c != null) 673 { 674 return new PDGamma( c ); 675 } 676 else 677 { 678 return null; 679 } 680 } 681 } | Popular Tags |