001/* 002 * $Id: PdfDestination.java 4860 2011-05-09 09:57:46Z 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.pdf; 045 046import java.util.StringTokenizer; 047 048/** 049 * A <CODE>PdfDestination</CODE> is a reference to a location in a PDF file. 050 */ 051 052public class PdfDestination extends PdfArray { 053 054 // public static final member-variables 055 056/** This is a possible destination type */ 057 public static final int XYZ = 0; 058 059/** This is a possible destination type */ 060 public static final int FIT = 1; 061 062/** This is a possible destination type */ 063 public static final int FITH = 2; 064 065/** This is a possible destination type */ 066 public static final int FITV = 3; 067 068/** This is a possible destination type */ 069 public static final int FITR = 4; 070 071/** This is a possible destination type */ 072 public static final int FITB = 5; 073 074/** This is a possible destination type */ 075 public static final int FITBH = 6; 076 077/** This is a possible destination type */ 078 public static final int FITBV = 7; 079 080 // member variables 081 082/** Is the indirect reference to a page already added? */ 083 private boolean status = false; 084 085 // constructors 086 087/** 088 * Constructs a new <CODE>PdfDestination</CODE>. 089 * <P> 090 * If <VAR>type</VAR> equals <VAR>FITB</VAR>, the bounding box of a page 091 * will fit the window of the Reader. Otherwise the type will be set to 092 * <VAR>FIT</VAR> so that the entire page will fit to the window. 093 * 094 * @param type The destination type 095 */ 096 097 public PdfDestination(int type) { 098 super(); 099 if (type == FITB) { 100 add(PdfName.FITB); 101 } 102 else { 103 add(PdfName.FIT); 104 } 105 } 106 107/** 108 * Constructs a new <CODE>PdfDestination</CODE>. 109 * <P> 110 * If <VAR>type</VAR> equals <VAR>FITBH</VAR> / <VAR>FITBV</VAR>, 111 * the width / height of the bounding box of a page will fit the window 112 * of the Reader. The parameter will specify the y / x coordinate of the 113 * top / left edge of the window. If the <VAR>type</VAR> equals <VAR>FITH</VAR> 114 * or <VAR>FITV</VAR> the width / height of the entire page will fit 115 * the window and the parameter will specify the y / x coordinate of the 116 * top / left edge. In all other cases the type will be set to <VAR>FITH</VAR>. 117 * 118 * @param type the destination type 119 * @param parameter a parameter to combined with the destination type 120 */ 121 122 public PdfDestination(int type, float parameter) { 123 super(new PdfNumber(parameter)); 124 switch(type) { 125 default: 126 addFirst(PdfName.FITH); 127 break; 128 case FITV: 129 addFirst(PdfName.FITV); 130 break; 131 case FITBH: 132 addFirst(PdfName.FITBH); 133 break; 134 case FITBV: 135 addFirst(PdfName.FITBV); 136 } 137 } 138 139/** Constructs a new <CODE>PdfDestination</CODE>. 140 * <P> 141 * Display the page, with the coordinates (left, top) positioned 142 * at the top-left corner of the window and the contents of the page magnified 143 * by the factor zoom. A negative value for any of the parameters left or top, or a 144 * zoom value of 0 specifies that the current value of that parameter is to be retained unchanged. 145 * @param type must be a <VAR>PdfDestination.XYZ</VAR> 146 * @param left the left value. Negative to place a null 147 * @param top the top value. Negative to place a null 148 * @param zoom The zoom factor. A value of 0 keeps the current value 149 */ 150 151 public PdfDestination(int type, float left, float top, float zoom) { 152 super(PdfName.XYZ); 153 if (left < 0) 154 add(PdfNull.PDFNULL); 155 else 156 add(new PdfNumber(left)); 157 if (top < 0) 158 add(PdfNull.PDFNULL); 159 else 160 add(new PdfNumber(top)); 161 add(new PdfNumber(zoom)); 162 } 163 164/** Constructs a new <CODE>PdfDestination</CODE>. 165 * <P> 166 * Display the page, with its contents magnified just enough 167 * to fit the rectangle specified by the coordinates left, bottom, right, and top 168 * entirely within the window both horizontally and vertically. If the required 169 * horizontal and vertical magnification factors are different, use the smaller of 170 * the two, centering the rectangle within the window in the other dimension. 171 * 172 * @param type must be PdfDestination.FITR 173 * @param left a parameter 174 * @param bottom a parameter 175 * @param right a parameter 176 * @param top a parameter 177 * @since iText0.38 178 */ 179 180 public PdfDestination(int type, float left, float bottom, float right, float top) { 181 super(PdfName.FITR); 182 add(new PdfNumber(left)); 183 add(new PdfNumber(bottom)); 184 add(new PdfNumber(right)); 185 add(new PdfNumber(top)); 186 } 187 188 /** 189 * Creates a PdfDestination based on a String. 190 * Valid Strings are for instance the values returned by SimpleNamedDestination: 191 * "Fit", "XYZ 36 806 0",... 192 * @param dest a String notation of a destination. 193 * @since iText 5.0 194 */ 195 public PdfDestination(String dest) { 196 super(); 197 StringTokenizer tokens = new StringTokenizer(dest); 198 if (tokens.hasMoreTokens()) { 199 add(new PdfName(tokens.nextToken())); 200 } 201 while (tokens.hasMoreTokens()) { 202 add(new PdfNumber(tokens.nextToken())); 203 } 204 } 205 206 // methods 207 208/** 209 * Checks if an indirect reference to a page has been added. 210 * 211 * @return <CODE>true</CODE> or <CODE>false</CODE> 212 */ 213 214 public boolean hasPage() { 215 return status; 216 } 217 218/** Adds the indirect reference of the destination page. 219 * 220 * @param page an indirect reference 221 * @return true if the page reference was added 222 */ 223 224 public boolean addPage(PdfIndirectReference page) { 225 if (!status) { 226 addFirst(page); 227 status = true; 228 return true; 229 } 230 return false; 231 } 232}