| 1 50 51 package com.lowagie.text.rtf; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayInputStream ; 55 import java.io.ByteArrayOutputStream ; 56 import java.io.IOException ; 57 import java.io.InputStream ; 58 import java.io.OutputStream ; 59 import java.text.ParsePosition ; 60 import java.text.SimpleDateFormat ; 61 import java.util.ArrayList ; 62 import java.util.Calendar ; 63 import java.util.Date ; 64 import java.util.Iterator ; 65 import java.util.ListIterator ; 66 67 import com.lowagie.text.Anchor; 68 import com.lowagie.text.Annotation; 69 import com.lowagie.text.Chunk; 70 import com.lowagie.text.DocWriter; 71 import com.lowagie.text.Document; 72 import com.lowagie.text.DocumentException; 73 import com.lowagie.text.Element; 74 import com.lowagie.text.ExceptionConverter; 75 import com.lowagie.text.Font; 76 import com.lowagie.text.HeaderFooter; 77 import com.lowagie.text.Image; 78 import com.lowagie.text.ListItem; 79 import com.lowagie.text.Meta; 80 import com.lowagie.text.PageSize; 81 import com.lowagie.text.Paragraph; 82 import com.lowagie.text.Phrase; 83 import com.lowagie.text.Rectangle; 84 import com.lowagie.text.Section; 85 import com.lowagie.text.SimpleTable; 86 import com.lowagie.text.Table; 87 import com.lowagie.text.pdf.codec.wmf.MetaDo; 88 89 141 public class RtfWriter extends DocWriter { 142 145 146 149 150 151 public static final byte escape = (byte) '\\'; 152 153 154 private static final byte[] extendedEscape = "\\*\\".getBytes(); 155 156 157 protected static final byte delimiter = (byte) ' '; 158 159 160 private static final byte commaDelimiter = (byte) ';'; 161 162 163 public static final byte openGroup = (byte) '{'; 164 165 166 public static final byte closeGroup = (byte) '}'; 167 168 171 172 173 private static final byte[] docBegin = "rtf1".getBytes(); 174 175 176 private static final byte[] ansi = "ansi".getBytes(); 177 178 179 private static final byte[] ansiCodepage = "ansicpg".getBytes(); 180 181 184 185 186 private static final byte[] fontTable = "fonttbl".getBytes(); 187 188 189 protected static final byte fontNumber = (byte) 'f'; 190 191 192 protected static final byte[] fontSize = "fs".getBytes(); 193 194 195 protected static final byte[] fontColor = "cf".getBytes(); 196 197 198 private static final byte[] fontModern = "fmodern".getBytes(); 199 200 201 private static final byte[] fontSwiss = "fswiss".getBytes(); 202 203 204 private static final byte[] fontRoman = "froman".getBytes(); 205 206 207 private static final byte[] fontTech = "ftech".getBytes(); 208 209 210 private static final byte[] fontCharset = "fcharset".getBytes(); 211 212 213 private static final byte[] fontCourier = "Courier".getBytes(); 214 215 216 private static final byte[] fontArial = "Arial".getBytes(); 217 218 219 private static final byte[] fontSymbol = "Symbol".getBytes(); 220 221 222 private static final byte[] fontTimesNewRoman = "Times New Roman".getBytes(); 223 224 225 private static final byte[] fontWindings = "Windings".getBytes(); 226 227 228 private static final byte[] defaultFont = "deff".getBytes(); 229 230 231 private static final byte[] firstIndent = "fi".getBytes(); 232 233 234 private static final byte[] listIndent = "li".getBytes(); 235 236 237 private static final byte[] rightIndent = "ri".getBytes(); 238 239 242 243 244 private static final byte[] sectionDefaults = "sectd".getBytes(); 245 246 247 private static final byte[] section = "sect".getBytes(); 248 249 250 public static final byte[] paragraphDefaults = "pard".getBytes(); 251 252 253 public static final byte[] paragraph = "par".getBytes(); 254 255 256 public static final byte[] sectionPageWidth = "pgwsxn".getBytes(); 257 258 259 public static final byte[] sectionPageHeight = "pghsxn".getBytes(); 260 261 264 265 266 private static final byte[] listtableGroup = "listtable".getBytes(); 267 268 269 private static final byte[] listoverridetableGroup = "listoverridetable".getBytes(); 270 271 272 private static final byte[] listDefinition = "list".getBytes(); 273 274 275 private static final byte[] listTemplateID = "listtemplateid".getBytes(); 276 277 278 private static final byte[] hybridList = "hybrid".getBytes(); 279 280 281 private static final byte[] listLevelDefinition = "listlevel".getBytes(); 282 283 284 private static final byte[] listLevelTypeOld = "levelnfc".getBytes(); 285 286 287 private static final byte[] listLevelTypeNew = "levelnfcn".getBytes(); 288 289 290 private static final byte[] listLevelAlignOld = "leveljc".getBytes(); 291 292 293 private static final byte[] listLevelAlignNew = "leveljcn".getBytes(); 294 295 296 private static final byte[] listLevelStartAt = "levelstartat".getBytes(); 297 298 299 private static final byte[] listLevelTextDefinition = "leveltext".getBytes(); 300 301 302 private static final byte[] listLevelTextLength = "\'0".getBytes(); 303 304 305 private static final byte[] listLevelTextStyleNumbers = "\'00.".getBytes(); 306 307 308 private static final byte[] listLevelTextStyleBullet = "u-3913 ?".getBytes(); 309 310 311 private static final byte[] listLevelNumbersDefinition = "levelnumbers".getBytes(); 312 313 314 private static final byte[] listLevelNumbers = "\\'0".getBytes(); 315 316 317 private static final byte[] tabStop = "tx".getBytes(); 318 319 320 private static final byte[] listBegin = "ls".getBytes(); 321 322 323 private static final byte[] listCurrentLevel = "ilvl".getBytes(); 324 325 326 private static final byte[] listTextOld = "listtext".getBytes(); 327 328 329 private static final byte[] tab = "tab".getBytes(); 330 331 332 private static final byte[] listBulletOld = "\'b7".getBytes(); 333 334 335 private static final byte[] listID = "listid".getBytes(); 336 337 338 private static final byte[] listOverride = "listoverride".getBytes(); 339 340 341 private static final byte[] listOverrideCount = "listoverridecount".getBytes(); 342 343 346 347 348 protected static final byte bold = (byte) 'b'; 349 350 351 protected static final byte italic = (byte) 'i'; 352 353 354 protected static final byte[] underline = "ul".getBytes(); 355 356 357 protected static final byte[] strikethrough = "strike".getBytes(); 358 359 360 public static final byte[] alignLeft = "ql".getBytes(); 361 362 363 public static final byte[] alignCenter = "qc".getBytes(); 364 365 366 public static final byte[] alignRight = "qr".getBytes(); 367 368 369 public static final byte[] alignJustify = "qj".getBytes(); 370 371 374 375 376 private static final byte[] colorTable = "colortbl".getBytes(); 377 378 379 private static final byte[] colorRed = "red".getBytes(); 380 381 382 private static final byte[] colorGreen = "green".getBytes(); 383 384 385 private static final byte[] colorBlue = "blue".getBytes(); 386 387 390 391 392 private static final byte[] infoBegin = "info".getBytes(); 393 394 395 private static final byte[] metaAuthor = "author".getBytes(); 396 397 398 private static final byte[] metaSubject = "subject".getBytes(); 399 400 401 private static final byte[] metaKeywords = "keywords".getBytes(); 402 403 404 private static final byte[] metaTitle = "title".getBytes(); 405 406 407 private static final byte[] metaProducer = "operator".getBytes(); 408 409 410 private static final byte[] metaCreationDate = "creationdate".getBytes(); 411 412 413 private static final byte[] year = "yr".getBytes(); 414 415 416 private static final byte[] month = "mo".getBytes(); 417 418 419 private static final byte[] day = "dy".getBytes(); 420 421 422 private static final byte[] hour = "hr".getBytes(); 423 424 425 private static final byte[] minute = "min".getBytes(); 426 427 428 private static final byte[] second = "sec".getBytes(); 429 430 431 private static final byte[] startSuper = "super".getBytes(); 432 433 434 private static final byte[] startSub = "sub".getBytes(); 435 436 437 private static final byte[] endSuperSub = "nosupersub".getBytes(); 438 439 442 443 444 private static final byte[] titlePage = "titlepg".getBytes(); 445 446 447 private static final byte[] facingPages = "facingp".getBytes(); 448 449 450 private static final byte[] headerBegin = "header".getBytes(); 451 452 453 private static final byte[] footerBegin = "footer".getBytes(); 454 455 private static final byte[] headerlBegin = "headerl".getBytes(); 457 458 private static final byte[] footerlBegin = "footerl".getBytes(); 459 460 private static final byte[] headerrBegin = "headerr".getBytes(); 461 462 private static final byte[] footerrBegin = "footerr".getBytes(); 463 464 private static final byte[] headerfBegin = "headerf".getBytes(); 465 466 private static final byte[] footerfBegin = "footerf".getBytes(); 467 468 471 472 473 private static final byte[] rtfPaperWidth = "paperw".getBytes(); 474 475 476 private static final byte[] rtfPaperHeight = "paperh".getBytes(); 477 478 479 private static final byte[] rtfMarginLeft = "margl".getBytes(); 480 481 482 private static final byte[] rtfMarginRight = "margr".getBytes(); 483 484 485 private static final byte[] rtfMarginTop = "margt".getBytes(); 486 487 488 private static final byte[] rtfMarginBottom = "margb".getBytes(); 489 490 491 private static final byte[] newPage = "page".getBytes(); 492 493 494 private static final byte[] landscapeTag1 = "landscape".getBytes(); 495 496 497 private static final byte[] landscapeTag2 = "lndscpsxn".getBytes(); 498 499 502 503 504 private static final byte[] annotationID = "atnid".getBytes(); 505 506 507 private static final byte[] annotationAuthor = "atnauthor".getBytes(); 508 509 510 private static final byte[] annotation = "annotation".getBytes(); 511 512 515 516 517 private static final byte[] pictureGroup = "shppict".getBytes(); 518 519 520 private static final byte[] picture = "pict".getBytes(); 521 522 523 private static final byte[] picturePNG = "pngblip".getBytes(); 524 525 526 private static final byte[] pictureJPEG = "jpegblip".getBytes(); 527 528 529 private static final byte[] pictureBMP = "dibitmap0".getBytes(); 530 531 532 private static final byte[] pictureWMF = "wmetafile8".getBytes(); 533 534 535 private static final byte[] pictureWidth = "picw".getBytes(); 536 537 538 private static final byte[] pictureHeight = "pich".getBytes(); 539 540 541 private static final byte[] pictureScaleX = "picscalex".getBytes(); 542 543 544 private static final byte[] pictureScaleY = "picscaley".getBytes(); 545 546 549 550 551 protected static final byte[] field = "field".getBytes(); 552 553 554 protected static final byte[] fieldContent = "fldinst".getBytes(); 555 556 557 protected static final byte[] fieldPage = "PAGE".getBytes(); 558 559 560 protected static final byte[] fieldHyperlink = "HYPERLINK".getBytes(); 561 562 563 protected static final byte[] fieldDisplay = "fldrslt".getBytes(); 564 565 566 567 568 572 573 574 private ArrayList fontList = new ArrayList (); 575 576 577 private ArrayList colorList = new ArrayList (); 578 579 580 private ByteArrayOutputStream content = null; 581 582 583 private ByteArrayOutputStream info = null; 584 585 586 private ByteArrayOutputStream listtable = null; 587 588 589 private ByteArrayOutputStream listoverride = null; 590 591 592 private HeaderFooter header = null; 593 594 595 private HeaderFooter footer = null; 596 597 598 private int marginLeft = 1800; 599 600 601 private int marginRight = 1800; 602 603 604 private int marginTop = 1440; 605 606 607 private int marginBottom = 1440; 608 609 610 private int pageWidth = 11906; 611 612 613 private int pageHeight = 16838; 614 615 616 public final static double TWIPSFACTOR = 20; 618 619 private int currentListID = 1; 620 621 622 private ArrayList listIds = null; 623 624 625 private int listLevel = 0; 626 627 628 private int maxListLevel = 0; 629 630 631 private boolean writeTOC = false; 632 633 634 private boolean hasTitlePage = false; 635 636 637 private boolean inHeaderFooter = false; 638 639 640 private boolean inTable = false; 641 642 643 private boolean landscape = false; 644 645 646 647 653 654 protected RtfWriter(Document doc, OutputStream os) { 655 super(doc, os); 656 document.addDocListener(this); 657 initDefaults(); 658 } 659 660 661 662 667 public void setGenerateTOCEntries(boolean writeTOC) { 668 this.writeTOC = writeTOC; 669 } 670 671 676 public boolean getGeneratingTOCEntries() { 677 return writeTOC; 678 } 679 680 685 public void setHasTitlePage(boolean hasTitlePage) { 686 this.hasTitlePage = hasTitlePage; 687 } 688 689 694 public boolean getHasTitlePage() { 695 return hasTitlePage; 696 } 697 698 704 public void setLandscape(boolean landscape) { 705 this.landscape = landscape; 706 } 707 708 713 public boolean getLandscape() { 714 return landscape; 715 } 716 717 718 719 726 public static RtfWriter getInstance(Document document, OutputStream os) { 727 return new RtfWriter(document, os); 728 } 729 730 734 public void open() { 735 super.open(); 736 } 737 738 745 public void close() { 746 if (open) { 747 writeDocument(); 748 super.close(); 749 } 750 } 751 752 756 public void setFooter(HeaderFooter footer) { 757 this.footer = footer; 758 processHeaderFooter(this.footer); 759 } 760 761 765 public void setHeader(HeaderFooter header) { 766 this.header = header; 767 processHeaderFooter(this.header); 768 } 769 770 773 public void resetFooter() { 774 setFooter(null); 775 } 776 777 780 public void resetHeader() { 781 setHeader(null); 782 } 783 784 790 public boolean newPage() { 791 try { 792 content.write(escape); 793 content.write(newPage); 794 content.write(escape); 795 content.write(paragraph); 796 } catch (IOException e) { 797 throw new ExceptionConverter(e); 798 } 799 return true; 800 } 801 802 812 public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) { 813 this.marginLeft = (int) (marginLeft * TWIPSFACTOR); 814 this.marginRight = (int) (marginRight * TWIPSFACTOR); 815 this.marginTop = (int) (marginTop * TWIPSFACTOR); 816 this.marginBottom = (int) (marginBottom * TWIPSFACTOR); 817 return true; 818 } 819 820 827 public boolean setPageSize(Rectangle pageSize) { 828 if (!parseFormat(pageSize, false)) { 829 pageWidth = (int) (pageSize.getWidth() * TWIPSFACTOR); 830 pageHeight = (int) (pageSize.getHeight() * TWIPSFACTOR); 831 landscape = pageWidth > pageHeight; 832 } 833 return true; 834 } 835 836 846 public boolean writeTOC(String tocTitle, Font titleFont, boolean showTOCasEntry, Font showTOCEntryFont) { 847 try { 848 RtfTOC toc = new RtfTOC(tocTitle, titleFont); 849 if (showTOCasEntry) { 850 toc.addTOCAsTOCEntry(tocTitle, showTOCEntryFont); 851 } 852 add(new Paragraph(toc)); 853 } catch (DocumentException de) { 854 return false; 855 } 856 return true; 857 } 858 859 866 public boolean add(Element element) throws DocumentException { 867 if (pause) { 868 return false; 869 } 870 return addElement(element, content); 871 } 872 873 874 875 876 883 protected boolean addElement(Element element, ByteArrayOutputStream out) throws DocumentException { 884 try { 885 switch (element.type()) { 886 case Element.CHUNK: 887 writeChunk((Chunk) element, out); 888 break; 889 case Element.PARAGRAPH: 890 writeParagraph((Paragraph) element, out); 891 break; 892 case Element.ANCHOR: 893 writeAnchor((Anchor) element, out); 894 break; 895 case Element.PHRASE: 896 writePhrase((Phrase) element, out); 897 break; 898 case Element.CHAPTER: 899 case Element.SECTION: 900 writeSection((Section) element, out); 901 break; 902 case Element.LIST: 903 writeList((com.lowagie.text.List) element, out); 904 break; 905 case Element.TABLE: 906 try { 907 writeTable((Table) element, out); 908 } 909 catch(ClassCastException cce) { 910 writeTable(((SimpleTable)element).createTable(), out); 911 } 912 break; 913 case Element.ANNOTATION: 914 writeAnnotation((Annotation) element, out); 915 break; 916 case Element.IMGRAW: 917 case Element.IMGTEMPLATE: 918 case Element.JPEG: 919 Image img = (Image)element; 920 writeImage(img, out); 921 break; 922 923 case Element.AUTHOR: 924 writeMeta(metaAuthor, (Meta) element); 925 break; 926  
|