1 20 package gnu.jpdf; 21 22 import java.io.*; 23 import java.util.*; 24 25 33 public class PDFOutline extends PDFObject implements Serializable 34 { 35 36 44 45 48 private Vector outlines; 49 50 53 protected PDFOutline parent; 54 55 58 private String title; 59 60 63 PDFPage dest; 64 65 68 int l,b,r,t; 69 70 73 boolean destMode; 74 75 78 static final boolean FITPAGE = false; 79 80 83 static final boolean FITRECT = true; 84 85 88 protected PDFOutline() 89 { 90 super("/Outlines"); 91 outlines = new Vector(); 92 title = null; 93 dest = null; 94 destMode = FITPAGE; 95 } 96 97 104 public PDFOutline(String title,PDFPage dest) 105 { 106 this(); 107 this.title = title; 108 this.dest = dest; 109 } 110 111 122 public PDFOutline(String title,PDFPage dest,int l,int b,int r,int t) 123 { 124 this(title,dest); 125 this.destMode = FITRECT; 126 this.l = l; 127 this.b = b; 128 this.r = r; 129 this.t = t; 130 } 131 132 145 public PDFOutline add(String title,PDFPage dest) { 146 PDFOutline outline = new PDFOutline(title,dest); 147 pdfDocument.add(outline); add(outline); 149 return outline; 150 } 151 152 172 public PDFOutline add(String title,PDFPage dest, 173 int x,int y,int w,int h) { 174 int xy1[] = dest.cxy(x,y+h); 175 int xy2[] = dest.cxy(x+w,y); 176 PDFOutline outline = new PDFOutline(title,dest, 177 xy1[0],xy1[1], 178 xy2[0],xy2[1]); 179 pdfDocument.add(outline); add(outline); 181 return outline; 182 } 183 184 192 public void add(PDFOutline outline) 193 { 194 outlines.addElement(outline); 195 196 outline.parent = this; 198 } 199 200 204 public void write(OutputStream os) throws IOException 205 { 206 writeStart(os); 208 209 211 if(parent!=null) { 213 os.write("/Title ".getBytes()); 214 os.write(PDFStringHelper.makePDFString(title).getBytes()); 215 os.write("\n/Dest [".getBytes()); 216 os.write(dest.toString().getBytes()); 217 218 if(destMode==FITPAGE) { 219 os.write(" /Fit]\n/Parent ".getBytes()); 221 } else { 222 os.write(" /FitR ".getBytes()); 223 os.write(Integer.toString(l).getBytes()); 224 os.write(" ".getBytes()); 225 os.write(Integer.toString(b).getBytes()); 226 os.write(" ".getBytes()); 227 os.write(Integer.toString(r).getBytes()); 228 os.write(" ".getBytes()); 229 os.write(Integer.toString(t).getBytes()); 230 os.write("]\n/Parent ".getBytes()); 231 } 232 os.write(parent.toString().getBytes()); 233 os.write("\n".getBytes()); 234 } 235 236 if(parent==null) { 238 os.write("/Count ".getBytes()); 240 os.write(Integer.toString(outlines.size()).getBytes()); 241 os.write("\n".getBytes()); 242 } else { 243 int c = descendants(); 246 if(c>0) { 247 os.write("/Count ".getBytes()); 248 os.write(Integer.toString(-c).getBytes()); 249 os.write("\n".getBytes()); 250 } 251 } 252 253 if(outlines.size()>0) { 255 os.write("/First ".getBytes()); 257 os.write(outlines.elementAt(0).toString().getBytes()); 258 os.write("\n".getBytes()); 259 260 os.write("/Last ".getBytes()); 262 os.write(outlines.elementAt(outlines.size()-1).toString().getBytes()); 263 os.write("\n".getBytes()); 264 } 265 266 if(parent!=null) { 267 int index = parent.getIndex(this); 268 if(index>0) { 269 os.write("/Prev ".getBytes()); 271 os.write(parent.getNode(index-1).toString().getBytes()); 272 os.write("\n".getBytes()); 273 } 274 if(index<parent.getLast()) { 275 os.write("/Next ".getBytes()); 277 os.write(parent.getNode(index+1).toString().getBytes()); 278 os.write("\n".getBytes()); 279 } 280 } 281 282 writeEnd(os); 284 } 285 286 293 protected int getIndex(PDFOutline outline) 294 { 295 return outlines.indexOf(outline); 296 } 297 298 302 protected int getLast() 303 { 304 return outlines.size()-1; 305 } 306 307 312 protected PDFOutline getNode(int i) 313 { 314 return (PDFOutline)(outlines.elementAt(i)); 315 } 316 317 321 public Enumeration elements() 322 { 323 return outlines.elements(); 324 } 325 326 330 protected int descendants() 331 { 332 int c = outlines.size(); 334 for(Enumeration en = outlines.elements(); en.hasMoreElements(); ) { 336 c += ((PDFOutline)en.nextElement()).descendants(); 337 } 338 339 return c; 340 } 341 } | Popular Tags |