1 50 51 package com.lowagie.text.pdf; 52 53 import java.awt.Color ; 54 import java.io.IOException ; 55 import java.util.HashMap ; 56 57 import com.lowagie.text.Rectangle; 58 63 64 public class PdfAnnotation extends PdfDictionary { 65 66 public static final PdfName HIGHLIGHT_NONE = PdfName.N; 67 68 public static final PdfName HIGHLIGHT_INVERT = PdfName.I; 69 70 public static final PdfName HIGHLIGHT_OUTLINE = PdfName.O; 71 72 public static final PdfName HIGHLIGHT_PUSH = PdfName.P; 73 74 public static final PdfName HIGHLIGHT_TOGGLE = PdfName.T; 75 76 public static final int FLAGS_INVISIBLE = 1; 77 78 public static final int FLAGS_HIDDEN = 2; 79 80 public static final int FLAGS_PRINT = 4; 81 82 public static final int FLAGS_NOZOOM = 8; 83 84 public static final int FLAGS_NOROTATE = 16; 85 86 public static final int FLAGS_NOVIEW = 32; 87 88 public static final int FLAGS_READONLY = 64; 89 90 public static final int FLAGS_LOCKED = 128; 91 92 public static final int FLAGS_TOGGLENOVIEW = 256; 93 94 public static final PdfName APPEARANCE_NORMAL = PdfName.N; 95 96 public static final PdfName APPEARANCE_ROLLOVER = PdfName.R; 97 98 public static final PdfName APPEARANCE_DOWN = PdfName.D; 99 100 public static final PdfName AA_ENTER = PdfName.E; 101 102 public static final PdfName AA_EXIT = PdfName.X; 103 104 public static final PdfName AA_DOWN = PdfName.D; 105 106 public static final PdfName AA_UP = PdfName.U; 107 108 public static final PdfName AA_FOCUS = PdfName.FO; 109 110 public static final PdfName AA_BLUR = PdfName.BL; 111 112 public static final PdfName AA_JS_KEY = PdfName.K; 113 114 public static final PdfName AA_JS_FORMAT = PdfName.F; 115 116 public static final PdfName AA_JS_CHANGE = PdfName.V; 117 118 public static final PdfName AA_JS_OTHER_CHANGE = PdfName.C; 119 120 public static final int MARKUP_HIGHLIGHT = 0; 121 122 public static final int MARKUP_UNDERLINE = 1; 123 124 public static final int MARKUP_STRIKEOUT = 2; 125 126 protected PdfWriter writer; 127 protected PdfIndirectReference reference; 128 protected HashMap templates; 129 protected boolean form = false; 130 protected boolean annotation = true; 131 132 133 protected boolean used = false; 134 135 136 private int placeInPage = -1; 137 138 public PdfAnnotation(PdfWriter writer, Rectangle rect) { 140 this.writer = writer; 141 if (rect != null) 142 put(PdfName.RECT, new PdfRectangle(rect)); 143 } 144 145 155 156 public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfString title, PdfString content) { 157 this.writer = writer; 158 put(PdfName.SUBTYPE, PdfName.TEXT); 159 put(PdfName.T, title); 160 put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury)); 161 put(PdfName.CONTENTS, content); 162 } 163 164 173 174 public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfAction action) { 175 this.writer = writer; 176 put(PdfName.SUBTYPE, PdfName.LINK); 177 put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury)); 178 put(PdfName.A, action); 179 put(PdfName.BORDER, new PdfBorderArray(0, 0, 0)); 180 put(PdfName.C, new PdfColor(0x00, 0x00, 0xFF)); 181 } 182 183 194 public static PdfAnnotation createScreen(PdfWriter writer, Rectangle rect, String clipTitle, PdfFileSpecification fs, 195 String mimeType, boolean playOnDisplay) throws IOException { 196 PdfAnnotation ann = new PdfAnnotation(writer, rect); 197 ann.put(PdfName.SUBTYPE, PdfName.SCREEN); 198 ann.put (PdfName.F, new PdfNumber(FLAGS_PRINT)); 199 ann.put(PdfName.TYPE, PdfName.ANNOT); 200 ann.setPage(); 201 PdfIndirectReference ref = ann.getIndirectReference(); 202 PdfAction action = PdfAction.rendition(clipTitle,fs,mimeType, ref); 203 PdfIndirectReference actionRef = writer.addToBody(action).getIndirectReference(); 204 if (playOnDisplay) 206 { 207 PdfDictionary aa = new PdfDictionary(); 208 aa.put(new PdfName("PV"), actionRef); 209 ann.put(PdfName.AA, aa); 210 } 211 ann.put(PdfName.A, actionRef); 212 return ann; 213 } 214 215 public PdfIndirectReference getIndirectReference() { 216 if (reference == null) { 217 reference = writer.getPdfIndirectReference(); 218 } 219 return reference; 220 } 221 222 231 public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) { 232 PdfAnnotation annot = new PdfAnnotation(writer, rect); 233 annot.put(PdfName.SUBTYPE, PdfName.TEXT); 234 if (title != null) 235 annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE)); 236 if (contents != null) 237 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 238 if (open) 239 annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE); 240 if (icon != null) { 241 annot.put(PdfName.NAME, new PdfName(icon)); 242 } 243 return annot; 244 } 245 246 253 protected static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight) { 254 PdfAnnotation annot = new PdfAnnotation(writer, rect); 255 annot.put(PdfName.SUBTYPE, PdfName.LINK); 256 if (!highlight.equals(HIGHLIGHT_INVERT)) 257 annot.put(PdfName.H, highlight); 258 return annot; 259 } 260 261 269 public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, PdfAction action) { 270 PdfAnnotation annot = createLink(writer, rect, highlight); 271 annot.putEx(PdfName.A, action); 272 return annot; 273 } 274 275 283 public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, String namedDestination) { 284 PdfAnnotation annot = createLink(writer, rect, highlight); 285 annot.put(PdfName.DEST, new PdfString(namedDestination)); 286 return annot; 287 } 288 289 298 public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, int page, PdfDestination dest) { 299 PdfAnnotation annot = createLink(writer, rect, highlight); 300 PdfIndirectReference ref = writer.getPageReference(page); 301 dest.addPage(ref); 302 annot.put(PdfName.DEST, dest); 303 return annot; 304 } 305 306 314 public static PdfAnnotation createFreeText(PdfWriter writer, Rectangle rect, String contents, PdfContentByte defaultAppearance) { 315 PdfAnnotation annot = new PdfAnnotation(writer, rect); 316 annot.put(PdfName.SUBTYPE, PdfName.FREETEXT); 317 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 318 annot.setDefaultAppearanceString(defaultAppearance); 319 return annot; 320 } 321 322 333 public static PdfAnnotation createLine(PdfWriter writer, Rectangle rect, String contents, float x1, float y1, float x2, float y2) { 334 PdfAnnotation annot = new PdfAnnotation(writer, rect); 335 annot.put(PdfName.SUBTYPE, PdfName.LINE); 336 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 337 PdfArray array = new PdfArray(new PdfNumber(x1)); 338 array.add(new PdfNumber(y1)); 339 array.add(new PdfNumber(x2)); 340 array.add(new PdfNumber(y2)); 341 annot.put(PdfName.L, array); 342 return annot; 343 } 344 345 353 public static PdfAnnotation createSquareCircle(PdfWriter writer, Rectangle rect, String contents, boolean square) { 354 PdfAnnotation annot = new PdfAnnotation(writer, rect); 355 if (square) 356 annot.put(PdfName.SUBTYPE, PdfName.SQUARE); 357 else 358 annot.put(PdfName.SUBTYPE, PdfName.CIRCLE); 359 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 360 return annot; 361 } 362 363 public static PdfAnnotation createMarkup(PdfWriter writer, Rectangle rect, String contents, int type, float quadPoints[]) { 364 PdfAnnotation annot = new PdfAnnotation(writer, rect); 365 PdfName name = PdfName.HIGHLIGHT; 366 switch (type) { 367 case MARKUP_UNDERLINE: 368 name = PdfName.UNDERLINE; 369 break; 370 case MARKUP_STRIKEOUT: 371 name = PdfName.STRIKEOUT; 372 break; 373 } 374 annot.put(PdfName.SUBTYPE, name); 375 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 376 PdfArray array = new PdfArray(); 377 for (int k = 0; k < quadPoints.length; ++k) 378 array.add(new PdfNumber(quadPoints[k])); 379 annot.put(PdfName.QUADPOINTS, array); 380 return annot; 381 } 382 383 391 public static PdfAnnotation createStamp(PdfWriter writer, Rectangle rect, String contents, String name) { 392 PdfAnnotation annot = new PdfAnnotation(writer, rect); 393 annot.put(PdfName.SUBTYPE, PdfName.STAMP); 394 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 395 annot.put(PdfName.NAME, new PdfName(name)); 396 return annot; 397 } 398 399 public static PdfAnnotation createInk(PdfWriter writer, Rectangle rect, String contents, float inkList[][]) { 400 PdfAnnotation annot = new PdfAnnotation(writer, rect); 401 annot.put(PdfName.SUBTYPE, PdfName.INK); 402 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 403 PdfArray outer = new PdfArray(); 404 for (int k = 0; k < inkList.length; ++k) { 405 PdfArray inner = new PdfArray(); 406 float deep[] = inkList[k]; 407 for (int j = 0; j < deep.length; ++j) 408 inner.add(new PdfNumber(deep[j])); 409 outer.add(inner); 410 } 411 annot.put(PdfName.INKLIST, outer); 412 return annot; 413 } 414 415 427 public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, byte fileStore[], String file, String fileDisplay) throws IOException { 428 return createFileAttachment(writer, rect, contents, PdfFileSpecification.fileEmbedded(writer, file, fileDisplay, fileStore)); 429 } 430 431 439 public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, PdfFileSpecification fs) throws IOException { 440 PdfAnnotation annot = new PdfAnnotation(writer, rect); 441 annot.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT); 442 if (contents != null) 443 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 444 annot.put(PdfName.FS, fs.getReference()); 445 return annot; 446 } 447 448 456 public static PdfAnnotation createPopup(PdfWriter writer, Rectangle rect, String contents, boolean open) { 457 PdfAnnotation annot = new PdfAnnotation(writer, rect); 458 annot.put(PdfName.SUBTYPE, PdfName.POPUP); 459 if (contents != null) 460 annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE)); 461 if (open) 462 annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE); 463 return annot; 464 } 465 466 public void setDefaultAppearanceString(PdfContentByte cb) { 467 byte b[] = cb.getInternalBuffer().toByteArray(); 468 int len = b.length; 469 for (int k = 0; k < len; ++k) { 470 if (b[k] == '\n') 471 b[k] = 32; 472 } 473 put(PdfName.DA, new PdfString(b)); 474 } 475 476 public void setFlags(int flags) { 477 if (flags == 0) 478 remove(PdfName.F); 479 else 480 put(PdfName.F, new PdfNumber(flags)); 481 } 482 483 public void setBorder(PdfBorderArray border) { 484 put(PdfName.BORDER, border); 485 } 486 487 public void setBorderStyle(PdfBorderDictionary border) { 488 put(PdfName.BS, border); 489 } 490 491 497 public void setHighlighting(PdfName highlight) { 498 if (highlight.equals(HIGHLIGHT_INVERT)) 499 remove(PdfName.H); 500 else 501 put(PdfName.H, highlight); 502 } 503 504 public void setAppearance(PdfName ap, PdfTemplate template) { 505 PdfDictionary dic = (PdfDictionary)get(PdfName.AP); 506 if (dic == null) 507 dic = new PdfDictionary(); 508 dic.put(ap, template.getIndirectReference()); 509 put(PdfName.AP, dic); 510 if (!form) 511 return; 512 if (templates == null) 513 templates = new HashMap (); 514 templates.put(template, null); 515 } 516 517 public void setAppearance(PdfName ap, String state, PdfTemplate template) { 518 PdfDictionary dicAp = (PdfDictionary)get(PdfName.AP); 519 if (dicAp == null) 520 dicAp = new PdfDictionary(); 521 522 PdfDictionary dic; 523 PdfObject obj = dicAp.get(ap); 524 if (obj != null && obj.isDictionary()) 525 dic = (PdfDictionary)obj; 526 else 527 dic = new PdfDictionary(); 528 dic.put(new PdfName(state), template.getIndirectReference()); 529 dicAp.put(ap, dic); 530 put(PdfName.AP, dicAp); 531 if (!form) 532 return; 533 if (templates == null) 534 templates = new HashMap (); 535 templates.put(template, null); 536 } 537 538 public void setAppearanceState(String state) { 539 if (state == null) { 540 remove(PdfName.AS); 541 return; 542 } 543 put(PdfName.AS, new PdfName(state)); 544 } 545 546 public void setColor(Color color) { 547 put(PdfName.C, new PdfColor(color)); 548 } 549 550 public void setTitle(String title) { 551 if (title == null) { 552 remove(PdfName.T); 553 return; 554 } 555 put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE)); 556 } 557 558 public void setPopup(PdfAnnotation popup) { 559 put(PdfName.POPUP, popup.getIndirectReference()); 560 popup.put(PdfName.PARENT, getIndirectReference()); 561 } 562 563 public void setAction(PdfAction action) { 564 put(PdfName.A, action); 565 } 566 567 public void setAdditionalActions(PdfName key, PdfAction action) { 568 PdfDictionary dic; 569 PdfObject obj = get(PdfName.AA); 570 if (obj != null && obj.isDictionary()) 571 dic = (PdfDictionary)obj; 572 else 573 dic = new PdfDictionary(); 574 dic.put(key, action); 575 put(PdfName.AA, dic); 576 } 577 578 581 public boolean isUsed() { 582 return used; 583 } 584 585 587 public void setUsed() { 588 used = true; 589 } 590 591 public HashMap getTemplates() { 592 return templates; 593 } 594 595 598 public boolean isForm() { 599 return form; 600 } 601 602 605 public boolean isAnnotation() { 606 return annotation; 607 } 608 609 public void setPage(int page) { 610 put(PdfName.P, writer.getPageReference(page)); 611 } 612 613 public void setPage() { 614 put(PdfName.P, writer.getCurrentPage()); 615 } 616 617 620 public int getPlaceInPage() { 621 return placeInPage; 622 } 623 624 629 public void setPlaceInPage(int placeInPage) { 630 this.placeInPage = placeInPage; 631 } 632 633 public void setRotate(int v) { 634 put(PdfName.ROTATE, new PdfNumber(v)); 635 } 636 637 PdfDictionary getMK() { 638 PdfDictionary mk = (PdfDictionary)get(PdfName.MK); 639 if (mk == null) { 640 mk = new PdfDictionary(); 641 put(PdfName.MK, mk); 642 } 643 return mk; 644 } 645 646 public void setMKRotation(int rotation) { 647 getMK().put(PdfName.R, new PdfNumber(rotation)); 648 } 649 650 public static PdfArray getMKColor(Color color) { 651 PdfArray array = new PdfArray(); 652 int type = ExtendedColor.getType(color); 653 switch (type) { 654 case ExtendedColor.TYPE_GRAY: { 655 array.add(new PdfNumber(((GrayColor)color).getGray())); 656 break; 657 } 658 case ExtendedColor.TYPE_CMYK: { 659 CMYKColor cmyk = (CMYKColor)color; 660 array.add(new PdfNumber(cmyk.getCyan())); 661 array.add(new PdfNumber(cmyk.getMagenta())); 662 array.add(new PdfNumber(cmyk.getYellow())); 663 array.add(new PdfNumber(cmyk.getBlack())); 664 break; 665 } 666 case ExtendedColor.TYPE_SEPARATION: 667 case ExtendedColor.TYPE_PATTERN: 668 case ExtendedColor.TYPE_SHADING: 669 throw new RuntimeException ("Separations, patterns and shadings are not allowed in MK dictionary."); 670 default: 671 array.add(new PdfNumber(color.getRed() / 255f)); 672 array.add(new PdfNumber(color.getGreen() / 255f)); 673 array.add(new PdfNumber(color.getBlue() / 255f)); 674 } 675 return array; 676 } 677 678 public void setMKBorderColor(Color color) { 679 if (color == null) 680 getMK().remove(PdfName.BC); 681 else 682 getMK().put(PdfName.BC, getMKColor(color)); 683 } 684 685 public void setMKBackgroundColor(Color color) { 686 if (color == null) 687 getMK().remove(PdfName.BG); 688 else 689 getMK().put(PdfName.BG, getMKColor(color)); 690 } 691 692 public void setMKNormalCaption(String caption) { 693 getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE)); 694 } 695 696 public void setMKRolloverCaption(String caption) { 697 getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE)); 698 } 699 700 public void setMKAlternateCaption(String caption) { 701 getMK().put(PdfName.AC, new PdfString(caption, PdfObject.TEXT_UNICODE)); 702 } 703 704 public void setMKNormalIcon(PdfTemplate template) { 705 getMK().put(PdfName.I, template.getIndirectReference()); 706 } 707 708 public void setMKRolloverIcon(PdfTemplate template) { 709 getMK().put(PdfName.RI, template.getIndirectReference()); 710 } 711 712 public void setMKAlternateIcon(PdfTemplate template) { 713 getMK().put(PdfName.IX, template.getIndirectReference()); 714 } 715 716 public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) { 717 PdfDictionary dic = new PdfDictionary(); 718 if (!scale.equals(PdfName.A)) 719 dic.put(PdfName.SW, scale); 720 if (!scalingType.equals(PdfName.P)) 721 dic.put(PdfName.S, scalingType); 722 if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) { 723 PdfArray array = new PdfArray(new PdfNumber(leftoverLeft)); 724 array.add(new PdfNumber(leftoverBottom)); 725 dic.put(PdfName.A, array); 726 } 727 if (fitInBounds) 728 dic.put(PdfName.FB, PdfBoolean.PDFTRUE); 729 getMK().put(PdfName.IF, dic); 730 } 731 732 public void setMKTextPosition(int tp) { 733 getMK().put(PdfName.TP, new PdfNumber(tp)); 734 } 735 736 740 public void setLayer(PdfOCG layer) { 741 put(PdfName.OC, layer.getRef()); 742 } 743 744 749 public void setName(String name) { 750 put(PdfName.NM, new PdfString(name)); 751 } 752 } | Popular Tags |