File: RtfWriter.java

package info (click to toggle)
libitext1-java 1.4-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 7,636 kB
  • sloc: java: 86,865; xml: 396; makefile: 15
file content (2299 lines) | stat: -rw-r--r-- 80,956 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
/*
 * $Id: RtfWriter.java,v 1.70 2006/02/09 17:25:25 hallm Exp $
 * $Name:  $
 *
 * Copyright 2001, 2002 by Mark Hall
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */

package com.lowagie.text.rtf;

import com.lowagie.text.*;

import java.io.*;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.Iterator;
import java.util.Calendar;
import java.util.Date;
import java.awt.Color;
import java.text.SimpleDateFormat;
import java.text.ParsePosition;
import com.lowagie.text.pdf.codec.wmf.MetaDo;

/**
 * If you are creating a new project using the rtf part of iText, please
 * consider using the new RtfWriter2. The RtfWriter is in bug-fix-only mode,
 * will be deprecated end of 2005 and removed end of 2007.
 * 
 * A <CODE>DocWriter</CODE> class for Rich Text Files (RTF).
 * <P>
 * A <CODE>RtfWriter</CODE> can be added as a <CODE>DocListener</CODE>
 * to a certain <CODE>Document</CODE> by getting an instance.
 * Every <CODE>Element</CODE> added to the original <CODE>Document</CODE>
 * will be written to the <CODE>OutputStream</CODE> of this <CODE>RtfWriter</CODE>.
 * <P>
 * Example:
 * <BLOCKQUOTE><PRE>
 * // creation of the document with a certain size and certain margins
 * Document document = new Document(PageSize.A4, 50, 50, 50, 50);
 * try {
 *    // this will write RTF to the Standard OutputStream
 *    <STRONG>RtfWriter.getInstance(document, System.out);</STRONG>
 *    // this will write Rtf to a file called text.rtf
 *    <STRONG>RtfWriter.getInstance(document, new FileOutputStream("text.rtf"));</STRONG>
 *    // this will write Rtf to for instance the OutputStream of a HttpServletResponse-object
 *    <STRONG>RtfWriter.getInstance(document, response.getOutputStream());</STRONG>
 * }
 * catch(DocumentException de) {
 *    System.err.println(de.getMessage());
 * }
 * // this will close the document and all the OutputStreams listening to it
 * <STRONG>document.close();</CODE>
 * </PRE></BLOCKQUOTE>
 * <P>
 * <STRONG>LIMITATIONS</STRONG><BR>
 * There are currently still a few limitations on what the RTF Writer can do:
 * <ul>
 *    <li>Watermarks</li>
 *    <li>Viewer preferences</li>
 *    <li>Encryption</li>
 *    <li>Embedded fonts</li>
 *    <li>Phrases with a leading</li>
 *    <li>Lists with non-bullet symbols</li>
 *    <li>Nested tables</li>
 *    <li>Images other than JPEG and PNG</li>
 *    <li>Rotated images</li>
 * </ul>
 * <br />
 *
 * @author <a href="mailto:mhall@myrealbox.com">Mark.Hall@myrealbox.com</a>
 * @author Steffen Stundzig
 * @author <a href="ericmattes@yahoo.com">Eric Mattes</a>
 * @author <a href="raul.wegmann@uam.es">Raul Wegmann</a>
 * @deprecated The RtfWriter is deprecated and will be removed from the iText library end of 2007
 */
public class RtfWriter extends DocWriter implements DocListener {
    /**
     * Static Constants
     */

    /**
     * General
     */

    /** This is the escape character which introduces RTF tags. */
    public static final byte escape = (byte) '\\';

    /** This is another escape character which introduces RTF tags. */
    private static final byte[] extendedEscape = "\\*\\".getBytes();

    /** This is the delimiter between RTF tags and normal text. */
    protected static final byte delimiter = (byte) ' ';

    /** This is another delimiter between RTF tags and normal text. */
    private static final byte commaDelimiter = (byte) ';';

    /** This is the character for beginning a new group. */
    public static final byte openGroup = (byte) '{';

    /** This is the character for closing a group. */
    public static final byte closeGroup = (byte) '}';

    /**
     * RTF Information
     */

    /** RTF begin and version. */
    private static final byte[] docBegin = "rtf1".getBytes();

    /** RTF encoding. */
    private static final byte[] ansi = "ansi".getBytes();

    /** RTF encoding codepage. */
    private static final byte[] ansiCodepage = "ansicpg".getBytes();

    /**
     *Font Data
     */

    /** Begin the font table tag. */
    private static final byte[] fontTable = "fonttbl".getBytes();

    /** Font number tag. */
    protected static final byte fontNumber = (byte) 'f';

    /** Font size tag. */
    protected static final byte[] fontSize = "fs".getBytes();

    /** Font color tag. */
    protected static final byte[] fontColor = "cf".getBytes();

    /** Modern font tag. */
    private static final byte[] fontModern = "fmodern".getBytes();

    /** Swiss font tag. */
    private static final byte[] fontSwiss = "fswiss".getBytes();

    /** Roman font tag. */
    private static final byte[] fontRoman = "froman".getBytes();

    /** Tech font tag. */
    private static final byte[] fontTech = "ftech".getBytes();

    /** Font charset tag. */
    private static final byte[] fontCharset = "fcharset".getBytes();

    /** Font Courier tag. */
    private static final byte[] fontCourier = "Courier".getBytes();

    /** Font Arial tag. */
    private static final byte[] fontArial = "Arial".getBytes();

    /** Font Symbol tag. */
    private static final byte[] fontSymbol = "Symbol".getBytes();

    /** Font Times New Roman tag. */
    private static final byte[] fontTimesNewRoman = "Times New Roman".getBytes();

    /** Font Windings tag. */
    private static final byte[] fontWindings = "Windings".getBytes();

    /** Default Font. */
    private static final byte[] defaultFont = "deff".getBytes();

    /** First indent tag. */
    private static final byte[] firstIndent = "fi".getBytes();

    /** Left indent tag. */
    private static final byte[] listIndent = "li".getBytes();

    /** Right indent tag. */
    private static final byte[] rightIndent = "ri".getBytes();

    /**
     * Sections / Paragraphs
     */

    /** Reset section defaults tag. */
    private static final byte[] sectionDefaults = "sectd".getBytes();

    /** Begin new section tag. */
    private static final byte[] section = "sect".getBytes();

    /** Reset paragraph defaults tag. */
    public static final byte[] paragraphDefaults = "pard".getBytes();

    /** Begin new paragraph tag. */
    public static final byte[] paragraph = "par".getBytes();

	/** Page width of a section. */
	public static final byte[] sectionPageWidth = "pgwsxn".getBytes();

	/** Page height of a section. */
	public static final byte[] sectionPageHeight = "pghsxn".getBytes();

    /**
     * Lists
     */

    /** Begin the List Table */
    private static final byte[] listtableGroup = "listtable".getBytes();

    /** Begin the List Override Table */
    private static final byte[] listoverridetableGroup = "listoverridetable".getBytes();

    /** Begin a List definition */
    private static final byte[] listDefinition = "list".getBytes();

    /** List Template ID */
    private static final byte[] listTemplateID = "listtemplateid".getBytes();

    /** RTF Writer outputs hybrid lists */
    private static final byte[] hybridList = "hybrid".getBytes();

    /** Current List level */
    private static final byte[] listLevelDefinition = "listlevel".getBytes();

    /** Level numbering (old) */
    private static final byte[] listLevelTypeOld = "levelnfc".getBytes();

    /** Level numbering (new) */
    private static final byte[] listLevelTypeNew = "levelnfcn".getBytes();

    /** Level alignment (old) */
    private static final byte[] listLevelAlignOld = "leveljc".getBytes();

    /** Level alignment (new) */
    private static final byte[] listLevelAlignNew = "leveljcn".getBytes();

    /** Level starting number */
    private static final byte[] listLevelStartAt = "levelstartat".getBytes();

    /** Level text group */
    private static final byte[] listLevelTextDefinition = "leveltext".getBytes();

    /** Filler for Level Text Length */
    private static final byte[] listLevelTextLength = "\'0".getBytes();

    /** Level Text Numbering Style */
    private static final byte[] listLevelTextStyleNumbers = "\'00.".getBytes();

    /** Level Text Bullet Style */
    private static final byte[] listLevelTextStyleBullet = "u-3913 ?".getBytes();

