1 50 51 package com.lowagie.text; 52 53 import java.text.SimpleDateFormat ; 54 import java.util.ArrayList ; 55 import java.util.Date ; 56 import java.util.Iterator ; 57 58 99 100 public class Document implements DocListener { 101 102 104 105 private static final String ITEXT_VERSION = "iText 2.0.4 (by lowagie.com)"; 106 107 111 public static boolean compress = true; 112 113 117 public static boolean plainRandomAccess = false; 118 119 120 private ArrayList listeners = new ArrayList (); 121 122 123 protected boolean open; 124 125 126 protected boolean close; 127 128 130 131 protected Rectangle pageSize; 132 133 134 protected float marginLeft = 0; 135 136 137 protected float marginRight = 0; 138 139 140 protected float marginTop = 0; 141 142 143 protected float marginBottom = 0; 144 145 protected boolean marginMirroring = false; 146 147 148 protected String javaScript_onLoad = null; 149 150 151 protected String javaScript_onUnLoad = null; 152 153 154 protected String htmlStyleClass = null; 155 156 158 159 protected int pageN = 0; 160 161 162 protected HeaderFooter header = null; 163 164 165 protected HeaderFooter footer = null; 166 167 168 protected int chapternumber = 0; 169 170 172 175 176 public Document() { 177 this(PageSize.A4); 178 } 179 180 186 187 public Document(Rectangle pageSize) { 188 this(pageSize, 36, 36, 36, 36); 189 } 190 191 205 206 public Document(Rectangle pageSize, float marginLeft, float marginRight, 207 float marginTop, float marginBottom) { 208 this.pageSize = pageSize; 209 this.marginLeft = marginLeft; 210 this.marginRight = marginRight; 211 this.marginTop = marginTop; 212 this.marginBottom = marginBottom; 213 } 214 215 217 223 224 public void addDocListener(DocListener listener) { 225 listeners.add(listener); 226 } 227 228 234 235 public void removeDocListener(DocListener listener) { 236 listeners.remove(listener); 237 } 238 239 241 251 252 public boolean add(Element element) throws DocumentException { 253 if (close) { 254 throw new DocumentException( 255 "The document has been closed. You can't add any Elements."); 256 } 257 int type = element.type(); 258 if (open) { 259 if (!(type == Element.CHUNK || type == Element.PHRASE 260 || type == Element.PARAGRAPH || type == Element.TABLE 261 || type == Element.PTABLE 262 || type == Element.MULTI_COLUMN_TEXT 263 || type == Element.ANCHOR || type == Element.ANNOTATION 264 || type == Element.CHAPTER || type == Element.SECTION 265 || type == Element.LIST || type == Element.LISTITEM 266 || type == Element.RECTANGLE || type == Element.JPEG 267 || type == Element.IMGRAW || type == Element.IMGTEMPLATE 268 || type == Element.MARKED)) { 269 throw new DocumentException( 270 "The document is open; you can only add Elements with content."); 271 } 272 } else { 273 if (!(type == Element.HEADER || type == Element.TITLE 274 || type == Element.SUBJECT || type == Element.KEYWORDS 275 || type == Element.AUTHOR || type == Element.PRODUCER 276 || type == Element.CREATOR || type == Element.CREATIONDATE 277 || type == Element.MARKED)) { 278 throw new DocumentException( 279 "The document is not open yet; you can only add Meta information."); 280 } 281 } 282 boolean success = false; 283 DocListener listener; 284 if (element instanceof ChapterAutoNumber) { 285 chapternumber++; 286 ((ChapterAutoNumber)element).setChapterNumber(chapternumber); 287 } 288 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 289 listener = (DocListener) iterator.next(); 290 success |= listener.add(element); 291 } 292 return success; 293 } 294 295 302 303 public void open() { 304 if (!close) { 305 open = true; 306 } 307 DocListener listener; 308 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 309 listener = (DocListener) iterator.next(); 310 listener.setPageSize(pageSize); 311 listener.setMargins(marginLeft, marginRight, marginTop, 312 marginBottom); 313 listener.open(); 314 } 315 } 316 317 324 325 public boolean setPageSize(Rectangle pageSize) { 326 this.pageSize = pageSize; 327 DocListener listener; 328 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 329 listener = (DocListener) iterator.next(); 330 listener.setPageSize(pageSize); 331 } 332 return true; 333 } 334 335 348 349 public boolean setMargins(float marginLeft, float marginRight, 350 float marginTop, float marginBottom) { 351 this.marginLeft = marginLeft; 352 this.marginRight = marginRight; 353 this.marginTop = marginTop; 354 this.marginBottom = marginBottom; 355 DocListener listener; 356 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 357 listener = (DocListener) iterator.next(); 358 listener.setMargins(marginLeft, marginRight, marginTop, 359 marginBottom); 360 } 361 return true; 362 } 363 364 372 373 public boolean newPage() { 374 if (!open || close) { 375 return false; 376 } 377 DocListener listener; 378 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 379 listener = (DocListener) iterator.next(); 380 listener.newPage(); 381 } 382 return true; 383 } 384 385 391 392 public void setHeader(HeaderFooter header) { 393 this.header = header; 394 DocListener listener; 395 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 396 listener = (DocListener) iterator.next(); 397 listener.setHeader(header); 398 } 399 } 400 401 404 405 public void resetHeader() { 406 this.header = null; 407 DocListener listener; 408 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 409 listener = (DocListener) iterator.next(); 410 listener.resetHeader(); 411 } 412 } 413 414 420 421 public void setFooter(HeaderFooter footer) { 422 this.footer = footer; 423 DocListener listener; 424 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 425 listener = (DocListener) iterator.next(); 426 listener.setFooter(footer); 427 } 428 } 429 430 433 434 public void resetFooter() { 435 this.footer = null; 436 DocListener listener; 437 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 438 listener = (DocListener) iterator.next(); 439 listener.resetFooter(); 440 } 441 } 442 443 446 447 public void resetPageCount() { 448 pageN = 0; 449 DocListener listener; 450 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 451 listener = (DocListener) iterator.next(); 452 listener.resetPageCount(); 453 } 454 } 455 456 462 463 public void setPageCount(int pageN) { 464 this.pageN = pageN; 465 DocListener listener; 466 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 467 listener = (DocListener) iterator.next(); 468 listener.setPageCount(pageN); 469 } 470 } 471 472 477 478 public int getPageNumber() { 479 return this.pageN; 480 } 481 482 488 489 public void close() { 490 if (!close) { 491 open = false; 492 close = true; 493 } 494 DocListener listener; 495 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 496 listener = (DocListener) iterator.next(); 497 listener.close(); 498 } 499 } 500 501 503 512 513 public boolean addHeader(String name, String content) { 514 try { 515 return add(new Header(name, content)); 516 } catch (DocumentException de) { 517 throw new ExceptionConverter(de); 518 } 519 } 520 521 528 529 public boolean addTitle(String title) { 530 try { 531 return add(new Meta(Element.TITLE, title)); 532 } catch (DocumentException de) { 533 throw new ExceptionConverter(de); 534 } 535 } 536 537 544 545 public boolean addSubject(String subject) { 546 try { 547 return add(new Meta(Element.SUBJECT, subject)); 548 } catch (DocumentException de) { 549 throw new ExceptionConverter(de); 550 } 551 } 552 553 560 561 public boolean addKeywords(String keywords) { 562 try { 563 return add(new Meta(Element.KEYWORDS, keywords)); 564 } catch (DocumentException de) { 565 throw new ExceptionConverter(de); 566 } 567 } 568 569 576 577 public boolean addAuthor(String author) { 578 try { 579 return add(new Meta(Element.AUTHOR, author)); 580 } catch (DocumentException de) { 581 throw new ExceptionConverter(de); 582 } 583 } 584 585 592 593 public boolean addCreator(String creator) { 594 try { 595 return add(new Meta(Element.CREATOR, creator)); 596 } catch (DocumentException de) { 597 throw new ExceptionConverter(de); 598 } 599 } 600 601 606 607 public boolean addProducer() { 608 try { 609 return add(new Meta(Element.PRODUCER, "iText by lowagie.com")); 610 } catch (DocumentException de) { 611 throw new ExceptionConverter(de); 612 } 613 } 614 615 620 621 public boolean addCreationDate() { 622 try { 623 624 final SimpleDateFormat sdf = new SimpleDateFormat ( 625 "EEE MMM dd HH:mm:ss zzz yyyy"); 626 return add(new Meta(Element.CREATIONDATE, sdf.format(new Date ()))); 627 } catch (DocumentException de) { 628 throw new ExceptionConverter(de); 629 } 630 } 631 632 634 639 640 public float leftMargin() { 641 return marginLeft; 642 } 643 644 649 650 public float rightMargin() { 651 return marginRight; 652 } 653 654 659 660 public float topMargin() { 661 return marginTop; 662 } 663 664 669 670 public float bottomMargin() { 671 return marginBottom; 672 } 673 674 679 680 public float left() { 681 return pageSize.getLeft(marginLeft); 682 } 683 684 689 690 public float right() { 691 return pageSize.getRight(marginRight); 692 } 693 694 699 700 public float top() { 701 return pageSize.getTop(marginTop); 702 } 703 704 709 710 public float bottom() { 711 return pageSize.getBottom(marginBottom); 712 } 713 714 721 722 public float left(float margin) { 723 return pageSize.getLeft(marginLeft + margin); 724 } 725 726 733 734 public float right(float margin) { 735 return pageSize.getRight(marginRight + margin); 736 } 737 738 745 746 public float top(float margin) { 747 return pageSize.getTop(marginTop + margin); 748 } 749 750 757 758 public float bottom(float margin) { 759 return pageSize.getBottom(marginBottom + margin); 760 } 761 762 767 768 public Rectangle getPageSize() { 769 return this.pageSize; 770 } 771 772 777 public boolean isOpen() { 778 return open; 779 } 780 781 786 public static final String getVersion() { 787 return ITEXT_VERSION; 788 } 789 790 796 797 public void setJavaScript_onLoad(String code) { 798 this.javaScript_onLoad = code; 799 } 800 801 806 807 public String getJavaScript_onLoad() { 808 return this.javaScript_onLoad; 809 } 810 811 817 818 public void setJavaScript_onUnLoad(String code) { 819 this.javaScript_onUnLoad = code; 820 } 821 822 827 828 public String getJavaScript_onUnLoad() { 829 return this.javaScript_onUnLoad; 830 } 831 832 838 839 public void setHtmlStyleClass(String htmlStyleClass) { 840 this.htmlStyleClass = htmlStyleClass; 841 } 842 843 848 849 public String getHtmlStyleClass() { 850 return this.htmlStyleClass; 851 } 852 853 862 public boolean setMarginMirroring(boolean marginMirroring) { 863 this.marginMirroring = marginMirroring; 864 DocListener listener; 865 for (Iterator iterator = listeners.iterator(); iterator.hasNext();) { 866 listener = (DocListener) iterator.next(); 867 listener.setMarginMirroring(marginMirroring); 868 } 869 return true; 870 } 871 872 877 public boolean isMarginMirroring() { 878 return marginMirroring; 879 } 880 } 881 | Popular Tags |