All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aowagie.text.Annotation Maven / Gradle / Ivy

/*
 * $Id: Annotation.java 3373 2008-05-12 16:21:24Z xlv $
 *
 * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.aowagie.text;

import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * An Annotation is a little note that can be added to a page on
 * a document.
 *
 * @see Element
 * @see Anchor
 */

public class Annotation implements Element {

	// membervariables

	/** This is a possible annotation type. */
	public static final int TEXT = 0;

	/** This is a possible annotation type. */
	public static final int URL_NET = 1;

	/** This is a possible annotation type. */
	public static final int URL_AS_STRING = 2;

	/** This is a possible annotation type. */
	public static final int FILE_DEST = 3;

	/** This is a possible annotation type. */
	public static final int FILE_PAGE = 4;

	/** This is a possible annotation type. */
	public static final int NAMED_DEST = 5;

	/** This is a possible annotation type. */
	public static final int LAUNCH = 6;

	/** This is a possible annotation type. */
	public static final int SCREEN = 7;

	/** This is a possible attribute. */
	public static final String TITLE = "title";

	/** This is a possible attribute. */
	public static final String CONTENT = "content";

	/** This is a possible attribute. */
	public static final String URL = "url";

	/** This is a possible attribute. */
	public static final String FILE = "file";

	/** This is a possible attribute. */
	public static final String DESTINATION = "destination";

	/** This is a possible attribute. */
	public static final String PAGE = "page";

	/** This is a possible attribute. */
	public static final String NAMED = "named";

	/** This is a possible attribute. */
	public static final String APPLICATION = "application";

	/** This is a possible attribute. */
	public static final String PARAMETERS = "parameters";

	/** This is a possible attribute. */
	public static final String OPERATION = "operation";

	/** This is a possible attribute. */
	public static final String DEFAULTDIR = "defaultdir";

	/** This is a possible attribute. */
	public static final String LLX = "llx";

	/** This is a possible attribute. */
	public static final String LLY = "lly";

	/** This is a possible attribute. */
	public static final String URX = "urx";

	/** This is a possible attribute. */
	public static final String URY = "ury";

	/** This is a possible attribute. */
	public static final String MIMETYPE = "mime";

	/** This is the type of annotation. */
	protected int annotationtype;

	/** This is the title of the Annotation. */
	protected HashMap annotationAttributes = new HashMap();

	/** This is the lower left x-value */
	protected float llx = Float.NaN;

	/** This is the lower left y-value */
	protected float lly = Float.NaN;

	/** This is the upper right x-value */
	protected float urx = Float.NaN;

	/** This is the upper right y-value */
	protected float ury = Float.NaN;

	// constructors

	/**
	 * Constructs an Annotation with a certain title and some
	 * text.
	 *
	 * @param llx
	 *            lower left x coordinate
	 * @param lly
	 *            lower left y coordinate
	 * @param urx
	 *            upper right x coordinate
	 * @param ury
	 *            upper right y coordinate
	 */
	private Annotation(final float llx, final float lly, final float urx, final float ury) {
		this.llx = llx;
		this.lly = lly;
		this.urx = urx;
		this.ury = ury;
	}

	/**
	 * Copy constructor.
	 * @param an Annotation
	 */
    public Annotation(final Annotation an) {
        this.annotationtype = an.annotationtype;
        this.annotationAttributes = an.annotationAttributes;
        this.llx = an.llx;
        this.lly = an.lly;
        this.urx = an.urx;
        this.ury = an.ury;
    }

	/**
	 * Constructs an Annotation with a certain title and some
	 * text.
	 *
	 * @param title
	 *            the title of the annotation
	 * @param text
	 *            the content of the annotation
	 */
	public Annotation(final String title, final String text) {
		this.annotationtype = TEXT;
		this.annotationAttributes.put(TITLE, title);
		this.annotationAttributes.put(CONTENT, text);
	}

	/**
	 * Constructs an Annotation with a certain title and some
	 * text.
	 *
	 * @param title
	 *            the title of the annotation
	 * @param text
	 *            the content of the annotation
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 */
	public Annotation(final String title, final String text, final float llx, final float lly,
			final float urx, final float ury) {
		this(llx, lly, urx, ury);
		this.annotationtype = TEXT;
		this.annotationAttributes.put(TITLE, title);
		this.annotationAttributes.put(CONTENT, text);
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param url
	 *            the external reference
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury, final URL url) {
		this(llx, lly, urx, ury);
		this.annotationtype = URL_NET;
		this.annotationAttributes.put(URL, url);
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param url
	 *            the external reference
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury, final String url) {
		this(llx, lly, urx, ury);
		this.annotationtype = URL_AS_STRING;
		this.annotationAttributes.put(FILE, url);
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param file
	 *            an external PDF file
	 * @param dest
	 *            the destination in this file
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury, final String file,
			final String dest) {
		this(llx, lly, urx, ury);
		this.annotationtype = FILE_DEST;
		this.annotationAttributes.put(FILE, file);
		this.annotationAttributes.put(DESTINATION, dest);
	}