    /** Level Numbers Definition */
    private static final byte[] listLevelNumbersDefinition = "levelnumbers".getBytes();

    /** Filler for Level Numbers */
    private static final byte[] listLevelNumbers = "\\'0".getBytes();

    /** Tab Stop */
    private static final byte[] tabStop = "tx".getBytes();

    /** Actual list begin */
    private static final byte[] listBegin = "ls".getBytes();

    /** Current list level */
    private static final byte[] listCurrentLevel = "ilvl".getBytes();

    /** List text group for older browsers */
    private static final byte[] listTextOld = "listtext".getBytes();

    /** Tab */
    private static final byte[] tab = "tab".getBytes();

    /** Old Bullet Style */
    private static final byte[] listBulletOld = "\'b7".getBytes();

    /** Current List ID */
    private static final byte[] listID = "listid".getBytes();

    /** List override */
    private static final byte[] listOverride = "listoverride".getBytes();

    /** Number of overrides */
    private static final byte[] listOverrideCount = "listoverridecount".getBytes();

    /**
     * Text Style
     */

    /** Bold tag. */
    protected static final byte bold = (byte) 'b';

    /** Italic tag. */
    protected static final byte italic = (byte) 'i';

    /** Underline tag. */
    protected static final byte[] underline = "ul".getBytes();

    /** Strikethrough tag. */
    protected static final byte[] strikethrough = "strike".getBytes();

    /** Text alignment left tag. */
    public static final byte[] alignLeft = "ql".getBytes();

    /** Text alignment center tag. */
    public static final byte[] alignCenter = "qc".getBytes();

    /** Text alignment right tag. */
    public static final byte[] alignRight = "qr".getBytes();

    /** Text alignment justify tag. */
    public static final byte[] alignJustify = "qj".getBytes();

    /**
     * Colors
     */

    /** Begin colour table tag. */
    private static final byte[] colorTable = "colortbl".getBytes();

    /** Red value tag. */
    private static final byte[] colorRed = "red".getBytes();

    /** Green value tag. */
    private static final byte[] colorGreen = "green".getBytes();

    /** Blue value tag. */
    private static final byte[] colorBlue = "blue".getBytes();

    /**
     * Information Group
     */

    /** Begin the info group tag.*/
    private static final byte[] infoBegin = "info".getBytes();

    /** Author tag. */
    private static final byte[] metaAuthor = "author".getBytes();

    /** Subject tag. */
    private static final byte[] metaSubject = "subject".getBytes();

    /** Keywords tag. */
    private static final byte[] metaKeywords = "keywords".getBytes();

    /** Title tag. */
    private static final byte[] metaTitle = "title".getBytes();

    /** Producer tag. */
    private static final byte[] metaProducer = "operator".getBytes();

    /** Creation Date tag. */
    private static final byte[] metaCreationDate = "creationdate".getBytes();

    /** Year tag. */
    private static final byte[] year = "yr".getBytes();

    /** Month tag. */
    private static final byte[] month = "mo".getBytes();

    /** Day tag. */
    private static final byte[] day = "dy".getBytes();

    /** Hour tag. */
    private static final byte[] hour = "hr".getBytes();

    /** Minute tag. */
    private static final byte[] minute = "min".getBytes();

    /** Second tag. */
    private static final byte[] second = "sec".getBytes();

    /** Start superscript. */
    private static final byte[] startSuper = "super".getBytes();

    /** Start subscript. */
    private static final byte[] startSub = "sub".getBytes();

    /** End super/sub script. */
    private static final byte[] endSuperSub = "nosupersub".getBytes();

    /**
     * Header / Footer
     */

    /** Title Page tag */
    private static final byte[] titlePage = "titlepg".getBytes();

    /** Facing pages tag */
    private static final byte[] facingPages = "facingp".getBytes();

    /** Begin header group tag. */
    private static final byte[] headerBegin = "header".getBytes();

    /** Begin footer group tag. */
    private static final byte[] footerBegin = "footer".getBytes();

    // header footer 'left', 'right', 'first'
    private static final byte[] headerlBegin = "headerl".getBytes();

    private static final byte[] footerlBegin = "footerl".getBytes();

    private static final byte[] headerrBegin = "headerr".getBytes();

    private static final byte[] footerrBegin = "footerr".getBytes();

    private static final byte[] headerfBegin = "headerf".getBytes();

    private static final byte[] footerfBegin = "footerf".getBytes();

    /**
     * Paper Properties
     */

    /** Paper width tag. */
    private static final byte[] rtfPaperWidth = "paperw".getBytes();

    /** Paper height tag. */
    private static final byte[] rtfPaperHeight = "paperh".getBytes();

    /** Margin left tag. */
    private static final byte[] rtfMarginLeft = "margl".getBytes();

    /** Margin right tag. */
    private static final byte[] rtfMarginRight = "margr".getBytes();

    /** Margin top tag. */
    private static final byte[] rtfMarginTop = "margt".getBytes();

    /** Margin bottom tag. */
    private static final byte[] rtfMarginBottom = "margb".getBytes();

    /** New Page tag. */
    private static final byte[] newPage = "page".getBytes();

    /** Document Landscape tag 1. */
    private static final byte[] landscapeTag1 = "landscape".getBytes();

    /** Document Landscape tag 2. */
    private static final byte[] landscapeTag2 = "lndscpsxn".getBytes();

    /**
     * Annotations
     */

    /** Annotation ID tag. */
    private static final byte[] annotationID = "atnid".getBytes();

    /** Annotation Author tag. */
    private static final byte[] annotationAuthor = "atnauthor".getBytes();

    /** Annotation text tag. */
    private static final byte[] annotation = "annotation".getBytes();

    /**
     * Images
     */

    /** Begin the main Picture group tag */
    private static final byte[] pictureGroup = "shppict".getBytes();

    /** Begin the picture tag */
    private static final byte[] picture = "pict".getBytes();

    /** PNG Image */
    private static final byte[] picturePNG = "pngblip".getBytes();

    /** JPEG Image */
    private static final byte[] pictureJPEG = "jpegblip".getBytes();

    /** BMP Image */
    private static final byte[] pictureBMP = "dibitmap0".getBytes();

    /** WMF Image */
    private static final byte[] pictureWMF = "wmetafile8".getBytes();

    /** Picture width */
    private static final byte[] pictureWidth = "picw".getBytes();

    /** Picture height */
    private static final byte[] pictureHeight = "pich".getBytes();

    /** Picture scale horizontal percent */
    private static final byte[] pictureScaleX = "picscalex".getBytes();

    /** Picture scale vertical percent */
    private static final byte[] pictureScaleY = "picscaley".getBytes();

    /**
     * Fields (for page numbering)
     */

    /** Begin field tag */
    protected static final byte[] field = "field".getBytes();

    /** Content fo the field */
    protected static final byte[] fieldContent = "fldinst".getBytes();

    /** PAGE numbers */
    protected static final byte[] fieldPage = "PAGE".getBytes();

    /** HYPERLINK field */
    protected static final byte[] fieldHyperlink = "HYPERLINK".getBytes();

    /** Last page number (not used) */
    protected static final byte[] fieldDisplay = "fldrslt".getBytes();


    /** Class variables */

    /**
     * Because of the way RTF works and the way itext works, the text has to be
     * stored and is only written to the actual OutputStream at the end.
     */

    /** This <code>ArrayList</code> contains all fonts used in the document. */
    private ArrayList fontList = new ArrayList();

    /** This <code>ArrayList</code> contains all colours used in the document. */
    private ArrayList colorList = new ArrayList();

    /** This <code>ByteArrayOutputStream</code> contains the main body of the document. */
    private ByteArrayOutputStream content = null;

    /** This <code>ByteArrayOutputStream</code> contains the information group. */
    private ByteArrayOutputStream info = null;

    /** This <code>ByteArrayOutputStream</code> contains the list table. */
    private ByteArrayOutputStream listtable = null;

    /** This <code>ByteArrayOutputStream</code> contains the list override table. */
    private ByteArrayOutputStream listoverride = null;

    /** Document header. */
    private HeaderFooter header = null;

    /** Document footer. */
    private HeaderFooter footer = null;

    /** Left margin. */
    private int marginLeft = 1800;

    /** Right margin. */
    private int marginRight = 1800;

    /** Top margin. */
    private int marginTop = 1440;

    /** Bottom margin. */
    private int marginBottom = 1440;

