KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfAnnotation


1 /*
2  * $Id: PdfAnnotation.java 2565 2007-02-02 14:52:16Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 Bruno Lowagie
6  *
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/
10  *
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.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
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.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
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.
35  *
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.
40  *
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
44  * details.
45  *
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/
49  */

50
51 package com.lowagie.text.pdf;
52
53 import java.awt.Color JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.util.HashMap JavaDoc;
56
57 import com.lowagie.text.Rectangle;
58 /**
59  * A <CODE>PdfAnnotation</CODE> is a note that is associated with a page.
60  *
61  * @see PdfDictionary
62  */

63
64 public class PdfAnnotation extends PdfDictionary {
65     /** highlight attributename */
66     public static final PdfName HIGHLIGHT_NONE = PdfName.N;
67     /** highlight attributename */
68     public static final PdfName HIGHLIGHT_INVERT = PdfName.I;
69     /** highlight attributename */
70     public static final PdfName HIGHLIGHT_OUTLINE = PdfName.O;
71     /** highlight attributename */
72     public static final PdfName HIGHLIGHT_PUSH = PdfName.P;
73     /** highlight attributename */
74     public static final PdfName HIGHLIGHT_TOGGLE = PdfName.T;
75     /** flagvalue */
76     public static final int FLAGS_INVISIBLE = 1;
77     /** flagvalue */
78     public static final int FLAGS_HIDDEN = 2;
79     /** flagvalue */
80     public static final int FLAGS_PRINT = 4;
81     /** flagvalue */
82     public static final int FLAGS_NOZOOM = 8;
83     /** flagvalue */
84     public static final int FLAGS_NOROTATE = 16;
85     /** flagvalue */
86     public static final int FLAGS_NOVIEW = 32;
87     /** flagvalue */
88     public static final int FLAGS_READONLY = 64;
89     /** flagvalue */
90     public static final int FLAGS_LOCKED = 128;
91     /** flagvalue */
92     public static final int FLAGS_TOGGLENOVIEW = 256;
93     /** appearance attributename */
94     public static final PdfName APPEARANCE_NORMAL = PdfName.N;
95     /** appearance attributename */
96     public static final PdfName APPEARANCE_ROLLOVER = PdfName.R;
97     /** appearance attributename */
98     public static final PdfName APPEARANCE_DOWN = PdfName.D;
99     /** attributevalue */
100     public static final PdfName AA_ENTER = PdfName.E;
101     /** attributevalue */
102     public static final PdfName AA_EXIT = PdfName.X;
103     /** attributevalue */
104     public static final PdfName AA_DOWN = PdfName.D;
105     /** attributevalue */
106     public static final PdfName AA_UP = PdfName.U;
107     /** attributevalue */
108     public static final PdfName AA_FOCUS = PdfName.FO;
109     /** attributevalue */
110     public static final PdfName AA_BLUR = PdfName.BL;
111     /** attributevalue */
112     public static final PdfName AA_JS_KEY = PdfName.K;
113     /** attributevalue */
114     public static final PdfName AA_JS_FORMAT = PdfName.F;
115     /** attributevalue */
116     public static final PdfName AA_JS_CHANGE = PdfName.V;
117     /** attributevalue */
118     public static final PdfName AA_JS_OTHER_CHANGE = PdfName.C;
119     /** attributevalue */
120     public static final int MARKUP_HIGHLIGHT = 0;
121     /** attributevalue */
122     public static final int MARKUP_UNDERLINE = 1;
123     /** attributevalue */
124     public static final int MARKUP_STRIKEOUT = 2;
125     
126     protected PdfWriter writer;
127     protected PdfIndirectReference reference;
128     protected HashMap JavaDoc templates;
129     protected boolean form = false;
130     protected boolean annotation = true;
131     
132     /** Holds value of property used. */
133     protected boolean used = false;
134     
135     /** Holds value of property placeInPage. */
136     private int placeInPage = -1;
137     
138     // constructors
139
public PdfAnnotation(PdfWriter writer, Rectangle rect) {
140         this.writer = writer;
141         if (rect != null)
142             put(PdfName.RECT, new PdfRectangle(rect));
143     }
144     
145 /**
146  * Constructs a new <CODE>PdfAnnotation</CODE> of subtype text.
147  * @param writer
148  * @param llx
149  * @param lly
150  * @param urx
151  * @param ury
152  * @param title
153  * @param content
154  */

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 /**
165  * Constructs a new <CODE>PdfAnnotation</CODE> of subtype link (Action).
166  * @param writer
167  * @param llx
168  * @param lly
169  * @param urx
170  * @param ury
171  * @param action
172  */

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     /**
184      * Creates a screen PdfAnnotation
185      * @param writer
186      * @param rect
187      * @param clipTitle
188      * @param fs
189      * @param mimeType
190      * @param playOnDisplay
191      * @return a screen PdfAnnotation
192      * @throws IOException
193      */

194     public static PdfAnnotation createScreen(PdfWriter writer, Rectangle rect, String JavaDoc clipTitle, PdfFileSpecification fs,
195                                              String JavaDoc mimeType, boolean playOnDisplay) throws IOException JavaDoc {
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         // for play on display add trigger event
205
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     /**
223      * @param writer
224      * @param rect
225      * @param title
226      * @param contents
227      * @param open
228      * @param icon
229      * @return a PdfAnnotation
230      */

231     public static PdfAnnotation createText(PdfWriter writer, Rectangle rect, String JavaDoc title, String JavaDoc contents, boolean open, String JavaDoc 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     /**
247      * Creates a link.
248      * @param writer
249      * @param rect
250      * @param highlight
251      * @return A PdfAnnotation
252      */

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     /**
262      * Creates an Annotation with an Action.
263      * @param writer
264      * @param rect
265      * @param highlight
266      * @param action
267      * @return A PdfAnnotation
268      */

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     /**
276      * Creates an Annotation with an local destination.
277      * @param writer
278      * @param rect
279      * @param highlight
280      * @param namedDestination
281      * @return A PdfAnnotation
282      */

283     public static PdfAnnotation createLink(PdfWriter writer, Rectangle rect, PdfName highlight, String JavaDoc namedDestination) {
284         PdfAnnotation annot = createLink(writer, rect, highlight);
285         annot.put(PdfName.DEST, new PdfString(namedDestination));
286         return annot;
287     }
288
289     /**
290      * Creates an Annotation with a PdfDestination.
291      * @param writer
292      * @param rect
293      * @param highlight
294      * @param page
295      * @param dest
296      * @return A PdfAnnotation
297      */

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     /**
307      * Add some free text to the document.
308      * @param writer
309      * @param rect
310      * @param contents
311      * @param defaultAppearance
312      * @return A PdfAnnotation
313      */

314     public static PdfAnnotation createFreeText(PdfWriter writer, Rectangle rect, String JavaDoc 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     /**
323      * Adds a line to the document. Move over the line and a tooltip is shown.
324      * @param writer
325      * @param rect
326      * @param contents
327      * @param x1
328      * @param y1
329      * @param x2
330      * @param y2
331      * @return A PdfAnnotation
332      */

333     public static PdfAnnotation createLine(PdfWriter writer, Rectangle rect, String JavaDoc 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     /**
346      * Adds a circle or a square that shows a tooltip when you pass over it.
347      * @param writer
348      * @param rect
349      * @param contents The tooltip
350      * @param square true if you want a square, false if you want a circle
351      * @return A PdfAnnotation
352      */

353     public static PdfAnnotation createSquareCircle(PdfWriter writer, Rectangle rect, String JavaDoc 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 JavaDoc 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     /**
384      * Adds a Stamp to your document. Move over the stamp and a tooltip is shown
385      * @param writer
386      * @param rect
387      * @param contents
388      * @param name
389      * @return A PdfAnnotation
390      */

391     public static PdfAnnotation createStamp(PdfWriter writer, Rectangle rect, String JavaDoc contents, String JavaDoc 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 JavaDoc 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     /** Creates a file attachment annotation.
416      * @param writer the <CODE>PdfWriter</CODE>
417      * @param rect the dimensions in the page of the annotation
418      * @param contents the file description
419      * @param fileStore an array with the file. If it's <CODE>null</CODE>
420      * the file will be read from the disk
421      * @param file the path to the file. It will only be used if
422      * <CODE>fileStore</CODE> is not <CODE>null</CODE>
423      * @param fileDisplay the actual file name stored in the pdf
424      * @throws IOException on error
425      * @return the annotation
426      */

427     public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String JavaDoc contents, byte fileStore[], String JavaDoc file, String JavaDoc fileDisplay) throws IOException JavaDoc {
428         return createFileAttachment(writer, rect, contents, PdfFileSpecification.fileEmbedded(writer, file, fileDisplay, fileStore));
429     }
430
431     /** Creates a file attachment annotation
432      * @param writer
433      * @param rect
434      * @param contents
435      * @param fs
436      * @return the annotation
437      * @throws IOException
438      */

439     public static PdfAnnotation createFileAttachment(PdfWriter writer, Rectangle rect, String JavaDoc contents, PdfFileSpecification fs) throws IOException JavaDoc {
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     /**
449      * Adds a popup to your document.
450      * @param writer
451      * @param rect
452      * @param contents
453      * @param open
454      * @return A PdfAnnotation
455      */

456     public static PdfAnnotation createPopup(PdfWriter writer, Rectangle rect, String JavaDoc 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     /**
492      * Sets the annotation's highlighting mode. The values can be
493      * <CODE>HIGHLIGHT_NONE</CODE>, <CODE>HIGHLIGHT_INVERT</CODE>,
494      * <CODE>HIGHLIGHT_OUTLINE</CODE> and <CODE>HIGHLIGHT_PUSH</CODE>;
495      * @param highlight the annotation's highlighting mode
496      */

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 JavaDoc();
514         templates.put(template, null);
515     }
516
517     public void setAppearance(PdfName ap, String JavaDoc 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 JavaDoc();
535         templates.put(template, null);
536     }
537     
538     public void setAppearanceState(String JavaDoc 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 JavaDoc color) {
547         put(PdfName.C, new PdfColor(color));
548     }
549     
550     public void setTitle(String JavaDoc 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     /** Getter for property used.
579      * @return Value of property used.
580      */

581     public boolean isUsed() {
582         return used;
583     }
584     
585     /** Setter for property used.
586      */

587     public void setUsed() {
588         used = true;
589     }
590     
591     public HashMap JavaDoc getTemplates() {
592         return templates;
593     }
594     
595     /** Getter for property form.
596      * @return Value of property form.
597      */

598     public boolean isForm() {
599         return form;
600     }
601     
602     /** Getter for property annotation.
603      * @return Value of property annotation.
604      */

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     /** Getter for property placeInPage.
618      * @return Value of property placeInPage.
619      */

620     public int getPlaceInPage() {
621         return placeInPage;
622     }
623     
624     /** Places the annotation in a specified page that must be greater
625      * or equal to the current one. With <code>PdfStamper</code> the page
626      * can be any. The first page is 1.
627      * @param placeInPage New value of property placeInPage.
628      */

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 JavaDoc 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 JavaDoc("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 JavaDoc 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 JavaDoc 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 JavaDoc caption) {
693         getMK().put(PdfName.CA, new PdfString(caption, PdfObject.TEXT_UNICODE));
694     }
695     
696     public void setMKRolloverCaption(String JavaDoc caption) {
697         getMK().put(PdfName.RC, new PdfString(caption, PdfObject.TEXT_UNICODE));
698     }
699     
700     public void setMKAlternateCaption(String JavaDoc 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     /**
737      * Sets the layer this annotation belongs to.
738      * @param layer the layer this annotation belongs to
739      */

740     public void setLayer(PdfOCG layer) {
741         put(PdfName.OC, layer.getRef());
742     }
743     
744     /**
745      * Sets the name of the annotation.
746      * With this name the annotation can be identified among
747      * all the annotations on a page (it has to be unique).
748      */

749     public void setName(String JavaDoc name) {
750         put(PdfName.NM, new PdfString(name));
751     }
752 }
Popular Tags