001/*
002 * $Id: Annotation.java 4847 2011-05-05 19:46:13Z redlab_b $
003 *
004 * This file is part of the iText (R) project.
005 * Copyright (c) 1998-2011 1T3XT BVBA
006 * Authors: Bruno Lowagie, Paulo Soares, et al.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU Affero General Public License version 3
010 * as published by the Free Software Foundation with the addition of the
011 * following permission added to Section 15 as permitted in Section 7(a):
012 * FOR ANY PART OF THE COVERED WORK IN WHICH THE COPYRIGHT IS OWNED BY 1T3XT,
013 * 1T3XT DISCLAIMS THE WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
014 *
015 * This program is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
017 * or FITNESS FOR A PARTICULAR PURPOSE.
018 * See the GNU Affero General Public License for more details.
019 * You should have received a copy of the GNU Affero General Public License
020 * along with this program; if not, see http://www.gnu.org/licenses or write to
021 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
022 * Boston, MA, 02110-1301 USA, or download the license from the following URL:
023 * http://itextpdf.com/terms-of-use/
024 *
025 * The interactive user interfaces in modified source and object code versions
026 * of this program must display Appropriate Legal Notices, as required under
027 * Section 5 of the GNU Affero General Public License.
028 *
029 * In accordance with Section 7(b) of the GNU Affero General Public License,
030 * a covered work must retain the producer line in every PDF that is created
031 * or manipulated using iText.
032 *
033 * You can be released from the requirements of the license by purchasing
034 * a commercial license. Buying such a license is mandatory as soon as you
035 * develop commercial activities involving the iText software without
036 * disclosing the source code of your own applications.
037 * These activities include: offering paid services to customers as an ASP,
038 * serving PDFs on the fly in a web application, shipping iText with a closed
039 * source product.
040 *
041 * For more information, please contact iText Software Corp. at this
042 * address: sales@itextpdf.com
043 */
044package com.itextpdf.text;
045
046import java.net.URL;
047import java.util.List;
048import java.util.ArrayList;
049import java.util.HashMap;
050
051/**
052 * An <CODE>Annotation</CODE> is a little note that can be added to a page on
053 * a document.
054 *
055 * @see Element
056 * @see Anchor
057 */
058
059public class Annotation implements Element {
060
061        // membervariables
062
063        /** This is a possible annotation type. */
064        public static final int TEXT = 0;
065
066        /** This is a possible annotation type. */
067        public static final int URL_NET = 1;
068
069        /** This is a possible annotation type. */
070        public static final int URL_AS_STRING = 2;
071
072        /** This is a possible annotation type. */
073        public static final int FILE_DEST = 3;
074
075        /** This is a possible annotation type. */
076        public static final int FILE_PAGE = 4;
077
078        /** This is a possible annotation type. */
079        public static final int NAMED_DEST = 5;
080
081        /** This is a possible annotation type. */
082        public static final int LAUNCH = 6;
083
084        /** This is a possible annotation type. */
085        public static final int SCREEN = 7;
086
087        /** This is a possible attribute. */
088        public static final String TITLE = "title";
089
090        /** This is a possible attribute. */
091        public static final String CONTENT = "content";
092
093        /** This is a possible attribute. */
094        public static final String URL = "url";
095
096        /** This is a possible attribute. */
097        public static final String FILE = "file";
098
099        /** This is a possible attribute. */
100        public static final String DESTINATION = "destination";
101
102        /** This is a possible attribute. */
103        public static final String PAGE = "page";
104
105        /** This is a possible attribute. */
106        public static final String NAMED = "named";
107
108        /** This is a possible attribute. */
109        public static final String APPLICATION = "application";
110
111        /** This is a possible attribute. */
112        public static final String PARAMETERS = "parameters";
113
114        /** This is a possible attribute. */
115        public static final String OPERATION = "operation";
116
117        /** This is a possible attribute. */
118        public static final String DEFAULTDIR = "defaultdir";
119
120        /** This is a possible attribute. */
121        public static final String LLX = "llx";
122
123        /** This is a possible attribute. */
124        public static final String LLY = "lly";
125
126        /** This is a possible attribute. */
127        public static final String URX = "urx";
128
129        /** This is a possible attribute. */
130        public static final String URY = "ury";
131
132        /** This is a possible attribute. */
133        public static final String MIMETYPE = "mime";
134
135        /** This is the type of annotation. */
136        protected int annotationtype;
137
138        /** This is the title of the <CODE>Annotation</CODE>. */
139        protected HashMap<String, Object> annotationAttributes = new HashMap<String, Object>();
140
141        /** This is the lower left x-value */
142        protected float llx = Float.NaN;
143
144        /** This is the lower left y-value */
145        protected float lly = Float.NaN;
146
147        /** This is the upper right x-value */
148        protected float urx = Float.NaN;
149
150        /** This is the upper right y-value */
151        protected float ury = Float.NaN;
152
153        // constructors
154
155        /**
156         * Constructs an <CODE>Annotation</CODE> with a certain title and some
157         * text.
158         *
159         * @param llx
160         *            lower left x coordinate
161         * @param lly
162         *            lower left y coordinate
163         * @param urx
164         *            upper right x coordinate
165         * @param ury
166         *            upper right y coordinate
167         */
168        private Annotation(final float llx, final float lly, final float urx, final float ury) {
169                this.llx = llx;
170                this.lly = lly;
171                this.urx = urx;
172                this.ury = ury;
173        }
174
175        /**
176         * Copy constructor.
177         * @param an the annotation to create a new Annotation from
178         */
179    public Annotation(final Annotation an) {
180        annotationtype = an.annotationtype;
181        annotationAttributes = an.annotationAttributes;
182        llx = an.llx;
183        lly = an.lly;
184        urx = an.urx;
185        ury = an.ury;
186    }
187
188        /**
189         * Constructs an <CODE>Annotation</CODE> with a certain title and some
190         * text.
191         *
192         * @param title
193         *            the title of the annotation
194         * @param text
195         *            the content of the annotation
196         */
197        public Annotation(final String title, final String text) {
198                annotationtype = TEXT;
199                annotationAttributes.put(TITLE, title);
200                annotationAttributes.put(CONTENT, text);
201        }
202
203        /**
204         * Constructs an <CODE>Annotation</CODE> with a certain title and some
205         * text.
206         *
207         * @param title
208         *            the title of the annotation
209         * @param text
210         *            the content of the annotation
211         * @param llx
212         *            the lower left x-value
213         * @param lly
214         *            the lower left y-value
215         * @param urx
216         *            the upper right x-value
217         * @param ury
218         *            the upper right y-value
219         */
220        public Annotation(final String title, final String text, final float llx, final float lly,
221                        final float urx, final float ury) {
222                this(llx, lly, urx, ury);
223                annotationtype = TEXT;
224                annotationAttributes.put(TITLE, title);
225                annotationAttributes.put(CONTENT, text);
226        }
227
228        /**
229         * Constructs an <CODE>Annotation</CODE>.
230         *
231         * @param llx
232         *            the lower left x-value
233         * @param lly
234         *            the lower left y-value
235         * @param urx
236         *            the upper right x-value
237         * @param ury
238         *            the upper right y-value
239         * @param url
240         *            the external reference
241         */
242        public Annotation(final float llx, final float lly, final float urx, final float ury, final URL url) {
243                this(llx, lly, urx, ury);
244                annotationtype = URL_NET;
245                annotationAttributes.put(URL, url);
246        }
247
248        /**
249         * Constructs an <CODE>Annotation</CODE>.
250         *
251         * @param llx
252         *            the lower left x-value
253         * @param lly
254         *            the lower left y-value
255         * @param urx
256         *            the upper right x-value
257         * @param ury
258         *            the upper right y-value
259         * @param url
260         *            the external reference
261         */
262        public Annotation(final float llx, final float lly, final float urx, final float ury, final String url) {
263                this(llx, lly, urx, ury);
264                annotationtype = URL_AS_STRING;
265                annotationAttributes.put(FILE, url);
266        }
267
268        /**
269         * Constructs an <CODE>Annotation</CODE>.
270         *
271         * @param llx
272         *            the lower left x-value
273         * @param lly
274         *            the lower left y-value
275         * @param urx
276         *            the upper right x-value
277         * @param ury
278         *            the upper right y-value
279         * @param file
280         *            an external PDF file
281         * @param dest
282         *            the destination in this file
283         */
284        public Annotation(final float llx, final float lly, final float urx, final float ury, final String file,
285                        final String dest) {
286                this(llx, lly, urx, ury);
287                annotationtype = FILE_DEST;
288                annotationAttributes.put(FILE, file);
289                annotationAttributes.put(DESTINATION, dest);
290        }
291
292        /**
293         * Creates a Screen annotation to embed media clips
294         *
295         * @param llx
296         * @param lly
297         * @param urx
298         * @param ury
299         * @param moviePath
300         *            path to the media clip file
301         * @param mimeType
302         *            mime type of the media
303         * @param showOnDisplay
304         *            if true play on display of the page
305         */
306        public Annotation(final float llx, final float lly, final float urx, final float ury,
307                        final String moviePath, final String mimeType, final boolean showOnDisplay) {
308                this(llx, lly, urx, ury);
309                annotationtype = SCREEN;
310                annotationAttributes.put(FILE, moviePath);
311                annotationAttributes.put(MIMETYPE, mimeType);
312                annotationAttributes.put(PARAMETERS, new boolean[] {
313                                false /* embedded */, showOnDisplay });
314        }
315
316        /**
317         * Constructs an <CODE>Annotation</CODE>.
318         *
319         * @param llx
320         *            the lower left x-value
321         * @param lly
322         *            the lower left y-value
323         * @param urx
324         *            the upper right x-value
325         * @param ury
326         *            the upper right y-value
327         * @param file
328         *            an external PDF file
329         * @param page
330         *            a page number in this file
331         */
332        public Annotation(final float llx, final float lly, final float urx, final float ury, final String file,
333                        final int page) {
334                this(llx, lly, urx, ury);
335                annotationtype = FILE_PAGE;
336                annotationAttributes.put(FILE, file);
337                annotationAttributes.put(PAGE, Integer.valueOf(page));
338        }
339
340        /**
341         * Constructs an <CODE>Annotation</CODE>.
342         *
343         * @param llx
344         *            the lower left x-value
345         * @param lly
346         *            the lower left y-value
347         * @param urx
348         *            the upper right x-value
349         * @param ury
350         *            the upper right y-value
351         * @param named
352         *            a named destination in this file
353         */
354        public Annotation(final float llx, final float lly, final float urx, final float ury, final int named) {
355                this(llx, lly, urx, ury);
356                annotationtype = NAMED_DEST;
357                annotationAttributes.put(NAMED, Integer.valueOf(named));
358        }
359
360        /**
361         * Constructs an <CODE>Annotation</CODE>.
362         *
363         * @param llx
364         *            the lower left x-value
365         * @param lly
366         *            the lower left y-value
367         * @param urx
368         *            the upper right x-value
369         * @param ury
370         *            the upper right y-value
371         * @param application
372         *            an external application
373         * @param parameters
374         *            parameters to pass to this application
375         * @param operation
376         *            the operation to pass to this application
377         * @param defaultdir
378         *            the default directory to run this application in
379         */
380        public Annotation(final float llx, final float lly, final float urx, final float ury,
381                        final String application, final String parameters, final String operation,
382                        final String defaultdir) {
383                this(llx, lly, urx, ury);
384                annotationtype = LAUNCH;
385                annotationAttributes.put(APPLICATION, application);
386                annotationAttributes.put(PARAMETERS, parameters);
387                annotationAttributes.put(OPERATION, operation);
388                annotationAttributes.put(DEFAULTDIR, defaultdir);
389        }
390
391        // implementation of the Element-methods
392
393        /**
394         * Gets the type of the text element.
395         *
396         * @return a type
397         */
398        public int type() {
399                return Element.ANNOTATION;
400        }
401
402        /**
403         * Processes the element by adding it (or the different parts) to an <CODE>
404         * ElementListener</CODE>.
405         *
406         * @param listener
407         *            an <CODE>ElementListener</CODE>
408         * @return <CODE>true</CODE> if the element was processed successfully
409         */
410        public boolean process(final ElementListener listener) {
411                try {
412                        return listener.add(this);
413                } catch (DocumentException de) {
414                        return false;
415                }
416        }
417
418        /**
419         * Gets all the chunks in this element.
420         *
421         * @return an <CODE>ArrayList</CODE>
422         */
423
424        public List<Chunk> getChunks() {
425                return new ArrayList<Chunk>();
426        }
427
428        // methods
429
430        /**
431         * Sets the dimensions of this annotation.
432         *
433         * @param llx
434         *            the lower left x-value
435         * @param lly
436         *            the lower left y-value
437         * @param urx
438         *            the upper right x-value
439         * @param ury
440         *            the upper right y-value
441         */
442        public void setDimensions(final float llx, final float lly, final float urx, final float ury) {
443                this.llx = llx;
444                this.lly = lly;
445                this.urx = urx;
446                this.ury = ury;
447        }
448
449        // methods to retrieve information
450
451        /**
452         * Returns the lower left x-value.
453         *
454         * @return a value
455         */
456        public float llx() {
457                return llx;
458        }
459
460        /**
461         * Returns the lower left y-value.
462         *
463         * @return a value
464         */
465        public float lly() {
466                return lly;
467        }
468
469        /**
470         * Returns the upper right x-value.
471         *
472         * @return a value
473         */
474        public float urx() {
475                return urx;
476        }
477
478        /**
479         * Returns the upper right y-value.
480         *
481         * @return a value
482         */
483        public float ury() {
484                return ury;
485        }
486
487        /**
488         * Returns the lower left x-value.
489         *
490         * @param def
491         *            the default value
492         * @return a value
493         */
494        public float llx(final float def) {
495                if (Float.isNaN(llx))
496                        return def;
497                return llx;
498        }
499
500        /**
501         * Returns the lower left y-value.
502         *
503         * @param def
504         *            the default value
505         * @return a value
506         */
507        public float lly(final float def) {
508                if (Float.isNaN(lly))
509                        return def;
510                return lly;
511        }
512
513        /**
514         * Returns the upper right x-value.
515         *
516         * @param def
517         *            the default value
518         * @return a value
519         */
520        public float urx(final float def) {
521                if (Float.isNaN(urx))
522                        return def;
523                return urx;
524        }
525
526        /**
527         * Returns the upper right y-value.
528         *
529         * @param def
530         *            the default value
531         * @return a value
532         */
533        public float ury(final float def) {
534                if (Float.isNaN(ury))
535                        return def;
536                return ury;
537        }
538
539        /**
540         * Returns the type of this <CODE>Annotation</CODE>.
541         *
542         * @return a type
543         */
544        public int annotationType() {
545                return annotationtype;
546        }
547
548        /**
549         * Returns the title of this <CODE>Annotation</CODE>.
550         *
551         * @return a name
552         */
553        public String title() {
554                String s = (String) annotationAttributes.get(TITLE);
555                if (s == null)
556                        s = "";
557                return s;
558        }
559
560        /**
561         * Gets the content of this <CODE>Annotation</CODE>.
562         *
563         * @return a reference
564         */
565        public String content() {
566                String s = (String) annotationAttributes.get(CONTENT);
567                if (s == null)
568                        s = "";
569                return s;
570        }
571
572        /**
573         * Gets the content of this <CODE>Annotation</CODE>.
574         *
575         * @return a reference
576         */
577        public HashMap<String, Object> attributes() {
578                return annotationAttributes;
579        }
580
581        /**
582         * @see com.itextpdf.text.Element#isContent()
583         * @since       iText 2.0.8
584         */
585        public boolean isContent() {
586                return true;
587        }
588
589        /**
590         * @see com.itextpdf.text.Element#isNestable()
591         * @since       iText 2.0.8
592         */
593        public boolean isNestable() {
594                return true;
595        }
596
597}