    /** Page width. */
    private int pageWidth = 11906;

    /** Page height. */
    private int pageHeight = 16838;

    /** Factor to use when converting. */
    public final static double TWIPSFACTOR = 20;//20.57140;

    /** Current list ID. */
    private int currentListID = 1;

    /** List of current Lists. */
    private ArrayList listIds = null;

    /** Current List Level. */
    private int listLevel = 0;

    /** Current maximum List Level. */
    private int maxListLevel = 0;

    /** Write a TOC */
    private boolean writeTOC = false;

    /** Special title page */
    private boolean hasTitlePage = false;

    /** Currently writing either Header or Footer */
    private boolean inHeaderFooter = false;

    /** Currently writing a Table */
    private boolean inTable = false;

    /** Landscape or Portrait Document */
    private boolean landscape = false;

    /** Protected Constructor */

    /**
     * Constructs a <CODE>RtfWriter</CODE>.
     *
     * @param doc         The <CODE>Document</CODE> that is to be written as RTF
     * @param os          The <CODE>OutputStream</CODE> the writer has to write to.
     */

    protected RtfWriter(Document doc, OutputStream os) {
        super(doc, os);
        document.addDocListener(this);
        initDefaults();
    }

    /** Public functions special to the RtfWriter */

    /**
     * This method controls whether TOC entries are automatically generated
     *
     * @param writeTOC    boolean value indicating whether a TOC is to be generated
     */
    public void setGenerateTOCEntries(boolean writeTOC) {
        this.writeTOC = writeTOC;
    }

    /**
     * Gets the current setting of writeTOC
     *
     * @return    boolean value indicating whether a TOC is being generated
     */
    public boolean getGeneratingTOCEntries() {
        return writeTOC;
    }

    /**
     * This method controls whether the first page is a title page
     *
     * @param hasTitlePage    boolean value indicating whether the first page is a title page
     */
    public void setHasTitlePage(boolean hasTitlePage) {
        this.hasTitlePage = hasTitlePage;
    }

    /**
     * Gets the current setting of hasTitlePage
     *
     * @return    boolean value indicating whether the first page is a title page
     */
    public boolean getHasTitlePage() {
        return hasTitlePage;
    }

    /**
     * Explicitly sets the page format to use.
     * Otherwise the RtfWriter will try to guess the format by comparing pagewidth and pageheight
     *
     * @param landscape boolean value indicating whether we are using landscape format or not
     */
    public void setLandscape(boolean landscape) {
        this.landscape = landscape;
    }

    /**
     * Returns the current landscape setting
     *
     * @return boolean value indicating the current page format
     */
    public boolean getLandscape() {
        return landscape;
    }

    /** Public functions from the DocWriter Interface */

    /**
     * Gets an instance of the <CODE>RtfWriter</CODE>.
     *
     * @param document    The <CODE>Document</CODE> that has to be written
     * @param os  The <CODE>OutputStream</CODE> the writer has to write to.
     * @return    a new <CODE>RtfWriter</CODE>
     */
    public static RtfWriter getInstance(Document document, OutputStream os) {
        return (new RtfWriter(document, os));
    }

    /**
     * Signals that the <CODE>Document</CODE> has been opened and that
     * <CODE>Elements</CODE> can be added.
     */
    public void open() {
        super.open();
    }

    /**
     * Signals that the <CODE>Document</CODE> was closed and that no other
     * <CODE>Elements</CODE> will be added.
     * <p>
     * The content of the font table, color table, information group, content, header, footer are merged into the final
     * <code>OutputStream</code>
     */
    public void close() {
        writeDocument();
        super.close();
    }

    /**
     * Adds the footer to the bottom of the <CODE>Document</CODE>.
     * @param footer
     */
    public void setFooter(HeaderFooter footer) {
        this.footer = footer;
        processHeaderFooter(this.footer);
    }

    /**
     * Adds the header to the top of the <CODE>Document</CODE>.
     * @param header
     */
    public void setHeader(HeaderFooter header) {
        this.header = header;
        processHeaderFooter(this.header);
    }

    /**
     * Resets the footer.
     */
    public void resetFooter() {
        setFooter(null);
    }

    /**
     * Resets the header.
     */
    public void resetHeader() {
        setHeader(null);
    }

