001: /*
002: * $Id: PDFAnnot.java,v 1.3 2001/11/16 15:26:04 ezb Exp $
003: *
004: * $Date: 2001/11/16 15:26:04 $
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020: package gnu.jpdf;
021:
022: import java.awt.*;
023: import java.io.*;
024: import java.util.*;
025:
026: /**
027: * <p>This class defines an annotation (commonly known as a Bookmark).</p>
028: *
029: * @author Eric Z. Beard, ericzbeard@hotmail.com
030: * @author Peter T Mount, http://www.retep.org.uk/pdf/
031: * @author $Author: ezb $
032: * @version $Revision: 1.3 $, $Date: 2001/11/16 15:26:04 $
033: */
034: public class PDFAnnot extends PDFObject implements Serializable {
035: /*
036: * NOTE: The original class is the work of Peter T. Mount, who released it
037: * in the uk.org.retep.pdf package. It was modified by Eric Z. Beard as
038: * follows: the package name was changed to gnu.pdf. It is still
039: * licensed under the LGPL.
040: */
041:
042: /**
043: * Solid border. The border is drawn as a solid line.
044: */
045: public static final short SOLID = 0;
046:
047: /**
048: * The border is drawn with a dashed line.
049: */
050: public static final short DASHED = 1;
051:
052: /**
053: * The border is drawn in a beveled style (faux three-dimensional) such
054: * that it looks as if it is pushed out of the page (opposite of INSET)
055: */
056: public static final short BEVELED = 2;
057:
058: /**
059: * The border is drawn in an inset style (faux three-dimensional) such
060: * that it looks as if it is inset into the page (opposite of BEVELED)
061: */
062: public static final short INSET = 3;
063:
064: /**
065: * The border is drawn as a line on the bottom of the annotation rectangle
066: */
067: public static final short UNDERLINED = 4;
068:
069: /**
070: * The subtype of the outline, ie text, note, etc
071: */
072: private String subtype;
073:
074: /**
075: * The size of the annotation
076: */
077: private int l, b, r, t;
078:
079: /**
080: * The text of a text annotation
081: */
082: private String s;
083:
084: /**
085: * flag used to indicate that the destination should fit the screen
086: */
087: private static final int FULL_PAGE = -9999;
088:
089: /**
090: * Link to the Destination page
091: */
092: private PDFObject dest;
093:
094: /**
095: * If fl!=FULL_PAGE then this is the region of the destination page shown.
096: * Otherwise they are ignored.
097: */
098: private int fl, fb, fr, ft;
099:
100: /**
101: * the border for this annotation
102: */
103: private PDFBorder border;
104:
105: /**
106: * This is used to create an annotation.
107: * @param s Subtype for this annotation
108: * @param l Left coordinate
109: * @param b Bottom coordinate
110: * @param r Right coordinate
111: * @param t Top coordinate
112: */
113: protected PDFAnnot(String s, int l, int b, int r, int t) {
114: super ("/Annot");
115: subtype = s;
116: this .l = l;
117: this .b = b;
118: this .r = r;
119: this .t = t;
120: }
121:
122: /**
123: * Creates a text annotation
124: * @param l Left coordinate
125: * @param b Bottom coordinate
126: * @param r Right coordinate
127: * @param t Top coordinate
128: * @param s Text for this annotation
129: */
130: public PDFAnnot(int l, int b, int r, int t, String s) {
131: this ("/Text", l, b, r, t);
132: this .s = s;
133: }
134:
135: /**
136: * Creates a link annotation
137: * @param l Left coordinate
138: * @param b Bottom coordinate
139: * @param r Right coordinate
140: * @param t Top coordinate
141: * @param dest Destination for this link. The page will fit the display.
142: */
143: public PDFAnnot(int l, int b, int r, int t, PDFObject dest) {
144: this ("/Link", l, b, r, t);
145: this .dest = dest;
146: this .fl = FULL_PAGE; // this is used to indicate a full page
147: }
148:
149: /**
150: * Creates a link annotation
151: * @param l Left coordinate
152: * @param b Bottom coordinate
153: * @param r Right coordinate
154: * @param t Top coordinate
155: * @param dest Destination for this link
156: * @param rect Rectangle describing what part of the page to be displayed
157: * (must be in User Coordinates)
158: */
159: public PDFAnnot(int l, int b, int r, int t, PDFObject dest, int fl,
160: int fb, int fr, int ft) {
161: this ("/Link", l, b, r, t);
162: this .dest = dest;
163: this .fl = fl;
164: this .fb = fb;
165: this .fr = fr;
166: this .ft = ft;
167: }
168:
169: /**
170: * Sets the border for the annotation. By default, no border is defined.
171: *
172: * <p>If the style is DASHED, then this method uses PDF's default dash
173: * scheme {3}
174: *
175: * <p>Important: the annotation must have been added to the document before
176: * this is used. If the annotation was created using the methods in
177: * PDFPage, then the annotation is already in the document.
178: *
179: * @param style Border style SOLID, DASHED, BEVELED, INSET or UNDERLINED.
180: * @param width Width of the border
181: */
182: public void setBorder(short style, double width) {
183: border = new PDFBorder(style, width);
184: pdfDocument.add(border);
185: }
186:
187: /**
188: * Sets the border for the annotation. Unlike the other method, this
189: * produces a dashed border.
190: *
191: * <p>Important: the annotation must have been added to the document before
192: * this is used. If the annotation was created using the methods in
193: * PDFPage, then the annotation is already in the document.
194: *
195: * @param width Width of the border
196: * @param dash Array of lengths, used for drawing the dashes. If this
197: * is null, then the default of {3} is used.
198: */
199: public void setBorder(double width, double dash[]) {
200: border = new PDFBorder(width, dash);
201: pdfDocument.add(border);
202: }
203:
204: /**
205: * Should this be public??
206: *
207: * @param os OutputStream to send the object to
208: * @exception IOException on error
209: */
210: public void write(OutputStream os) throws IOException {
211: // Write the object header
212: writeStart(os);
213:
214: // now the objects body
215: os.write("/Subtype ".getBytes());
216: os.write(subtype.getBytes());
217: os.write("\n/Rect [".getBytes());
218: os.write(Integer.toString(l).getBytes());
219: os.write(" ".getBytes());
220: os.write(Integer.toString(b).getBytes());
221: os.write(" ".getBytes());
222: os.write(Integer.toString(r).getBytes());
223: os.write(" ".getBytes());
224: os.write(Integer.toString(t).getBytes());
225: os.write("]\n".getBytes());
226:
227: // handle the border
228: if (border == null) {
229: os.write("/Border [0 0 0]\n".getBytes());
230: //if(pdf.defaultOutlineBorder==null)
231: //pdf.add(pdf.defaultOutlineBorder = new border(SOLID,0.0));
232: //os.write(pdf.defaultOutlineBorder.toString().getBytes());
233: } else {
234: os.write("/BS ".getBytes());
235: os.write(border.toString().getBytes());
236: os.write("\n".getBytes());
237: }
238:
239: // Now the annotation subtypes
240: if (subtype.equals("/Text")) {
241: os.write("/Contents ".getBytes());
242: os.write(PDFStringHelper.makePDFString(s).getBytes());
243: os.write("\n".getBytes());
244: } else if (subtype.equals("/Link")) {
245: os.write("/Dest [".getBytes());
246: os.write(dest.toString().getBytes());
247: if (fl == FULL_PAGE)
248: os.write(" /Fit]".getBytes());
249: else {
250: os.write(" /FitR ".getBytes());
251: os.write(Integer.toString(fl).getBytes());
252: os.write(" ".getBytes());
253: os.write(Integer.toString(fb).getBytes());
254: os.write(" ".getBytes());
255: os.write(Integer.toString(fr).getBytes());
256: os.write(" ".getBytes());
257: os.write(Integer.toString(ft).getBytes());
258: os.write("]".getBytes());
259: }
260: os.write("\n".getBytes());
261: }
262:
263: // finish off with its footer
264: writeEnd(os);
265: }
266: }
|