	/**
	 * Creates a Screen annotation to embed media clips
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param moviePath
	 *            path to the media clip file
	 * @param mimeType
	 *            mime type of the media
	 * @param showOnDisplay
	 *            if true play on display of the page
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury,
			final String moviePath, final String mimeType, final boolean showOnDisplay) {
		this(llx, lly, urx, ury);
		this.annotationtype = SCREEN;
		this.annotationAttributes.put(FILE, moviePath);
		this.annotationAttributes.put(MIMETYPE, mimeType);
		this.annotationAttributes.put(PARAMETERS, new boolean[] {
				false /* embedded */, showOnDisplay });
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param file
	 *            an external PDF file
	 * @param page
	 *            a page number in this file
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury, final String file,
			final int page) {
		this(llx, lly, urx, ury);
		this.annotationtype = FILE_PAGE;
		this.annotationAttributes.put(FILE, file);
		this.annotationAttributes.put(PAGE, Integer.valueOf(page));
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param named
	 *            a named destination in this file
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury, final int named) {
		this(llx, lly, urx, ury);
		this.annotationtype = NAMED_DEST;
		this.annotationAttributes.put(NAMED, Integer.valueOf(named));
	}

	/**
	 * Constructs an Annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 * @param application
	 *            an external application
	 * @param parameters
	 *            parameters to pass to this application
	 * @param operation
	 *            the operation to pass to this application
	 * @param defaultdir
	 *            the default directory to run this application in
	 */
	public Annotation(final float llx, final float lly, final float urx, final float ury,
			final String application, final String parameters, final String operation,
			final String defaultdir) {
		this(llx, lly, urx, ury);
		this.annotationtype = LAUNCH;
		this.annotationAttributes.put(APPLICATION, application);
		this.annotationAttributes.put(PARAMETERS, parameters);
		this.annotationAttributes.put(OPERATION, operation);
		this.annotationAttributes.put(DEFAULTDIR, defaultdir);
	}

	// implementation of the Element-methods

	/**
	 * Gets the type of the text element.
	 *
	 * @return a type
	 */
	@Override
	public int type() {
		return Element.ANNOTATION;
	}

	/**
	 * Processes the element by adding it (or the different parts) to an 
	 * ElementListener.
	 *
	 * @param listener
	 *            an ElementListener
	 * @return true if the element was processed successfully
	 */
	@Override
	public boolean process(final ElementListener listener) {
		try {
			return listener.add(this);
		} catch (final DocumentException de) {
			return false;
		}
	}

	/**
	 * Gets all the chunks in this element.
	 *
	 * @return an ArrayList
	 */

	@Override
	public ArrayList getChunks() {
		return new ArrayList();
	}

	// methods

	/**
	 * Sets the dimensions of this annotation.
	 *
	 * @param llx
	 *            the lower left x-value
	 * @param lly
	 *            the lower left y-value
	 * @param urx
	 *            the upper right x-value
	 * @param ury
	 *            the upper right y-value
	 */
	public void setDimensions(final float llx, final float lly, final float urx, final float ury) {
		this.llx = llx;
		this.lly = lly;
		this.urx = urx;
		this.ury = ury;
	}

	// methods to retrieve information

	/**
	 * Returns the lower left x-value.
	 *
	 * @return a value
	 */
	public float llx() {
		return this.llx;
	}

	/**
	 * Returns the lower left y-value.
	 *
	 * @return a value
	 */
	public float lly() {
		return this.lly;
	}

	/**
	 * Returns the upper right x-value.
	 *
	 * @return a value
	 */
	public float urx() {
		return this.urx;
	}

	/**
	 * Returns the upper right y-value.
	 *
	 * @return a value
	 */
	public float ury() {
		return this.ury;
	}

	/**
	 * Returns the lower left x-value.
	 *
	 * @param def
	 *            the default value
	 * @return a value
	 */
	public float llx(final float def) {
		if (Float.isNaN(this.llx)) {
			return def;
		}
		return this.llx;
	}

	/**
	 * Returns the lower left y-value.
	 *
	 * @param def
	 *            the default value
	 * @return a value
	 */
	public float lly(final float def) {
		if (Float.isNaN(this.lly)) {
			return def;
		}
		return this.lly;
	}

	/**
	 * Returns the upper right x-value.
	 *
	 * @param def
	 *            the default value
	 * @return a value
	 */
	public float urx(final float def) {
		if (Float.isNaN(this.urx)) {
			return def;
		}
		return this.urx;
	}

	/**
	 * Returns the upper right y-value.
	 *
	 * @param def
	 *            the default value
	 * @return a value
	 */
	public float ury(final float def) {
		if (Float.isNaN(this.ury)) {
			return def;
		}
		return this.ury;
	}

	/**
	 * Returns the type of this Annotation.
	 *
	 * @return a type
	 */
	public int annotationType() {
		return this.annotationtype;
	}

	/**
	 * Returns the title of this Annotation.
	 *
	 * @return a name
	 */
	public String title() {
		String s = (String) this.annotationAttributes.get(TITLE);
		if (s == null) {
			s = "";
		}
		return s;
	}

	/**
	 * Gets the content of this Annotation.
	 *
	 * @return a reference
	 */
	public String content() {
		String s = (String) this.annotationAttributes.get(CONTENT);
		if (s == null) {
			s = "";
		}
		return s;
	}

	/**
	 * Gets the content of this Annotation.
	 *
	 * @return a reference
	 */
	public HashMap attributes() {
		return this.annotationAttributes;
	}

	/**
	 * @see com.aowagie.text.Element#isContent()
	 * @since	iText 2.0.8
	 */
	@Override
	public boolean isContent() {
		return true;
	}

	/**
	 * @see com.aowagie.text.Element#isNestable()
	 * @since	iText 2.0.8
	 */
	@Override
	public boolean isNestable() {
		return true;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy