1 3 package org.faceless.pdf; 4 5 import java.util.*; 6 import java.awt.Color ; 7 8 18 public class PDFAnnotation extends PeeredObject 19 { 20 final org.faceless.pdf2.PDFAnnotation annot; 21 22 Object getPeer() 23 { 24 return annot; 25 } 26 27 32 public static final int EVENT_ONCLICK =-1; 33 34 39 public static final int EVENT_ONMOUSEOVER =0; 40 41 46 public static final int EVENT_ONMOUSEOUT =1; 47 48 53 public static final int EVENT_ONMOUSEDOWN =2; 54 55 60 public static final int EVENT_ONMOUSEUP =3; 61 62 68 public static final int EVENT_ONFOCUS =4; 69 70 76 public static final int EVENT_ONBLUR =5; 77 78 84 public static final int EVENT_ONCHANGE =6; 85 86 92 public static final int EVENT_ONKEYPRESS =7; 93 94 100 public static final int EVENT_ONFORMAT =8; 101 102 110 public static final int EVENT_ONOTHERCHANGE =9; 111 112 113 117 public static final int TEXT_NOTE = 0; 118 119 123 public static final int TEXT_COMMENT = 1; 124 125 129 public static final int TEXT_HELP = 2; 130 131 135 public static final int TEXT_INSERT = 3; 136 137 141 public static final int TEXT_KEY = 4; 142 143 147 public static final int TEXT_NEWPARAGRAPH = 5; 148 149 153 public static final int TEXT_PARAGRAPH = 6; 154 155 PDFAnnotation(org.faceless.pdf2.PDFAnnotation annot) 156 { 157 this.annot=annot; 158 } 159 160 176 public static PDFAnnotation link(PDFAction action, boolean border) 177 { 178 org.faceless.pdf2.AnnotationLink link = new org.faceless.pdf2.AnnotationLink(); 179 link.setAction(action.action); 180 if (border) { 181 org.faceless.pdf2.PDFStyle style = new org.faceless.pdf2.PDFStyle(); 182 link.setStyle(style); 183 } 184 return new PDFAnnotation(link); 185 } 186 187 199 public static PDFAnnotation text(String label, String contents, boolean open, int type) 200 { 201 org.faceless.pdf2.AnnotationNote note = new org.faceless.pdf2.AnnotationNote(); 202 203 String newtype; 204 if (type==TEXT_COMMENT) newtype = "Comment"; 205 else if (type==TEXT_HELP) newtype = "Help"; 206 else if (type==TEXT_INSERT) newtype = "Insert"; 207 else if (type==TEXT_KEY) newtype = "Key"; 208 else if (type==TEXT_NEWPARAGRAPH) newtype = "New Paragraph"; 209 else if (type==TEXT_PARAGRAPH) newtype = "Paragraph"; 210 else newtype = "Note"; 211 212 if (newtype!=null) { 213 note.setType(newtype, Color.yellow); 214 } 215 note.setOpen(open); 216 note.setAuthor(label); 217 note.setContents(contents); 218 219 return new PDFAnnotation(note); 220 } 221 222 236 public static PDFAnnotation stamp(String type) 237 throws IllegalArgumentException 238 { 239 org.faceless.pdf2.AnnotationStamp stamp = new org.faceless.pdf2.AnnotationStamp("stamp.stencil."+type, 1); 240 return new PDFAnnotation(stamp); 241 } 242 243 264 public void setRectangle(float x1, float y1, float x2, float y2) 265 { 266 annot.setRectangle(x1,y1,x2,y2); 267 } 268 269 275 public float[] getRectangle() 276 { 277 return annot.getRectangle(); 278 } 279 280 288 public String getType() 289 { 290 return annot.getType(); 291 } 292 293 298 public PDFPage getPage() 299 { 300 return (PDFPage)PeeredObject.getPeer(annot.getPage()); 301 } 302 303 310 public void setPage(PDFPage page) 311 { 312 annot.setPage(page.page); 313 } 314 315 322 public void setVisible(boolean visible) 323 { 324 annot.setVisible(visible); 325 } 326 327 334 public void setPrintable(boolean printable) 335 { 336 annot.setPrintable(printable); 337 } 338 339 344 public boolean isVisible() 345 { 346 return annot.isVisible(); 347 } 348 349 354 public boolean isPrintable() 355 { 356 return annot.isPrintable(); 357 } 358 359 368 public void setColor(Color c) 369 { 370 if (annot instanceof org.faceless.pdf2.AnnotationLink) { 371 if (c==null) { 372 ((org.faceless.pdf2.AnnotationLink)annot).setStyle(null); 373 } else { 374 org.faceless.pdf2.PDFStyle style = new org.faceless.pdf2.PDFStyle(); 375 style.setLineColor(c); 376 ((org.faceless.pdf2.AnnotationLink)annot).setStyle(style); 377 } 378 } else if (annot instanceof org.faceless.pdf2.AnnotationNote) { 379 ((org.faceless.pdf2.AnnotationNote)annot).setType(((org.faceless.pdf2.AnnotationNote)annot).getType(), c); 380 } 381 } 382 383 388 public void setAction(PDFAction action) 389 { 390 if (annot instanceof org.faceless.pdf2.AnnotationLink) { 391 ((org.faceless.pdf2.AnnotationLink)annot).setAction(action==null ? null : action.action); 392 } 393 } 394 395 399 public PDFAction getAction() 400 { 401 if (annot instanceof org.faceless.pdf2.AnnotationLink) { 402 return (PDFAction)PeeredObject.getPeer(((org.faceless.pdf2.AnnotationLink)annot).getAction()); 403 } else if (annot instanceof org.faceless.pdf2.WidgetAnnotation) { 404 return (PDFAction)PeeredObject.getPeer(((org.faceless.pdf2.WidgetAnnotation)annot).getAction(org.faceless.pdf2.Event.CLICK)); 405 } else { 406 return null; 407 } 408 } 409 410 415 public void setTextAnnotationContents(String contents) 416 { 417 if (annot instanceof org.faceless.pdf2.AnnotationNote) { 418 ((org.faceless.pdf2.AnnotationNote)annot).setContents(contents); 419 } else if (annot instanceof org.faceless.pdf2.AnnotationStamp) { 420 ((org.faceless.pdf2.AnnotationStamp)annot).setContents(contents); 421 } 422 } 423 424 430 public String getTextAnnotationContents() 431 { 432 if (annot instanceof org.faceless.pdf2.AnnotationNote) { 433 return ((org.faceless.pdf2.AnnotationNote)annot).getContents(); 434 } else if (annot instanceof org.faceless.pdf2.AnnotationStamp) { 435 return ((org.faceless.pdf2.AnnotationStamp)annot).getContents(); 436 } else { 437 return null; 438 } 439 } 440 441 448 public void setTextAnnotationLabel(String label) 449 { 450 if (annot instanceof org.faceless.pdf2.AnnotationNote) { 451 ((org.faceless.pdf2.AnnotationNote)annot).setAuthor(label); 452 } 453 } 454 455 462 public String getTextAnnotationLabel() 463 { 464 if (annot instanceof org.faceless.pdf2.AnnotationNote) { 465 return ((org.faceless.pdf2.AnnotationNote)annot).getAuthor(); 466 } else { 467 return null; 468 } 469 } 470 471 476 public Calendar getLastModified() 477 { 478 return annot.getModifyDate(); 479 } 480 481 499 public void setEventAction(int event, PDFAction action) 500 { 501 org.faceless.pdf2.Event newevent=null; 502 if (annot instanceof org.faceless.pdf2.WidgetAnnotation) { 503 if (event==EVENT_ONCLICK) newevent=org.faceless.pdf2.Event.CLICK; 504 else if (event==EVENT_ONMOUSEOVER) newevent=org.faceless.pdf2.Event.MOUSEOVER; 505 else if (event==EVENT_ONMOUSEOUT) newevent=org.faceless.pdf2.Event.MOUSEOUT; 506 else if (event==EVENT_ONMOUSEDOWN) newevent=org.faceless.pdf2.Event.MOUSEDOWN; 507 else if (event==EVENT_ONMOUSEUP) newevent=org.faceless.pdf2.Event.MOUSEUP; 508 else if (event==EVENT_ONBLUR) newevent=org.faceless.pdf2.Event.BLUR; 509 else if (event==EVENT_ONFOCUS) newevent=org.faceless.pdf2.Event.FOCUS; 510 else if (event==EVENT_ONCHANGE) newevent=org.faceless.pdf2.Event.CHANGE; 511 else if (event==EVENT_ONFORMAT) newevent=org.faceless.pdf2.Event.FORMAT; 512 else if (event==EVENT_ONKEYPRESS) newevent=org.faceless.pdf2.Event.KEYPRESS; 513 else if (event==EVENT_ONOTHERCHANGE) newevent=org.faceless.pdf2.Event.OTHERCHANGE; 514 if (newevent!=null) { 515 if (event==EVENT_ONKEYPRESS || event==EVENT_ONFORMAT || event==EVENT_ONCHANGE || event==EVENT_ONOTHERCHANGE) { 516 ((org.faceless.pdf2.WidgetAnnotation)annot).getField().setAction(newevent, action==null ? null : action.action); 517 } else { 518 ((org.faceless.pdf2.WidgetAnnotation)annot).setAction(newevent, action==null ? null : action.action); 519 } 520 } 521 } 522 } 523 524 533 public PDFAction getEventAction(int event) 534 { 535 if (annot instanceof org.faceless.pdf2.WidgetAnnotation) { 536 org.faceless.pdf2.Event newevent=null; 537 if (event==EVENT_ONCLICK) newevent=org.faceless.pdf2.Event.CLICK; 538 else if (event==EVENT_ONMOUSEOVER) newevent=org.faceless.pdf2.Event.MOUSEOVER; 539 else if (event==EVENT_ONMOUSEOUT) newevent=org.faceless.pdf2.Event.MOUSEOUT; 540 else if (event==EVENT_ONMOUSEDOWN) newevent=org.faceless.pdf2.Event.MOUSEDOWN; 541 else if (event==EVENT_ONMOUSEUP) newevent=org.faceless.pdf2.Event.MOUSEUP; 542 else if (event==EVENT_ONBLUR) newevent=org.faceless.pdf2.Event.BLUR; 543 else if (event==EVENT_ONFOCUS) newevent=org.faceless.pdf2.Event.FOCUS; 544 else if (event==EVENT_ONCHANGE) newevent=org.faceless.pdf2.Event.CHANGE; 545 else if (event==EVENT_ONFORMAT) newevent=org.faceless.pdf2.Event.FORMAT; 546 else if (event==EVENT_ONKEYPRESS) newevent=org.faceless.pdf2.Event.KEYPRESS; 547 else if (event==EVENT_ONOTHERCHANGE) newevent=org.faceless.pdf2.Event.OTHERCHANGE; 548 if (newevent!=null) { 549 if (event==EVENT_ONKEYPRESS || event==EVENT_ONFORMAT || event==EVENT_ONCHANGE || event==EVENT_ONOTHERCHANGE) { 550 return (PDFAction)PeeredObject.getPeer(((org.faceless.pdf2.WidgetAnnotation)annot).getField().getAction(newevent)); 551 } else { 552 return (PDFAction)PeeredObject.getPeer(((org.faceless.pdf2.WidgetAnnotation)annot).getAction(newevent)); 553 } 554 } else { 555 return null; 556 } 557 } else { 558 return null; 559 } 560 } 561 562 public String toString() 563 { 564 String out = "<annotation type=\""+getType()+"\" rect=\""; 565 float[] f = getRectangle(); 566 out+=f[0]+","+f[1]+","+f[2]+","+f[3]+"\""; 567 if (getType().equals("Text")) { 568 out+=" label=\""+getTextAnnotationLabel()+"\" content=\""+getTextAnnotationContents()+"\""; 569 } else if (getType().equals("Widget")) { 570 String n = ((org.faceless.pdf2.WidgetAnnotation)annot).getValue(); 571 if (n!=null) { 572 out+=" value=\""+n+"\""; 573 } 574 } 575 if (!isVisible()) out+=" visible=\"false\""; 576 if (!isPrintable()) out+=" printable=\"false\""; 577 if (getAction()!=null) { 578 out+=" action=\""+getAction()+"\""; 579 } 580 String [] triggers = { "E","X","D","U","Fo","Bl","V","K","F","C" }; 581 for (int i=0;i<triggers.length;i++) { 582 final String [] names = { "onMouseOver", "onMouseOut", "onMouseDown", "onMouseUp", "onFocus", "onBlur", "onChange", "onKeyPress", "onFormat", "onOtherChange" }; 583 PDFAction a = getEventAction(i); 584 if (a!=null) { 585 out+=" "+names[i]+"=\""+a+"\""; 586 } 587 } 588 return out+"/>"; 589 } 590 591 } 592 | Popular Tags |