2
* $Id: PdfAnnotation.java,v 1.67 2006/09/10 15:35:56 psoares33 Exp $
5
* Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* (the "License"); you may not use this file except in compliance with the License.
9
* You may obtain a copy of the License at http://www.mozilla.org/MPL/
11
* Software distributed under the License is distributed on an "AS IS" basis,
12
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13
* for the specific language governing rights and limitations under the License.
15
* The Original Code is 'iText, a free JAVA-PDF library'.
17
* The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18
* the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19
* All Rights Reserved.
20
* Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21
* are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23
* Contributor(s): all the names of the contributors are added in the source code
26
* Alternatively, the contents of this file may be used under the terms of the
27
* LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28
* provisions of LGPL are applicable instead of those above. If you wish to
29
* allow use of your version of this file only under the terms of the LGPL
30
* License and not to allow others to use your version of this file under
31
* the MPL, indicate your decision by deleting the provisions above and
32
* replace them with the notice and other provisions required by the LGPL.
33
* If you do not delete the provisions above, a recipient may use your version
34
* of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36
* This library is free software; you can redistribute it and/or modify it
37
* under the terms of the MPL as stated above or under the terms of the GNU
38
* Library General Public License as published by the Free Software Foundation;
39
* either version 2 of the License, or any later version.
41
* This library is distributed in the hope that it will be useful, but WITHOUT
42
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43
* FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
46
* If you didn't download this code from the following link, you should check if
47
* you aren't using an obsolete version:
48
* http://www.lowagie.com/iText/
51
package com.lowagie.text.pdf;
53
import com.lowagie.text.Rectangle;
54
import java.util.HashMap;
55
import java.awt.Color;
58
* A <CODE>PdfAnnotation</CODE> is a note that is associated with a page.
63
public class PdfAnnotation extends PdfDictionary {
64
/** highlight attributename */
65
public static final PdfName HIGHLIGHT_NONE = PdfName.N;
66
/** highlight attributename */
67
public static final PdfName HIGHLIGHT_INVERT = PdfName.I;
68
/** highlight attributename */
69
public static final PdfName HIGHLIGHT_OUTLINE = PdfName.O;
70
/** highlight attributename */
71
public static final PdfName HIGHLIGHT_PUSH = PdfName.P;
72
/** highlight attributename */
73
public static final PdfName HIGHLIGHT_TOGGLE = PdfName.T;
75
public static final int FLAGS_INVISIBLE = 1;
77
public static final int FLAGS_HIDDEN = 2;
79
public static final int FLAGS_PRINT = 4;
81
public static final int FLAGS_NOZOOM = 8;
83
public static final int FLAGS_NOROTATE = 16;
85
public static final int FLAGS_NOVIEW = 32;
87
public static final int FLAGS_READONLY = 64;
89
public static final int FLAGS_LOCKED = 128;
91
public static final int FLAGS_TOGGLENOVIEW = 256;
92
/** appearance attributename */
93
public static final PdfName APPEARANCE_NORMAL = PdfName.N;
94
/** appearance attributename */
95
public static final PdfName APPEARANCE_ROLLOVER = PdfName.R;
96
/** appearance attributename */
97
public static final PdfName APPEARANCE_DOWN = PdfName.D;
99
public static final PdfName AA_ENTER = PdfName.E;
100
/** attributevalue */
101
public static final PdfName AA_EXIT = PdfName.X;
102
/** attributevalue */
103
public static final PdfName AA_DOWN = PdfName.D;
104
/** attributevalue */
105
public static final PdfName AA_UP = PdfName.U;
106
/** attributevalue */
107
public static final PdfName AA_FOCUS = PdfName.FO;
108
/** attributevalue */
109
public static final PdfName AA_BLUR = PdfName.BL;
110
/** attributevalue */
111
public static final PdfName AA_JS_KEY = PdfName.K;
112
/** attributevalue */
113
public static final PdfName AA_JS_FORMAT = PdfName.F;
114
/** attributevalue */
115
public static final PdfName AA_JS_CHANGE = PdfName.V;
116
/** attributevalue */
117
public static final PdfName AA_JS_OTHER_CHANGE = PdfName.C;
118
/** attributevalue */
119
public static final int MARKUP_HIGHLIGHT = 0;
120
/** attributevalue */
121
public static final int MARKUP_UNDERLINE = 1;
122
/** attributevalue */
123
public static final int MARKUP_STRIKEOUT = 2;
125
protected PdfWriter writer;
126
protected PdfIndirectReference reference;
127
protected HashMap templates;
128
protected boolean form = false;
129
protected boolean annotation = true;
131
/** Holds value of property used. */
132
protected boolean used = false;
134
/** Holds value of property placeInPage. */
135
private int placeInPage = -1;
138
public PdfAnnotation(PdfWriter writer, Rectangle rect) {
139
this.writer = writer;
141
put(PdfName.RECT, new PdfRectangle(rect));
145
* Constructs a new <CODE>PdfAnnotation</CODE> of subtype text.
155
PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfString title, PdfString content) {
156
this.writer = writer;
157
put(PdfName.SUBTYPE, PdfName.TEXT);
158
put(PdfName.T, title);
159
put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
160
put(PdfName.CONTENTS, content);
164
* Constructs a new <CODE>PdfAnnotation</CODE> of subtype link (Action).
173
public PdfAnnotation(PdfWriter writer, float llx, float lly, float urx, float ury, PdfAction action) {
174
this.writer = writer;
175
put(PdfName.SUBTYPE, PdfName.LINK);
176
put(PdfName.RECT, new PdfRectangle(llx, lly, urx, ury));
177
put(PdfName.A, action);
178
put(PdfName.BORDER, new PdfBorderArray(0, 0, 0));
179
put(PdfName.C, new PdfColor(0x00, 0x00, 0xFF));
183
* Creates a screen PdfAnnotation
189
* @param playOnDisplay
190
* @return a screen PdfAnnotation
191
* @throws IOException
193
public static PdfAnnotation createScreen(PdfWriter writer, Rectangle rect, String clipTitle, PdfFileSpecification fs,
194
String mimeType, boolean playOnDisplay) throws IOException {
195
PdfAnnotation ann = new PdfAnnotation(writer, rect);
196
ann.put(PdfName.SUBTYPE, PdfName.SCREEN);
197
ann.put (PdfName.F, new PdfNumber(FLAGS_PRINT));
198
ann.put(PdfName.TYPE, PdfName.ANNOT);
200
PdfIndirectReference ref = ann.getIndirectReference();
201
PdfAction action = PdfAction.rendition(clipTitle,fs,mimeType, ref);
202
PdfIndirectReference actionRef = writer.addToBody(action).getIndirectReference();
203
// for play on display add trigger event
206
PdfDictionary aa = new PdfDictionary();
207
aa.put(new PdfName("PV"), actionRef);
208
ann.put(PdfName.AA, aa);
210
ann.put(PdfName.A, actionRef);
214
public PdfIndirectReference getIndirectReference() {
215
if (reference == null) {
216
reference = writer.getPdfIndirectReference();
228
* @return a PdfAnnotation
230
public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String title, String contents, boolean open, String icon) {
231
PdfAnnotation annot = new PdfAnnotation(writer, rect);
232
annot.put(PdfName.SUBTYPE, PdfName.TEXT);
234
annot.put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
235
if (contents != null)
236
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
238
annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
240
annot.put(PdfName.NAME, new PdfName(icon));
250
* @return A PdfAnnotation
252
protected static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight) {
253
PdfAnnotation annot = new PdfAnnotation(writer, rect);
254
annot.put(PdfName.SUBTYPE, PdfName.LINK);
255
if (!highlight.equals(HIGHLIGHT_INVERT))
256
annot.put(PdfName.H, highlight);
261
* Creates an Annotation with an Action.
266
* @return A PdfAnnotation
268
public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, PdfAction action) {
269
PdfAnnotation annot = createLink(writer, rect, highlight);
270
annot.putEx(PdfName.A, action);
275
* Creates an Annotation with an local destination.
279
* @param namedDestination
280
* @return A PdfAnnotation
282
public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, String namedDestination) {
283
PdfAnnotation annot = createLink(writer, rect, highlight);
284
annot.put(PdfName.DEST, new PdfString(namedDestination));
289
* Creates an Annotation with a PdfDestination.
295
* @return A PdfAnnotation
297
public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, int page, PdfDestination dest) {
298
PdfAnnotation annot = createLink(writer, rect, highlight);
299
PdfIndirectReference ref = writer.getPageReference(page);
301
annot.put(PdfName.DEST, dest);
306
* Add some free text to the document.
310
* @param defaultAppearance
311
* @return A PdfAnnotation
313
public static PdfAnnotation createFreeText(PdfWriter writer, Rectangle rect, String contents, PdfContentByte defaultAppearance) {
314
PdfAnnotation annot = new PdfAnnotation(writer, rect);
315
annot.put(PdfName.SUBTYPE, PdfName.FREETEXT);
316
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
317
annot.setDefaultAppearanceString(defaultAppearance);
322
* Adds a line to the document. Move over the line and a tooltip is shown.
330
* @return A PdfAnnotation
332
public static PdfAnnotation createLine(PdfWriter writer, Rectangle rect, String contents, float x1, float y1, float x2, float y2) {
333
PdfAnnotation annot = new PdfAnnotation(writer, rect);
334
annot.put(PdfName.SUBTYPE, PdfName.LINE);
335
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
336
PdfArray array = new PdfArray(new PdfNumber(x1));
337
array.add(new PdfNumber(y1));
338
array.add(new PdfNumber(x2));
339
array.add(new PdfNumber(y2));
340
annot.put(PdfName.L, array);
345
* Adds a circle or a square that shows a tooltip when you pass over it.
348
* @param contents The tooltip
349
* @param square true if you want a square, false if you want a circle
350
* @return A PdfAnnotation
352
public static PdfAnnotation createSquareCircle(PdfWriter writer, Rectangle rect, String contents, boolean square) {
353
PdfAnnotation annot = new PdfAnnotation(writer, rect);
355
annot.put(PdfName.SUBTYPE, PdfName.SQUARE);
357
annot.put(PdfName.SUBTYPE, PdfName.CIRCLE);
358
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
362
public static PdfAnnotation createMarkup(PdfWriter writer, Rectangle rect, String contents, int type, float quadPoints[]) {
363
PdfAnnotation annot = new PdfAnnotation(writer, rect);
364
PdfName name = PdfName.HIGHLIGHT;
366
case MARKUP_UNDERLINE:
367
name = PdfName.UNDERLINE;
369
case MARKUP_STRIKEOUT:
370
name = PdfName.STRIKEOUT;
373
annot.put(PdfName.SUBTYPE, name);
374
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
375
PdfArray array = new PdfArray();
376
for (int k = 0; k < quadPoints.length; ++k)
377
array.add(new PdfNumber(quadPoints[k]));
378
annot.put(PdfName.QUADPOINTS, array);
383
* Adds a Stamp to your document. Move over the stamp and a tooltip is shown
388
* @return A PdfAnnotation
390
public static PdfAnnotation createStamp(PdfWriter writer, Rectangle rect, String contents, String name) {
391
PdfAnnotation annot = new PdfAnnotation(writer, rect);
392
annot.put(PdfName.SUBTYPE, PdfName.STAMP);
393
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
394
annot.put(PdfName.NAME, new PdfName(name));
398
public static PdfAnnotation createInk(PdfWriter writer, Rectangle rect, String contents, float inkList[][]) {
399
PdfAnnotation annot = new PdfAnnotation(writer, rect);
400
annot.put(PdfName.SUBTYPE, PdfName.INK);
401
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
402
PdfArray outer = new PdfArray();
403
for (int k = 0; k < inkList.length; ++k) {
404
PdfArray inner = new PdfArray();
405
float deep[] = inkList[k];
406
for (int j = 0; j < deep.length; ++j)
407
inner.add(new PdfNumber(deep[j]));
410
annot.put(PdfName.INKLIST, outer);
414
/** Creates a file attachment annotation.
415
* @param writer the <CODE>PdfWriter</CODE>
416
* @param rect the dimensions in the page of the annotation
417
* @param contents the file description
418
* @param fileStore an array with the file. If it's <CODE>null</CODE>
419
* the file will be read from the disk
420
* @param file the path to the file. It will only be used if
421
* <CODE>fileStore</CODE> is not <CODE>null</CODE>
422
* @param fileDisplay the actual file name stored in the pdf
423
* @throws IOException on error
424
* @return the annotation
426
public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, byte fileStore[], String file, String fileDisplay) throws IOException {
427
return createFileAttachment(writer, rect, contents, PdfFileSpecification.fileEmbedded(writer, file, fileDisplay, fileStore));
430
/** Creates a file attachment annotation
435
* @return the annotation
436
* @throws IOException
438
public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String contents, PdfFileSpecification fs) throws IOException {
439
PdfAnnotation annot = new PdfAnnotation(writer, rect);
440
annot.put(PdfName.SUBTYPE, PdfName.FILEATTACHMENT);
441
if (contents != null)
442
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
443
annot.put(PdfName.FS, fs.getReference());
448
* Adds a popup to your document.
453
* @return A PdfAnnotation
455
public static PdfAnnotation createPopup(PdfWriter writer, Rectangle rect, String contents, boolean open) {
456
PdfAnnotation annot = new PdfAnnotation(writer, rect);
457
annot.put(PdfName.SUBTYPE, PdfName.POPUP);
458
if (contents != null)
459
annot.put(PdfName.CONTENTS, new PdfString(contents, PdfObject.TEXT_UNICODE));
461
annot.put(PdfName.OPEN, PdfBoolean.PDFTRUE);
465
public void setDefaultAppearanceString(PdfContentByte cb) {
466
byte b[] = cb.getInternalBuffer().toByteArray();
468
for (int k = 0; k < len; ++k) {
472
put(PdfName.DA, new PdfString(b));
475
public void setFlags(int flags) {
479
put(PdfName.F, new PdfNumber(flags));
482
public void setBorder(PdfBorderArray border) {
483
put(PdfName.BORDER, border);
486
public void setBorderStyle(PdfBorderDictionary border) {
487
put(PdfName.BS, border);
491
* Sets the annotation's highlighting mode. The values can be
492
* <CODE>HIGHLIGHT_NONE</CODE>, <CODE>HIGHLIGHT_INVERT</CODE>,
493
* <CODE>HIGHLIGHT_OUTLINE</CODE> and <CODE>HIGHLIGHT_PUSH</CODE>;
494
* @param highlight the annotation's highlighting mode
496
public void setHighlighting(PdfName highlight) {
497
if (highlight.equals(HIGHLIGHT_INVERT))
500
put(PdfName.H, highlight);
503
public void setAppearance(PdfName ap, PdfTemplate template) {
504
PdfDictionary dic = (PdfDictionary)get(PdfName.AP);
506
dic = new PdfDictionary();
507
dic.put(ap, template.getIndirectReference());
508
put(PdfName.AP, dic);
511
if (templates == null)
512
templates = new HashMap();
513
templates.put(template, null);
516
public void setAppearance(PdfName ap, String state, PdfTemplate template) {
517
PdfDictionary dicAp = (PdfDictionary)get(PdfName.AP);
519
dicAp = new PdfDictionary();
522
PdfObject obj = dicAp.get(ap);
523
if (obj != null && obj.isDictionary())
524
dic = (PdfDictionary)obj;
526
dic = new PdfDictionary();
527
dic.put(new PdfName(state), template.getIndirectReference());
529
put(PdfName.AP, dicAp);
532
if (templates == null)
533
templates = new HashMap();
534
templates.put(template, null);
537
public void setAppearanceState(String state) {
542
put(PdfName.AS, new PdfName(state));
545
public void setColor(Color color) {
546
put(PdfName.C, new PdfColor(color));
549
public void setTitle(String title) {
554
put(PdfName.T, new PdfString(title, PdfObject.TEXT_UNICODE));
557
public void setPopup(PdfAnnotation popup) {
558
put(PdfName.POPUP, popup.getIndirectReference());
559
popup.put(PdfName.PARENT, getIndirectReference());
562
public void setAction(PdfAction action) {
563
put(PdfName.A, action);
566
public void setAdditionalActions(PdfName key, PdfAction action) {
568
PdfObject obj = get(PdfName.AA);
569
if (obj != null && obj.isDictionary())
570
dic = (PdfDictionary)obj;
572
dic = new PdfDictionary();
573
dic.put(key, action);
574
put(PdfName.AA, dic);
577
/** Getter for property used.
578
* @return Value of property used.
580
public boolean isUsed() {
584
/** Setter for property used.
590
HashMap getTemplates() {
594
/** Getter for property form.
595
* @return Value of property form.
597
public boolean isForm() {
601
/** Getter for property annotation.
602
* @return Value of property annotation.
604
public boolean isAnnotation() {
608
public void setPage(int page) {
609
put(PdfName.P, writer.getPageReference(page));
612
public void setPage() {
613
put(PdfName.P, writer.getCurrentPage());
616
/** Getter for property placeInPage.
617
* @return Value of property placeInPage.
619
public int getPlaceInPage() {
623
/** Places the annotation in a specified page that must be greater
624
* or equal to the current one. With <code>PdfStamper</code> the page
625
* can be any. The first page is 1.
626
* @param placeInPage New value of property placeInPage.
628
public void setPlaceInPage(int placeInPage) {
629
this.placeInPage = placeInPage;
632
public void setRotate(int v) {
633
put(PdfName.ROTATE, new PdfNumber(v));
636
PdfDictionary getMK() {
637
PdfDictionary mk = (PdfDictionary)get(PdfName.MK);
639
mk = new PdfDictionary();
645
public void setMKRotation(int rotation) {
646
getMK().put(PdfName.R, new PdfNumber(rotation));
649
public static PdfArray getMKColor(Color color) {
650
PdfArray array = new PdfArray();
651
int type = ExtendedColor.getType(color);
653
case ExtendedColor.TYPE_GRAY: {
654
array.add(new PdfNumber(((GrayColor)color).getGray()));
657
case ExtendedColor.TYPE_CMYK: {
658
CMYKColor cmyk = (CMYKColor)color;
659
array.add(new PdfNumber(cmyk.getCyan()));
660
array.add(new PdfNumber(cmyk.getMagenta()));
661
array.add(new PdfNumber(cmyk.getYellow()));
662
array.add(new PdfNumber(cmyk.getBlack()));
665
case ExtendedColor.TYPE_SEPARATION:
666
case ExtendedColor.TYPE_PATTERN:
667
case ExtendedColor.TYPE_SHADING:
668
throw new RuntimeException("Separations, patterns and shadings are not allowed in MK dictionary.");
670
array.add(new PdfNumber(color.getRed() / 255f));
671
array.add(new PdfNumber(color.getGreen() / 255f));
672
array.add(new PdfNumber(color.getBlue() / 255f));
677
public void setMKBorderColor(Color color) {
679
getMK().remove(PdfName.BC);
681
getMK().put(PdfName.BC, getMKColor(color));
684
public void setMKBackgroundColor(Color color) {
686
getMK().remove(PdfName.BG);
688
getMK().put(PdfName.BG, getMKColor(color));
691
public void setMKNormalCaption(String caption) {
692
getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE));
695
public void setMKRolloverCaption(String caption) {
696
getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE));
699
public void setMKAlternateCaption(String caption) {
700
getMK().put(PdfName.AC, new PdfString(caption, PdfObject.TEXT_UNICODE));
703
public void setMKNormalIcon(PdfTemplate template) {
704
getMK().put(PdfName.I, template.getIndirectReference());
707
public void setMKRolloverIcon(PdfTemplate template) {
708
getMK().put(PdfName.RI, template.getIndirectReference());
711
public void setMKAlternateIcon(PdfTemplate template) {
712
getMK().put(PdfName.IX, template.getIndirectReference());
715
public void setMKIconFit(PdfName scale, PdfName scalingType, float leftoverLeft, float leftoverBottom, boolean fitInBounds) {
716
PdfDictionary dic = new PdfDictionary();
717
if (!scale.equals(PdfName.A))
718
dic.put(PdfName.SW, scale);
719
if (!scalingType.equals(PdfName.P))
720
dic.put(PdfName.S, scalingType);
721
if (leftoverLeft != 0.5f || leftoverBottom != 0.5f) {
722
PdfArray array = new PdfArray(new PdfNumber(leftoverLeft));
723
array.add(new PdfNumber(leftoverBottom));
724
dic.put(PdfName.A, array);
727
dic.put(PdfName.FB, PdfBoolean.PDFTRUE);
728
getMK().put(PdfName.IF, dic);
731
public void setMKTextPosition(int tp) {
732
getMK().put(PdfName.TP, new PdfNumber(tp));
736
* Sets the layer this annotation belongs to.
737
* @param layer the layer this annotation belongs to
739
public void setLayer(PdfOCG layer) {
740
put(PdfName.OC, layer.getRef());
b'\\ No newline at end of file'