    /**
     * Tells the <code>RtfWriter</code> that a new page is to be begun.
     *
     * @return <code>true</code> if a new Page was begun.
     * @throws DocumentException if the Document was not open or had been closed.
     */
    public boolean newPage() throws DocumentException {
        try {
            content.write(escape);
            content.write(newPage);
            content.write(escape);
            content.write(paragraph);
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * Sets the page margins
     *
     * @param marginLeft The left margin
     * @param marginRight The right margin
     * @param marginTop The top margin
     * @param marginBottom The bottom margin
     *
     * @return <code>true</code> if the page margins were set.
     */
    public boolean setMargins(float marginLeft, float marginRight, float marginTop, float marginBottom) {
        this.marginLeft = (int) (marginLeft * TWIPSFACTOR);
        this.marginRight = (int) (marginRight * TWIPSFACTOR);
        this.marginTop = (int) (marginTop * TWIPSFACTOR);
        this.marginBottom = (int) (marginBottom * TWIPSFACTOR);
        return true;
    }

    /**
     * Sets the page size
     *
     * @param pageSize A <code>Rectangle</code> specifying the page size
     *
     * @return <code>true</code> if the page size was set
     */
    public boolean setPageSize(Rectangle pageSize) {
        if (!parseFormat(pageSize, false)) {
            pageWidth = (int) (pageSize.width() * TWIPSFACTOR);
            pageHeight = (int) (pageSize.height() * TWIPSFACTOR);
            landscape = pageWidth > pageHeight;
        }
        return true;
    }

    /**
     * Write the table of contents.
     *
     * @param tocTitle The title that will be displayed above the TOC
     * @param titleFont The <code>Font</code> that will be used for the tocTitle
     * @param showTOCasEntry Set this to true if you want the TOC to appear as an entry in the TOC
     * @param showTOCEntryFont Use this <code>Font</code> to specify what Font to use when showTOCasEntry is true
     *
     * @return <code>true</code> if the TOC was added.
     */
    public boolean writeTOC(String tocTitle, Font titleFont, boolean showTOCasEntry, Font showTOCEntryFont) {
        try {
            RtfTOC toc = new RtfTOC(tocTitle, titleFont);
            if (showTOCasEntry) {
                toc.addTOCAsTOCEntry(tocTitle, showTOCEntryFont);
            }
            add(new Paragraph(toc));
        } catch (DocumentException de) {
            return false;
        }
        return true;
    }

    /**
     * Signals that an <CODE>Element</CODE> was added to the <CODE>Document</CODE>.
     * 
     * @param element A high level object to add
     * @return    <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
     * @throws    DocumentException   if a document isn't open yet, or has been closed
     */
    public boolean add(Element element) throws DocumentException {
        if (pause) {
            return false;
        }
        return addElement(element, content);
    }


    /** Private functions */

    /**
     * Adds an <CODE>Element</CODE> to the <CODE>Document</CODE>.
     * @param element the high level element to add
     * @param out the outputstream to which the RTF data is sent
     * @return    <CODE>true</CODE> if the element was added, <CODE>false</CODE> if not.
     * @throws    DocumentException   if a document isn't open yet, or has been closed
     */
    protected boolean addElement(Element element, ByteArrayOutputStream out) throws DocumentException {
        try {
            switch (element.type()) {
                case Element.CHUNK:
                    writeChunk((Chunk) element, out);
                    break;
                case Element.PARAGRAPH:
                    writeParagraph((Paragraph) element, out);
                    break;
                case Element.ANCHOR:
                    writeAnchor((Anchor) element, out);
                    break;
                case Element.PHRASE:
                    writePhrase((Phrase) element, out);
                    break;
                case Element.CHAPTER:
                case Element.SECTION:
                    writeSection((Section) element, out);
                    break;
                case Element.LIST:
                    writeList((com.lowagie.text.List) element, out);
                    break;
                case Element.TABLE:
                	try {
                		writeTable((Table) element, out);
                	}
                	catch(ClassCastException cce) {
                		writeTable(((SimpleTable)element).createTable(), out);
                	}
                    break;
                case Element.ANNOTATION:
                    writeAnnotation((Annotation) element, out);
                    break;
                case Element.IMGRAW:
                case Element.IMGTEMPLATE:
                case Element.JPEG:
                    Image img = (Image)element;
                    writeImage(img, out);
                    break;

                case Element.AUTHOR:
                    writeMeta(metaAuthor, (Meta) element);
                    break;
                case Element.SUBJECT:
                    writeMeta(metaSubject, (Meta) element);
                    break;
                case Element.KEYWORDS:
                    writeMeta(metaKeywords, (Meta) element);
                    break;
                case Element.TITLE:
                    writeMeta(metaTitle, (Meta) element);
                    break;
                case Element.PRODUCER:
                    writeMeta(metaProducer, (Meta) element);
                    break;
                case Element.CREATIONDATE:
                    writeMeta(metaCreationDate, (Meta) element);
                    break;
            }
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    /**
     * Write the beginning of a new <code>Section</code>
     *
     * @param sectionElement The <code>Section</code> be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeSection(Section sectionElement, ByteArrayOutputStream out) throws IOException, DocumentException {
        if (sectionElement.type() == Element.CHAPTER) {
            out.write(escape);
            out.write(sectionDefaults);
            writeSectionDefaults(out);
        }
        if (sectionElement.title() != null) {
            if (writeTOC) {
                StringBuffer title = new StringBuffer("");
                for (ListIterator li = sectionElement.title().getChunks().listIterator(); li.hasNext();) {
                    title.append(((Chunk) li.next()).content());
                }
                add(new RtfTOCEntry(title.toString(), sectionElement.title().font()));
            } else {
                add(sectionElement.title());
            }
            out.write(escape);
            out.write(paragraph);
        }
        sectionElement.process(this);
        if (sectionElement.type() == Element.CHAPTER) {
            out.write(escape);
            out.write(section);
        }
        if (sectionElement.type() == Element.SECTION) {
            out.write(escape);
            out.write(paragraph);
        }
    }

    /**
     * Write the beginning of a new <code>Paragraph</code>
     *
     * @param paragraphElement The <code>Paragraph</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writeParagraph(Paragraph paragraphElement, ByteArrayOutputStream out) throws IOException {
        out.write(escape);
        out.write(paragraphDefaults);
        if (inTable) {
            out.write(escape);
            out.write(RtfCell.cellInTable);
        }
        switch (paragraphElement.alignment()) {
            case Element.ALIGN_LEFT:
                out.write(escape);
                out.write(alignLeft);
                break;
            case Element.ALIGN_RIGHT:
                out.write(escape);
                out.write(alignRight);
                break;
            case Element.ALIGN_CENTER:
                out.write(escape);
                out.write(alignCenter);
                break;
            case Element.ALIGN_JUSTIFIED:
            case Element.ALIGN_JUSTIFIED_ALL:
                out.write(escape);
                out.write(alignJustify);
                break;
        }
        out.write(escape);
        out.write(listIndent);
        writeInt(out, (int) (paragraphElement.indentationLeft() * TWIPSFACTOR));
        out.write(escape);
        out.write(rightIndent);
        writeInt(out, (int) (paragraphElement.indentationRight() * TWIPSFACTOR));
        Iterator chunks = paragraphElement.getChunks().iterator();
        while (chunks.hasNext()) {
            Chunk ch = (Chunk) chunks.next();
            ch.setFont(paragraphElement.font().difference(ch.font()));
        }
        ByteArrayOutputStream save = content;
        content = out;
        paragraphElement.process(this);
        content = save;
        if (!inTable) {
            out.write(escape);
            out.write(paragraph);
        }
    }

    /**
     * Write a <code>Phrase</code>.
     *
     * @param phrase  The <code>Phrase</code> item to be written
     * @param out     The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writePhrase(Phrase phrase, ByteArrayOutputStream out) throws IOException {
        out.write(escape);
        out.write(paragraphDefaults);
        if (inTable) {
            out.write(escape);
            out.write(RtfCell.cellInTable);
        }
        Iterator chunks = phrase.getChunks().iterator();
        while (chunks.hasNext()) {
            Chunk ch = (Chunk) chunks.next();
            ch.setFont(phrase.font().difference(ch.font()));
        }
        ByteArrayOutputStream save = content;
        content = out;
        phrase.process(this);
        content = save;
    }

    /**
     * Write an <code>Anchor</code>. Anchors are treated like Phrases.
     *
     * @param anchor  The <code>Chunk</code> item to be written
     * @param out     The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writeAnchor(Anchor anchor, ByteArrayOutputStream out) throws IOException {
        if (anchor.url() != null) {
            out.write(openGroup);
            out.write(escape);
            out.write(field);
            out.write(openGroup);
            out.write(extendedEscape);
            out.write(fieldContent);
            out.write(openGroup);
            out.write(fieldHyperlink);
            out.write(delimiter);
            out.write(anchor.url().toString().getBytes());
            out.write(closeGroup);
            out.write(closeGroup);
            out.write(openGroup);
            out.write(escape);
            out.write(fieldDisplay);
            out.write(delimiter);
            writePhrase(anchor, out);
            out.write(closeGroup);
            out.write(closeGroup);
        } else {
            writePhrase(anchor, out);
        }
    }

    /**
     * Write a <code>Chunk</code> and all its font properties.
     *
     * @param chunk The <code>Chunk</code> item to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeChunk(Chunk chunk, ByteArrayOutputStream out) throws IOException, DocumentException {
        if (chunk instanceof RtfField) {
            ((RtfField) chunk).write(this, out);
        } else {
            if (chunk.getImage() != null) {
                writeImage(chunk.getImage(), out);
            } else {
                writeInitialFontSignature(out, chunk);
                out.write(filterSpecialChar(chunk.content(), false).getBytes());
                writeFinishingFontSignature(out, chunk);
            }
        }
    }


    protected void writeInitialFontSignature(OutputStream out, Chunk chunk) throws IOException {
        Font font = chunk.font();

        out.write(escape);
        out.write(fontNumber);
        if (!font.getFamilyname().equalsIgnoreCase("unknown")) {
            writeInt(out, addFont(font));
        } else {
            writeInt(out, 0);
        }
        out.write(escape);
        out.write(fontSize);
        if (font.size() > 0) {
            writeInt(out, (int) (font.size() * 2));
        } else {
            writeInt(out, 20);
        }
        out.write(escape);
        out.write(fontColor);
        writeInt(out, addColor(font.color()));
        if (font.isBold()) {
            out.write(escape);
            out.write(bold);
        }
        if (font.isItalic()) {
            out.write(escape);
            out.write(italic);
        }
        if (font.isUnderlined()) {
            out.write(escape);
            out.write(underline);
        }
        if (font.isStrikethru()) {
            out.write(escape);
            out.write(strikethrough);
        }

        /*
         * Superscript / Subscript added by Scott Dietrich (sdietrich@emlab.com)
         */
        if (chunk.getAttributes() != null) {
            Float f = (Float) chunk.getAttributes().get(Chunk.SUBSUPSCRIPT);
            if (f != null)
                if (f.floatValue() > 0) {
                    out.write(escape);
                    out.write(startSuper);
                } else if (f.floatValue() < 0) {
                    out.write(escape);
                    out.write(startSub);
                }
        }

        out.write(delimiter);
    }


    protected void writeFinishingFontSignature(OutputStream out, Chunk chunk) throws IOException {
        Font font = chunk.font();

        if (font.isBold()) {
            out.write(escape);
            out.write(bold);
            writeInt(out, 0);
        }
        if (font.isItalic()) {
            out.write(escape);
            out.write(italic);
            writeInt(out, 0);
        }
        if (font.isUnderlined()) {
            out.write(escape);
            out.write(underline);
            writeInt(out, 0);
        }
        if (font.isStrikethru()) {
            out.write(escape);
            out.write(strikethrough);
            writeInt(out, 0);
        }

        /*
         * Superscript / Subscript added by Scott Dietrich (sdietrich@emlab.com)
         */
        if (chunk.getAttributes() != null) {
            Float f = (Float) chunk.getAttributes().get(Chunk.SUBSUPSCRIPT);
            if (f != null)
                if (f.floatValue() != 0) {
                    out.write(escape);
                    out.write(endSuperSub);
                }
        }
    }

    /**
     * Write a <code>ListItem</code>
     *
     * @param listItem The <code>ListItem</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeListElement(ListItem listItem, ByteArrayOutputStream out) throws IOException, DocumentException {
        Iterator chunks = listItem.getChunks().iterator();
        while (chunks.hasNext()) {
            Chunk ch = (Chunk) chunks.next();
            addElement(ch, out);
        }
        out.write(escape);
        out.write(paragraph);
    }

    /**
     * Write a <code>List</code>
     *
     * @param list The <code>List</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws    IOException
     * @throws    DocumentException
     */
    private void writeList(com.lowagie.text.List list, ByteArrayOutputStream out) throws IOException, DocumentException {
        int type = 0;
        int align = 0;
        int fontNr = addFont(new Font(Font.SYMBOL, 10, Font.NORMAL, new Color(0, 0, 0)));
        if (!list.isNumbered()) type = 23;
        if (listLevel == 0) {
            maxListLevel = 0;
            listtable.write(openGroup);
            listtable.write(escape);
            listtable.write(listDefinition);
            int i = getRandomInt();
            listtable.write(escape);
            listtable.write(listTemplateID);
            writeInt(listtable, i);
            listtable.write(escape);
            listtable.write(hybridList);
            listtable.write((byte) '\n');
        }
        if (listLevel >= maxListLevel) {
            maxListLevel++;
            listtable.write(openGroup);
            listtable.write(escape);
            listtable.write(listLevelDefinition);
            listtable.write(escape);
            listtable.write(listLevelTypeOld);
            writeInt(listtable, type);
            listtable.write(escape);
            listtable.write(listLevelTypeNew);
            writeInt(listtable, type);
            listtable.write(escape);
            listtable.write(listLevelAlignOld);
            writeInt(listtable, align);
            listtable.write(escape);
            listtable.write(listLevelAlignNew);
            writeInt(listtable, align);
            listtable.write(escape);
            listtable.write(listLevelStartAt);
            writeInt(listtable, 1);
            listtable.write(openGroup);
            listtable.write(escape);
            listtable.write(listLevelTextDefinition);
            listtable.write(escape);
            listtable.write(listLevelTextLength);
            if (list.isNumbered()) {
                writeInt(listtable, 2);
            } else {
                writeInt(listtable, 1);
            }
            listtable.write(escape);
            if (list.isNumbered()) {
                listtable.write(listLevelTextStyleNumbers);
            } else {
                listtable.write(listLevelTextStyleBullet);
            }
            listtable.write(commaDelimiter);
            listtable.write(closeGroup);
            listtable.write(openGroup);
            listtable.write(escape);
            listtable.write(listLevelNumbersDefinition);
            if (list.isNumbered()) {
                listtable.write(delimiter);
                listtable.write(listLevelNumbers);
                writeInt(listtable, listLevel + 1);
            }
            listtable.write(commaDelimiter);
            listtable.write(closeGroup);
            if (!list.isNumbered()) {
                listtable.write(escape);
                listtable.write(fontNumber);
                writeInt(listtable, fontNr);
            }
            listtable.write(escape);
            listtable.write(firstIndent);
            writeInt(listtable, (int) (list.indentationLeft() * TWIPSFACTOR * -1));
            listtable.write(escape);
            listtable.write(listIndent);
            writeInt(listtable, (int) ((list.indentationLeft() + list.symbolIndent()) * TWIPSFACTOR));
            listtable.write(escape);
            listtable.write(rightIndent);
            writeInt(listtable, (int) (list.indentationRight() * TWIPSFACTOR));
            listtable.write(escape);
            listtable.write(tabStop);
            writeInt(listtable, (int) (list.symbolIndent() * TWIPSFACTOR));
            listtable.write(closeGroup);
            listtable.write((byte) '\n');
        }
        // Actual List Begin in Content
        out.write(escape);
        out.write(paragraphDefaults);
        out.write(escape);
        out.write(alignLeft);
        out.write(escape);
        out.write(firstIndent);
        writeInt(out, (int) (list.indentationLeft() * TWIPSFACTOR * -1));
        out.write(escape);
        out.write(listIndent);
        writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * TWIPSFACTOR));
        out.write(escape);
        out.write(rightIndent);
        writeInt(out, (int) (list.indentationRight() * TWIPSFACTOR));
        out.write(escape);
        out.write(fontSize);
        writeInt(out, 20);
        out.write(escape);
        out.write(listBegin);
        writeInt(out, currentListID);
        if (listLevel > 0) {
            out.write(escape);
            out.write(listCurrentLevel);
            writeInt(out, listLevel);
        }
        out.write(openGroup);
        ListIterator listItems = list.getItems().listIterator();
        Element listElem;
        int count = 1;
        while (listItems.hasNext()) {
            listElem = (Element) listItems.next();
            if (listElem.type() == Element.CHUNK) {
                listElem = new ListItem((Chunk) listElem);
            }
            if (listElem.type() == Element.LISTITEM) {
                out.write(openGroup);
                out.write(escape);
                out.write(listTextOld);
                out.write(escape);
                out.write(paragraphDefaults);
                out.write(escape);
                out.write(fontNumber);
                if (list.isNumbered()) {
                    writeInt(out, addFont(new Font(Font.TIMES_ROMAN, Font.NORMAL, 10, new Color(0, 0, 0))));
                } else {
                    writeInt(out, fontNr);
                }
                out.write(escape);
                out.write(firstIndent);
                writeInt(out, (int) (list.indentationLeft() * TWIPSFACTOR * -1));
                out.write(escape);
                out.write(listIndent);
                writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * TWIPSFACTOR));
                out.write(escape);
                out.write(rightIndent);
                writeInt(out, (int) (list.indentationRight() * TWIPSFACTOR));
                out.write(delimiter);
                if (list.isNumbered()) {
                    writeInt(out, count);
                    out.write(".".getBytes());
                } else {
                    out.write(escape);
                    out.write(listBulletOld);
                }
                out.write(escape);
                out.write(tab);
                out.write(closeGroup);
                writeListElement((ListItem) listElem, out);
                count++;
            } else if (listElem.type() == Element.LIST) {
                listLevel++;
                writeList((com.lowagie.text.List) listElem, out);
                listLevel--;
                out.write(escape);
                out.write(paragraphDefaults);
                out.write(escape);
                out.write(alignLeft);
                out.write(escape);
                out.write(firstIndent);
                writeInt(out, (int) (list.indentationLeft() * TWIPSFACTOR * -1));
                out.write(escape);
                out.write(listIndent);
                writeInt(out, (int) ((list.indentationLeft() + list.symbolIndent()) * TWIPSFACTOR));
                out.write(escape);
                out.write(rightIndent);
                writeInt(out, (int) (list.indentationRight() * TWIPSFACTOR));
                out.write(escape);
                out.write(fontSize);
                writeInt(out, 20);
                out.write(escape);
                out.write(listBegin);
                writeInt(out, currentListID);
                if (listLevel > 0) {
                    out.write(escape);
                    out.write(listCurrentLevel);
                    writeInt(out, listLevel);
                }
            }
            out.write((byte) '\n');
        }
        out.write(closeGroup);
        if (listLevel == 0) {
            int i = getRandomInt();
            listtable.write(escape);
            listtable.write(listID);
            writeInt(listtable, i);
            listtable.write(closeGroup);
            listtable.write((byte) '\n');
            listoverride.write(openGroup);
            listoverride.write(escape);
            listoverride.write(listOverride);
            listoverride.write(escape);
            listoverride.write(listID);
            writeInt(listoverride, i);
            listoverride.write(escape);
            listoverride.write(listOverrideCount);
            writeInt(listoverride, 0);
            listoverride.write(escape);
            listoverride.write(listBegin);
            writeInt(listoverride, currentListID);
            currentListID++;
            listoverride.write(closeGroup);
            listoverride.write((byte) '\n');
        }
        out.write(escape);
        out.write(paragraphDefaults);
    }

    /**
     * Write a <code>Table</code>.
     *
     * @param table The <code>table</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * Currently no nesting of tables is supported. If a cell contains anything but a Cell Object it is ignored.
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeTable(Table table, ByteArrayOutputStream out) throws IOException, DocumentException {
        inTable = true;
        table.complete();
        RtfTable rtfTable = new RtfTable(this);
        rtfTable.importTable(table, pageWidth - marginLeft - marginRight);
        rtfTable.writeTable(out);
        inTable = false;
    }


    /**
     * Write an <code>Image</code>.
     *
     * @param image The <code>image</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * At the moment only PNG and JPEG Images are supported.
     *
     * @throws IOException
     * @throws DocumentException
     */
    private void writeImage(Image image, ByteArrayOutputStream out) throws IOException, DocumentException {
        int type = image.getOriginalType();
        if (!(type == Image.ORIGINAL_JPEG || type == Image.ORIGINAL_BMP
            || type == Image.ORIGINAL_PNG || type == Image.ORIGINAL_WMF))
            throw new DocumentException("Only BMP, PNG, WMF and JPEG images are supported by the RTF Writer");
        switch (image.alignment()) {
            case Element.ALIGN_LEFT:
                out.write(escape);
                out.write(alignLeft);
                break;
            case Element.ALIGN_RIGHT:
                out.write(escape);
                out.write(alignRight);
                break;
            case Element.ALIGN_CENTER:
                out.write(escape);
                out.write(alignCenter);
                break;
            case Element.ALIGN_JUSTIFIED:
                out.write(escape);
                out.write(alignJustify);
                break;
        }
        out.write(openGroup);
        out.write(extendedEscape);
        out.write(pictureGroup);
        out.write(openGroup);
        out.write(escape);
        out.write(picture);
        out.write(escape);
        switch (type) {
            case Image.ORIGINAL_JPEG:
                out.write(pictureJPEG);
                break;
            case Image.ORIGINAL_PNG:
                out.write(picturePNG);
                break;
            case Image.ORIGINAL_WMF:
            case Image.ORIGINAL_BMP:
                out.write(pictureWMF);
                break;
        }
        out.write(escape);
        out.write(pictureWidth);
        writeInt(out, (int) (image.plainWidth() * TWIPSFACTOR));
        out.write(escape);
        out.write(pictureHeight);
        writeInt(out, (int) (image.plainHeight() * TWIPSFACTOR));


// For some reason this messes up the intended image size. It makes it too big. Weird
//
//        out.write(escape);
//        out.write(pictureIntendedWidth);
//        writeInt(out, (int) (image.plainWidth() * twipsFactor));
//        out.write(escape);
//        out.write(pictureIntendedHeight);
//        writeInt(out, (int) (image.plainHeight() * twipsFactor));


        if (image.width() > 0) {
            out.write(escape);
            out.write(pictureScaleX);
            writeInt(out, (int) (100 / image.width() * image.plainWidth()));
        }
        if (image.height() > 0) {
            out.write(escape);
            out.write(pictureScaleY);
            writeInt(out, (int) (100 / image.height() * image.plainHeight()));
        }
        out.write(delimiter);
        InputStream imgIn;
        if (type == Image.ORIGINAL_BMP) {
            imgIn = new ByteArrayInputStream(MetaDo.wrapBMP(image));
        }
        else {
            if (image.getOriginalData() == null) {
                imgIn = image.url().openStream();
            } else {
                imgIn = new ByteArrayInputStream(image.getOriginalData());
            }
            if (type == Image.ORIGINAL_WMF) { //remove the placeable header
                long skipLength = 22;
            	while(skipLength > 0) {
            	    skipLength = skipLength - imgIn.skip(skipLength);
            	}
            }
        }
        int buffer = -1;
        int count = 0;
        out.write((byte) '\n');
        while ((buffer = imgIn.read()) != -1) {
            String helperStr = Integer.toHexString(buffer);
            if (helperStr.length() < 2) helperStr = "0" + helperStr;
            out.write(helperStr.getBytes());
            count++;
            if (count == 64) {
                out.write((byte) '\n');
                count = 0;
            }
        }
        imgIn.close();
        out.write(closeGroup);
        out.write(closeGroup);
        out.write((byte) '\n');
    }

    /**
     * Write an <code>Annotation</code>
     *
     * @param annotationElement The <code>Annotation</code> to be written
     * @param out The <code>ByteArrayOutputStream</code> to write to
     *
     * @throws IOException
     */
    private void writeAnnotation(Annotation annotationElement, ByteArrayOutputStream out) throws IOException {
        int id = getRandomInt();
        out.write(openGroup);
        out.write(extendedEscape);
        out.write(annotationID);
        out.write(delimiter);
        writeInt(out, id);
        out.write(closeGroup);
        out.write(openGroup);
        out.write(extendedEscape);
        out.write(annotationAuthor);
        out.write(delimiter);
        out.write(annotationElement.title().getBytes());
        out.write(closeGroup);
        out.write(openGroup);
        out.write(extendedEscape);
        out.write(annotation);
        out.write(escape);
        out.write(paragraphDefaults);
        out.write(delimiter);
        out.write(annotationElement.content().getBytes());
        out.write(closeGroup);
    }

    /**
     * Add a <code>Meta</code> element. It is written to the Inforamtion Group
     * and merged with the main <code>ByteArrayOutputStream</code> when the
     * Document is closed.
     *
     * @param metaName The type of <code>Meta</code> element to be added
     * @param meta The <code>Meta</code> element to be added
     *
     * Currently only the Meta Elements Author, Subject, Keywords, Title, Producer and CreationDate are supported.
     *
     * @throws IOException
     */
    private void writeMeta(byte[] metaName, Meta meta) throws IOException {
        info.write(openGroup);
        try {
            info.write(escape);
            info.write(metaName);
            info.write(delimiter);
            if (meta.type() == Meta.CREATIONDATE) {
                writeFormatedDateTime(meta.content());
            } else {
                info.write(meta.content().getBytes());
            }
        } finally {
            info.write(closeGroup);
        }
    }

    /**
     * Writes a date. The date is formated <strong>Year, Month, Day, Hour, Minute, Second</strong>
     *
     * @param date The date to be written
     *
     * @throws IOException
     */
    private void writeFormatedDateTime(String date) throws IOException {
        Calendar cal = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
        ParsePosition pp = new ParsePosition(0);
        Date d = sdf.parse(date, pp);
        if (d == null) {
            d = new Date();
        }
        cal.setTime(d);
        info.write(escape);
        info.write(year);
        writeInt(info, cal.get(Calendar.YEAR));
        info.write(escape);
        info.write(month);
        writeInt(info, cal.get(Calendar.MONTH));
        info.write(escape);
        info.write(day);
        writeInt(info, cal.get(Calendar.DAY_OF_MONTH));
        info.write(escape);
        info.write(hour);
        writeInt(info, cal.get(Calendar.HOUR_OF_DAY));
        info.write(escape);
        info.write(minute);
        writeInt(info, cal.get(Calendar.MINUTE));
        info.write(escape);
        info.write(second);
        writeInt(info, cal.get(Calendar.SECOND));
    }

    /**
     * Add a new <code>Font</code> to the list of fonts. If the <code>Font</code>
     * already exists in the list of fonts, then it is not added again.
     *
     * @param newFont The <code>Font</code> to be added
     *
     * @return The index of the <code>Font</code> in the font list
     */
    protected int addFont(Font newFont) {
        int fn = -1;

        for (int i = 0; i < fontList.size(); i++) {
            if (newFont.getFamilyname().equals(((Font) fontList.get(i)).getFamilyname())) {
                fn = i;
            }
        }
        if (fn == -1) {
            fontList.add(newFont);
            return fontList.size() - 1;
        }
        return fn;
    }

    /**
     * Add a new <code>Color</code> to the list of colours. If the <code>Color</code>
     * already exists in the list of colours, then it is not added again.
     *
     * @param newColor The <code>Color</code> to be added
     *
     * @return The index of the <code>color</code> in the colour list
     */
    protected int addColor(Color newColor) {
        int cn = 0;
        if (newColor == null) {
            return cn;
        }
        cn = colorList.indexOf(newColor);
        if (cn == -1) {
            colorList.add(newColor);
            return colorList.size() - 1;
        }
        return cn;
    }

    /**
     * Merge all the different <code>ArrayList</code>s and <code>ByteArrayOutputStream</code>s
     * to the final <code>ByteArrayOutputStream</code>
     *
     * @return <code>true</code> if all information was sucessfully written to the <code>ByteArrayOutputStream</code>
     */
    private boolean writeDocument() {
        try {
            writeDocumentIntro();
            writeFontList();
            os.write((byte) '\n');
            writeColorList();
            os.write((byte) '\n');
            writeList();
            os.write((byte) '\n');
            writeInfoGroup();
            os.write((byte) '\n');
            writeDocumentFormat();
            os.write((byte) '\n');
            ByteArrayOutputStream hf = new ByteArrayOutputStream();
            writeSectionDefaults(hf);
            hf.writeTo(os);
            content.writeTo(os);
            os.write(closeGroup);
            return true;
        } catch (IOException e) {
            System.err.println(e.getMessage());
            return false;
        }

    }

    /** Write the Rich Text file settings
     * @throws IOException
     */
    private void writeDocumentIntro() throws IOException {
        os.write(openGroup);
        os.write(escape);
        os.write(docBegin);
        os.write(escape);
        os.write(ansi);
        os.write(escape);
        os.write(ansiCodepage);
        writeInt(os, 1252);
        os.write((byte)'\n');
        os.write(escape);
        os.write(defaultFont);
        writeInt(os, 0);
    }

    /**
     * Write the font list to the final <code>ByteArrayOutputStream</code>
     * @throws IOException
     */
    private void writeFontList() throws IOException {
        Font fnt;

        os.write(openGroup);
        os.write(escape);
        os.write(fontTable);
        for (int i = 0; i < fontList.size(); i++) {
            fnt = (Font) fontList.get(i);
            os.write(openGroup);
            os.write(escape);
            os.write(fontNumber);
            writeInt(os, i);
            os.write(escape);
            switch (Font.getFamilyIndex(fnt.getFamilyname())) {
                case Font.COURIER:
                    os.write(fontModern);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 0);
                    os.write(delimiter);
                    os.write(fontCourier);
                    break;
                case Font.HELVETICA:
                    os.write(fontSwiss);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 0);
                    os.write(delimiter);
                    os.write(fontArial);
                    break;
                case Font.SYMBOL:
                    os.write(fontRoman);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 2);
                    os.write(delimiter);
                    os.write(fontSymbol);
                    break;
                case Font.TIMES_ROMAN:
                    os.write(fontRoman);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 0);
                    os.write(delimiter);
                    os.write(fontTimesNewRoman);
                    break;
                case Font.ZAPFDINGBATS:
                    os.write(fontTech);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 0);
                    os.write(delimiter);
                    os.write(fontWindings);
                    break;
                default:
                    os.write(fontRoman);
                    os.write(escape);
                    os.write(fontCharset);
                    writeInt(os, 0);
                    os.write(delimiter);
                    os.write(filterSpecialChar(fnt.getFamilyname(), true).getBytes());
            }
            os.write(commaDelimiter);
            os.write(closeGroup);
        }
        os.write(closeGroup);
    }

    /**
     * Write the colour list to the final <code>ByteArrayOutputStream</code>
     * @throws IOException
     */
    private void writeColorList() throws IOException {
        Color color = null;

        os.write(openGroup);
        os.write(escape);
        os.write(colorTable);
        for (int i = 0; i < colorList.size(); i++) {
            color = (Color) colorList.get(i);
            os.write(escape);
            os.write(colorRed);
            writeInt(os, color.getRed());
            os.write(escape);
            os.write(colorGreen);
            writeInt(os, color.getGreen());
            os.write(escape);
            os.write(colorBlue);
            writeInt(os, color.getBlue());
            os.write(commaDelimiter);
        }
        os.write(closeGroup);
    }

    /**
     * Write the Information Group to the final <code>ByteArrayOutputStream</code>
     * @throws IOException
     */
    private void writeInfoGroup() throws IOException {
        os.write(openGroup);
        os.write(escape);
        os.write(infoBegin);
        info.writeTo(os);
        os.write(closeGroup);
    }

    /**
     * Write the listtable and listoverridetable to the final <code>ByteArrayOutputStream</code>
     * @throws IOException
     */
    private void writeList() throws IOException {
        listtable.write(closeGroup);
        listoverride.write(closeGroup);
        listtable.writeTo(os);
        os.write((byte) '\n');
        listoverride.writeTo(os);
    }

    /**
     * Write an integer
     *
     * @param out The <code>OuputStream</code> to which the <code>int</code> value is to be written
     * @param i The <code>int</code> value to be written
     * @throws IOException
     */
    public final static void writeInt(OutputStream out, int i) throws IOException {
        out.write(Integer.toString(i).getBytes());
    }

    /**
     * Get a random integer.
     * This returns a <b>unique</b> random integer to be used with listids.
     *
     * @return Random <code>int</code> value.
     */
    private int getRandomInt() {
        boolean ok = false;
        Integer newInt = null;
        Integer oldInt = null;
        while (!ok) {
            newInt = new Integer((int) (Math.random() * Integer.MAX_VALUE));
            ok = true;
            for (int i = 0; i < listIds.size(); i++) {
                oldInt = (Integer) listIds.get(i);
                if (oldInt.equals(newInt)) {
                    ok = true;
                }
            }
        }
        listIds.add(newInt);
        return newInt.intValue();
    }

    /**
     * Write the current header and footer to a <code>ByteArrayOutputStream</code>
     *
     * @param os		The <code>ByteArrayOutputStream</code> to which the header and footer will be written.
     * @throws IOException
     */
    public void writeHeadersFooters(ByteArrayOutputStream os) throws IOException {
        if (this.footer instanceof RtfHeaderFooters) {
            RtfHeaderFooters rtfHf = (RtfHeaderFooters) this.footer;
            HeaderFooter hf = rtfHf.get(RtfHeaderFooters.ALL_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, footerBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.LEFT_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, footerlBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.RIGHT_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, footerrBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.FIRST_PAGE);
            if (hf != null) {
                writeHeaderFooter(hf, footerfBegin, os);
            }
        } else {
            writeHeaderFooter(this.footer, footerBegin, os);
        }
        if (this.header instanceof RtfHeaderFooters) {
            RtfHeaderFooters rtfHf = (RtfHeaderFooters) this.header;
            HeaderFooter hf = rtfHf.get(RtfHeaderFooters.ALL_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, headerBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.LEFT_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, headerlBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.RIGHT_PAGES);
            if (hf != null) {
                writeHeaderFooter(hf, headerrBegin, os);
            }
            hf = rtfHf.get(RtfHeaderFooters.FIRST_PAGE);
            if (hf != null) {
                writeHeaderFooter(hf, headerfBegin, os);
            }
        } else {
            writeHeaderFooter(this.header, headerBegin, os);
        }
    }

    /**
     * Write a <code>HeaderFooter</code> to a <code>ByteArrayOutputStream</code>
     *
     * @param headerFooter	The <code>HeaderFooter</code> object to be written.
     * @param hfType		The type of header or footer to be added.
     * @param target		The <code>ByteArrayOutputStream</code> to which the <code>HeaderFooter</code> will be written.
     * @throws IOException
     */
    private void writeHeaderFooter(HeaderFooter headerFooter, byte[] hfType, ByteArrayOutputStream target) throws IOException {
        inHeaderFooter = true;
        try {
            target.write(openGroup);
            target.write(escape);
            target.write(hfType);
            target.write(delimiter);
            if (headerFooter != null) {
                if (headerFooter instanceof RtfHeaderFooter && ((RtfHeaderFooter) headerFooter).content() != null) {
                    this.addElement(((RtfHeaderFooter) headerFooter).content(), target);
                } else {
                    Paragraph par = new Paragraph();
                    par.setAlignment(headerFooter.alignment());
                    if (headerFooter.getBefore() != null) {
                        par.add(headerFooter.getBefore());
                    }
                    if (headerFooter.isNumbered()) {
                        par.add(new RtfPageNumber("", headerFooter.getBefore().font()));
                    }
                    if (headerFooter.getAfter() != null) {
                        par.add(headerFooter.getAfter());
                    }
                    this.addElement(par, target);
                }
            }
            target.write(closeGroup);
        } catch (DocumentException e) {
            throw new IOException("DocumentException - " + e.getMessage());
        }
        inHeaderFooter = false;
    }

    /**
     *  Write the <code>Document</code>'s Paper and Margin Size
     *  to the final <code>ByteArrayOutputStream</code>
     * @throws IOException
     */
    private void writeDocumentFormat() throws IOException {
//        os.write(openGroup);
        os.write(escape);
        os.write(rtfPaperWidth);
        writeInt(os, pageWidth);
        os.write(escape);
        os.write(rtfPaperHeight);
        writeInt(os, pageHeight);
        os.write(escape);
        os.write(rtfMarginLeft);
        writeInt(os, marginLeft);
        os.write(escape);
        os.write(rtfMarginRight);
        writeInt(os, marginRight);
        os.write(escape);
        os.write(rtfMarginTop);
        writeInt(os, marginTop);
        os.write(escape);
        os.write(rtfMarginBottom);
        writeInt(os, marginBottom);
//        os.write(closeGroup);
    }

    /**
     * Initialise all helper classes.
     * Clears alls lists, creates new <code>ByteArrayOutputStream</code>'s
     */
    private void initDefaults() {
        fontList.clear();
        colorList.clear();
        info = new ByteArrayOutputStream();
        content = new ByteArrayOutputStream();
        listtable = new ByteArrayOutputStream();
        listoverride = new ByteArrayOutputStream();
        document.addProducer();
        document.addCreationDate();
        addFont(new Font(Font.TIMES_ROMAN, 10, Font.NORMAL));
        addColor(new Color(0, 0, 0));
        addColor(new Color(255, 255, 255));
        listIds = new ArrayList();
        try {
            listtable.write(openGroup);
            listtable.write(extendedEscape);
            listtable.write(listtableGroup);
            listtable.write((byte) '\n');
            listoverride.write(openGroup);
            listoverride.write(extendedEscape);
            listoverride.write(listoverridetableGroup);
            listoverride.write((byte) '\n');
        } catch (IOException e) {
            System.err.println("InitDefaultsError" + e);
        }
    }

    /**
     * Writes the default values for the current Section
     *
     * @param out The <code>ByteArrayOutputStream</code> to be written to
     * @throws IOException
     */
    private void writeSectionDefaults(ByteArrayOutputStream out) throws IOException {
        if (header instanceof RtfHeaderFooters || footer instanceof RtfHeaderFooters) {
            RtfHeaderFooters rtfHeader = (RtfHeaderFooters) header;
            RtfHeaderFooters rtfFooter = (RtfHeaderFooters) footer;
            if ((rtfHeader != null && (rtfHeader.get(RtfHeaderFooters.LEFT_PAGES) != null || rtfHeader.get(RtfHeaderFooters.RIGHT_PAGES) != null)) || (rtfFooter != null && (rtfFooter.get(RtfHeaderFooters.LEFT_PAGES) != null || rtfFooter.get(RtfHeaderFooters.RIGHT_PAGES) != null))) {
                out.write(escape);
                out.write(facingPages);
            }
        }
        if (hasTitlePage) {
            out.write(escape);
            out.write(titlePage);
        }
        writeHeadersFooters(out);
        if (landscape) {
            //out.write(escape);
            //out.write(landscapeTag1);
            out.write(escape);
            out.write(landscapeTag2);
            out.write(escape);
            out.write(sectionPageWidth);
            writeInt(out, pageWidth);
            out.write(escape);
            out.write(sectionPageHeight);
            writeInt(out, pageHeight);
        } else {
            out.write(escape);
            out.write(sectionPageWidth);
            writeInt(out, pageWidth);
            out.write(escape);
            out.write(sectionPageHeight);
            writeInt(out, pageHeight);
        }
    }

    /**
     * This method tries to fit the <code>Rectangle pageSize</code> to one of the predefined PageSize rectangles.
     * If a match is found the pageWidth and pageHeight will be set according to values determined from files
     * generated by MS Word2000 and OpenOffice 641. If no match is found the method will try to match the rotated
     * Rectangle by calling itself with the parameter rotate set to true.
     * @param pageSize a rectangle defining the size of the page
     * @param rotate portrait or lanscape?
     * @return true if the format parsing succeeded
     */
    private boolean parseFormat(Rectangle pageSize, boolean rotate) {
        if (rotate) {
            pageSize = pageSize.rotate();
        }
        if (rectEquals(pageSize, PageSize.A3)) {
            pageWidth = 16837;
            pageHeight = 23811;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.A4)) {
            pageWidth = 11907;
            pageHeight = 16840;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.A5)) {
            pageWidth = 8391;
            pageHeight = 11907;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.A6)) {
            pageWidth = 5959;
            pageHeight = 8420;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.B4)) {
            pageWidth = 14570;
            pageHeight = 20636;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.B5)) {
            pageWidth = 10319;
            pageHeight = 14572;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.HALFLETTER)) {
            pageWidth = 7927;
            pageHeight = 12247;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.LETTER)) {
            pageWidth = 12242;
            pageHeight = 15842;
            landscape = rotate;
            return true;
        }
        if (rectEquals(pageSize, PageSize.LEGAL)) {
            pageWidth = 12252;
            pageHeight = 20163;
            landscape = rotate;
            return true;
        }
        if (!rotate && parseFormat(pageSize, true)) {
            int x = pageWidth;
            pageWidth = pageHeight;
            pageHeight = x;
            return true;
        }
        return false;
    }

    /**
     * This method compares to Rectangles. They are considered equal if width and height are the same
     * @param rect1
     * @param rect2
     * @return true if rect1 and rect2 represent the same rectangle
     */
    private boolean rectEquals(Rectangle rect1, Rectangle rect2) {
        return (rect1.width() == rect2.width()) && (rect1.height() == rect2.height());
    }

    /**
     * Returns whether we are currently writing a header or footer
     *
     * @return the value of inHeaderFooter
     */
    public boolean writingHeaderFooter() {
        return inHeaderFooter;
    }

    /**
     * Replaces special characters with their unicode values
     *
     * @param str The original <code>String</code>
     * @param useHex
     * @return The converted String
     */
    public final static String filterSpecialChar(String str, boolean useHex) {
        int length = str.length();
        int z = (int) 'z';
        StringBuffer ret = new StringBuffer(length);
        for (int i = 0; i < length; i++) {
            char ch = str.charAt(i);

            if (ch == '\\') {
                ret.append("\\\\");
            } else if (ch == '\n') {
                ret.append("\\par ");
            } else if (((int) ch) > z) {
                if(useHex) {
                    ret.append("\\\'").append(Long.toHexString((long) ch));
                } else {
                ret.append("\\u").append((long) ch).append('?');
                }
            } else {
                ret.append(ch);
            }
        }
        String s = ret.toString();
        if(s.indexOf("$newpage$") >= 0) {
            String before = s.substring(0, s.indexOf("$newpage$"));
            String after = s.substring(s.indexOf("$newpage$") + 9);
            ret = new StringBuffer(before);
            ret.append("\\page\\par ");
            ret.append(after);
            return ret.toString();
        }
        return s;
    }

    private void addHeaderFooterFontColor(HeaderFooter hf) {
        if(hf instanceof RtfHeaderFooter) {
            RtfHeaderFooter rhf = (RtfHeaderFooter) hf;
            if(rhf.content() instanceof Chunk) {
                addFont(((Chunk) rhf.content()).font());
                addColor(((Chunk) rhf.content()).font().color());
            } else if(rhf.content() instanceof Phrase) {
                addFont(((Phrase) rhf.content()).font());
                addColor(((Phrase) rhf.content()).font().color());
            }
        }
        if(hf.getBefore() != null) {
            addFont(hf.getBefore().font());
            addColor(hf.getBefore().font().color());
        }
        if(hf.getAfter() != null) {
            addFont(hf.getAfter().font());
            addColor(hf.getAfter().font().color());
        }
    }

    private void processHeaderFooter(HeaderFooter hf) {
        if(hf != null) {
            if(hf instanceof RtfHeaderFooters) {
                RtfHeaderFooters rhf = (RtfHeaderFooters) hf;
                if(rhf.get(RtfHeaderFooters.ALL_PAGES) != null) {
                    addHeaderFooterFontColor(rhf.get(RtfHeaderFooters.ALL_PAGES));
                }
                if(rhf.get(RtfHeaderFooters.LEFT_PAGES) != null) {
                    addHeaderFooterFontColor(rhf.get(RtfHeaderFooters.LEFT_PAGES));
                }
                if(rhf.get(RtfHeaderFooters.RIGHT_PAGES) != null) {
                    addHeaderFooterFontColor(rhf.get(RtfHeaderFooters.RIGHT_PAGES));
                }
                if(rhf.get(RtfHeaderFooters.FIRST_PAGE) != null) {
                    addHeaderFooterFontColor(rhf.get(RtfHeaderFooters.FIRST_PAGE));
                }
            } else {
                addHeaderFooterFontColor(hf);
            }
        }
    }
    
    /**
     * @see com.lowagie.text.DocListener#setMarginMirroring(boolean)
     */
    public boolean setMarginMirroring(boolean MarginMirroring) {
        return false;
    }
    
}