KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jpdf > PDFAnnot


1 /*
2  * $Id: PDFAnnot.java,v 1.3 2001/11/16 15:26:04 ezb Exp $
3  *
4  * $Date: 2001/11/16 15:26:04 $
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */

20 package gnu.jpdf;
21
22 import java.awt.*;
23 import java.io.*;
24 import java.util.*;
25
26 /**
27  * <p>This class defines an annotation (commonly known as a Bookmark).</p>
28  *
29  * @author Eric Z. Beard, ericzbeard@hotmail.com
30  * @author Peter T Mount, http://www.retep.org.uk/pdf/
31  * @author $Author: ezb $
32  * @version $Revision: 1.3 $, $Date: 2001/11/16 15:26:04 $
33  */

34 public class PDFAnnot extends PDFObject implements Serializable
35 {
36   /*
37    * NOTE: The original class is the work of Peter T. Mount, who released it
38    * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
39    * follows: the package name was changed to gnu.pdf. It is still
40    * licensed under the LGPL.
41    */

42
43   /**
44    * Solid border. The border is drawn as a solid line.
45    */

46   public static final short SOLID = 0;
47   
48   /**
49    * The border is drawn with a dashed line.
50    */

51   public static final short DASHED = 1;
52   
53   /**
54    * The border is drawn in a beveled style (faux three-dimensional) such
55    * that it looks as if it is pushed out of the page (opposite of INSET)
56    */

57   public static final short BEVELED = 2;
58   
59   /**
60    * The border is drawn in an inset style (faux three-dimensional) such
61    * that it looks as if it is inset into the page (opposite of BEVELED)
62    */

63   public static final short INSET = 3;
64   
65   /**
66    * The border is drawn as a line on the bottom of the annotation rectangle
67    */

68   public static final short UNDERLINED = 4;
69   
70   /**
71    * The subtype of the outline, ie text, note, etc
72    */

73   private String JavaDoc subtype;
74   
75   /**
76    * The size of the annotation
77    */

78   private int l,b,r,t;
79   
80   /**
81    * The text of a text annotation
82    */

83   private String JavaDoc s;
84   
85   /**
86    * flag used to indicate that the destination should fit the screen
87    */

88   private static final int FULL_PAGE = -9999;
89   
90   /**
91    * Link to the Destination page
92    */

93   private PDFObject dest;
94   
95   /**
96    * If fl!=FULL_PAGE then this is the region of the destination page shown.
97    * Otherwise they are ignored.
98    */

99   private int fl,fb,fr,ft;
100   
101   /**
102    * the border for this annotation
103    */

104   private PDFBorder border;
105   
106   /**
107    * This is used to create an annotation.
108    * @param s Subtype for this annotation
109    * @param l Left coordinate
110    * @param b Bottom coordinate
111    * @param r Right coordinate
112    * @param t Top coordinate
113    */

114   protected PDFAnnot(String JavaDoc s,int l,int b,int r,int t) {
115     super("/Annot");
116     subtype = s;
117     this.l = l;
118     this.b = b;
119     this.r = r;
120     this.t = t;
121   }
122   
123   /**
124    * Creates a text annotation
125    * @param l Left coordinate
126    * @param b Bottom coordinate
127    * @param r Right coordinate
128    * @param t Top coordinate
129    * @param s Text for this annotation
130    */

131   public PDFAnnot(int l,int b,int r,int t,String JavaDoc s) {
132     this("/Text",l,b,r,t);
133     this.s = s;
134   }
135   
136   /**
137    * Creates a link annotation
138    * @param l Left coordinate
139    * @param b Bottom coordinate
140    * @param r Right coordinate
141    * @param t Top coordinate
142    * @param dest Destination for this link. The page will fit the display.
143    */

144   public PDFAnnot(int l,int b,int r,int t,PDFObject dest) {
145     this("/Link",l,b,r,t);
146     this.dest = dest;
147     this.fl = FULL_PAGE; // this is used to indicate a full page
148
}
149   
150   /**
151    * Creates a link annotation
152    * @param l Left coordinate
153    * @param b Bottom coordinate
154    * @param r Right coordinate
155    * @param t Top coordinate
156    * @param dest Destination for this link
157    * @param rect Rectangle describing what part of the page to be displayed
158    * (must be in User Coordinates)
159    */

160   public PDFAnnot(int l,int b,int r,int t,
161                   PDFObject dest,
162                   int fl,int fb,int fr,int ft
163                   ) {
164     this("/Link",l,b,r,t);
165     this.dest = dest;
166     this.fl = fl;
167     this.fb = fb;
168     this.fr = fr;
169     this.ft = ft;
170   }
171   
172   /**
173    * Sets the border for the annotation. By default, no border is defined.
174    *
175    * <p>If the style is DASHED, then this method uses PDF's default dash
176    * scheme {3}
177    *
178    * <p>Important: the annotation must have been added to the document before
179    * this is used. If the annotation was created using the methods in
180    * PDFPage, then the annotation is already in the document.
181    *
182    * @param style Border style SOLID, DASHED, BEVELED, INSET or UNDERLINED.
183    * @param width Width of the border
184    */

185   public void setBorder(short style,double width) {
186     border = new PDFBorder(style,width);
187     pdfDocument.add(border);
188   }
189   
190   /**
191    * Sets the border for the annotation. Unlike the other method, this
192    * produces a dashed border.
193    *
194    * <p>Important: the annotation must have been added to the document before
195    * this is used. If the annotation was created using the methods in
196    * PDFPage, then the annotation is already in the document.
197    *
198    * @param width Width of the border
199    * @param dash Array of lengths, used for drawing the dashes. If this
200    * is null, then the default of {3} is used.
201    */

202   public void setBorder(double width,double dash[]) {
203     border = new PDFBorder(width,dash);
204     pdfDocument.add(border);
205   }
206   
207   /**
208    * Should this be public??
209    *
210    * @param os OutputStream to send the object to
211    * @exception IOException on error
212    */

213   public void write(OutputStream os) throws IOException {
214     // Write the object header
215
writeStart(os);
216     
217     // now the objects body
218
os.write("/Subtype ".getBytes());
219     os.write(subtype.getBytes());
220     os.write("\n/Rect [".getBytes());
221     os.write(Integer.toString(l).getBytes());
222     os.write(" ".getBytes());
223     os.write(Integer.toString(b).getBytes());
224     os.write(" ".getBytes());
225     os.write(Integer.toString(r).getBytes());
226     os.write(" ".getBytes());
227     os.write(Integer.toString(t).getBytes());
228     os.write("]\n".getBytes());
229     
230     // handle the border
231
if(border==null) {
232       os.write("/Border [0 0 0]\n".getBytes());
233       //if(pdf.defaultOutlineBorder==null)
234
//pdf.add(pdf.defaultOutlineBorder = new border(SOLID,0.0));
235
//os.write(pdf.defaultOutlineBorder.toString().getBytes());
236
} else {
237       os.write("/BS ".getBytes());
238       os.write(border.toString().getBytes());
239       os.write("\n".getBytes());
240     }
241     
242     // Now the annotation subtypes
243
if(subtype.equals("/Text")) {
244       os.write("/Contents ".getBytes());
245       os.write(PDFStringHelper.makePDFString(s).getBytes());
246       os.write("\n".getBytes());
247     } else if(subtype.equals("/Link")) {
248       os.write("/Dest [".getBytes());
249       os.write(dest.toString().getBytes());
250       if(fl==FULL_PAGE)
251         os.write(" /Fit]".getBytes());
252       else {
253         os.write(" /FitR ".getBytes());
254         os.write(Integer.toString(fl).getBytes());
255         os.write(" ".getBytes());
256         os.write(Integer.toString(fb).getBytes());
257         os.write(" ".getBytes());
258         os.write(Integer.toString(fr).getBytes());
259         os.write(" ".getBytes());
260         os.write(Integer.toString(ft).getBytes());
261         os.write("]".getBytes());
262       }
263       os.write("\n".getBytes());
264     }
265     
266     // finish off with its footer
267
writeEnd(os);
268   }
269 }
270
Popular Tags