1 50 51 package com.lowagie.text.pdf; 52 53 import java.awt.Color ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 59 import com.lowagie.text.Chunk; 60 import com.lowagie.text.Font; 61 import com.lowagie.text.Paragraph; 62 63 72 73 public class PdfOutline extends PdfDictionary { 74 75 77 78 private PdfIndirectReference reference; 79 80 81 private int count = 0; 82 83 84 private PdfOutline parent; 85 86 87 private PdfDestination destination; 88 89 91 private PdfAction action; 92 93 protected ArrayList kids = new ArrayList (); 94 95 protected PdfWriter writer; 96 97 98 private String tag; 99 100 101 private boolean open; 102 103 104 private Color color; 105 106 107 private int style = 0; 108 109 111 118 119 PdfOutline(PdfWriter writer) { 120 super(OUTLINES); 121 open = true; 122 parent = null; 123 this.writer = writer; 124 } 125 126 136 137 public PdfOutline(PdfOutline parent, PdfAction action, String title) { 138 this(parent, action, title, true); 139 } 140 141 151 public PdfOutline(PdfOutline parent, PdfAction action, String title, boolean open) { 152 super(); 153 this.action = action; 154 initOutline(parent, title, open); 155 } 156 157 167 168 public PdfOutline(PdfOutline parent, PdfDestination destination, String title) { 169 this(parent, destination, title, true); 170 } 171 172 182 public PdfOutline(PdfOutline parent, PdfDestination destination, String title, boolean open) { 183 super(); 184 this.destination = destination; 185 initOutline(parent, title, open); 186 } 187 188 198 public PdfOutline(PdfOutline parent, PdfAction action, PdfString title) { 199 this(parent, action, title, true); 200 } 201 202 212 public PdfOutline(PdfOutline parent, PdfAction action, PdfString title, boolean open) { 213 this(parent, action, title.toString(), open); 214 } 215 216 226 227 public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title) { 228 this(parent, destination, title, true); 229 } 230 231 241 public PdfOutline(PdfOutline parent, PdfDestination destination, PdfString title, boolean open) { 242 this(parent, destination, title.toString(), true); 243 } 244 245 255 256 public PdfOutline(PdfOutline parent, PdfAction action, Paragraph title) { 257 this(parent, action, title, true); 258 } 259 260 270 public PdfOutline(PdfOutline parent, PdfAction action, Paragraph title, boolean open) { 271 super(); 272 StringBuffer buf = new StringBuffer (); 273 for (Iterator i = title.getChunks().iterator(); i.hasNext(); ) { 274 Chunk chunk = (Chunk) i.next(); 275 buf.append(chunk.getContent()); 276 } 277 this.action = action; 278 initOutline(parent, buf.toString(), open); 279 } 280 281 291 292 public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title) { 293 this(parent, destination, title, true); 294 } 295 296 306 public PdfOutline(PdfOutline parent, PdfDestination destination, Paragraph title, boolean open) { 307 super(); 308 StringBuffer buf = new StringBuffer (); 309 for (Iterator i = title.getChunks().iterator(); i.hasNext(); ) { 310 Chunk chunk = (Chunk) i.next(); 311 buf.append(chunk.getContent()); 312 } 313 this.destination = destination; 314 initOutline(parent, buf.toString(), open); 315 } 316 317 318 320 325 void initOutline(PdfOutline parent, String title, boolean open) { 326 this.open = open; 327 this.parent = parent; 328 writer = parent.writer; 329 put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE)); 330 parent.addKid(this); 331 if (destination != null && !destination.hasPage()) setDestinationPage(writer.getCurrentPage()); 333 } 334 335 340 341 public void setIndirectReference(PdfIndirectReference reference) { 342 this.reference = reference; 343 } 344 345 350 351 public PdfIndirectReference indirectReference() { 352 return reference; 353 } 354 355 360 361 public PdfOutline parent() { 362 return parent; 363 } 364 365 371 372 public boolean setDestinationPage(PdfIndirectReference pageReference) { 373 if (destination == null) { 374 return false; 375 } 376 return destination.addPage(pageReference); 377 } 378 379 383 public PdfDestination getPdfDestination() { 384 return destination; 385 } 386 387 int getCount() { 388 return count; 389 } 390 391 void setCount(int count) { 392 this.count = count; 393 } 394 395 400 401 public int level() { 402 if (parent == null) { 403 return 0; 404 } 405 return (parent.level() + 1); 406 } 407 408 415 416 public void toPdf(PdfWriter writer, OutputStream os) throws IOException { 417 if (color != null && !color.equals(Color.black)) { 418 put(PdfName.C, new PdfArray(new float[]{color.getRed()/255f,color.getGreen()/255f,color.getBlue()/255f})); 419 } 420 int flag = 0; 421 if ((style & Font.BOLD) != 0) 422 flag |= 2; 423 if ((style & Font.ITALIC) != 0) 424 flag |= 1; 425 if (flag != 0) 426 put(PdfName.F, new PdfNumber(flag)); 427 if (parent != null) { 428 put(PdfName.PARENT, parent.indirectReference()); 429 } 430 if (destination != null && destination.hasPage()) { 431 put(PdfName.DEST, destination); 432 } 433 if (action != null) 434 put(PdfName.A, action); 435 if (count != 0) { 436 put(PdfName.COUNT, new PdfNumber(count)); 437 } 438 super.toPdf(writer, os); 439 } 440 441 445 public void addKid(PdfOutline outline) { 446 kids.add(outline); 447 } 448 449 453 public ArrayList getKids() { 454 return kids; 455 } 456 457 461 public void setKids(ArrayList kids) { 462 this.kids = kids; 463 } 464 465 468 public String getTag() { 469 return tag; 470 } 471 472 475 public void setTag(String tag) { 476 this.tag = tag; 477 } 478 479 483 public String getTitle() { 484 PdfString title = (PdfString)get(PdfName.TITLE); 485 return title.toString(); 486 } 487 488 492 public void setTitle(String title) { 493 put(PdfName.TITLE, new PdfString(title, PdfObject.TEXT_UNICODE)); 494 } 495 496 499 public boolean isOpen() { 500 return open; 501 } 502 503 506 public void setOpen(boolean open) { 507 this.open = open; 508 } 509 510 514 public Color getColor() { 515 return this.color; 516 } 517 518 522 public void setColor(Color color) { 523 this.color = color; 524 } 525 526 530 public int getStyle() { 531 return this.style; 532 } 533 534 538 public void setStyle(int style) { 539 this.style = style; 540 } 541 542 } | Popular Tags |