KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > rtf > RtfTOC


1 /**
2  * $Id: RtfTOC.java 2698 2007-04-19 12:03:08Z blowagie $
3  *
4  * Copyright 2002 by
5  * <a HREF="http://www.smb-tec.com">SMB</a>
6  * Steffen.Stundzig (Steffen.Stundzig@smb-tec.com)
7  *
8  * The contents of this file are subject to the Mozilla Public License Version 1.1
9  * (the "License"); you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the License.
15  *
16  * The Original Code is 'iText, a free JAVA-PDF library'.
17  *
18  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
19  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
20  * All Rights Reserved.
21  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
22  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
23  *
24  * Contributor(s): all the names of the contributors are added in the source code
25  * where applicable.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
29  * provisions of LGPL are applicable instead of those above. If you wish to
30  * allow use of your version of this file only under the terms of the LGPL
31  * License and not to allow others to use your version of this file under
32  * the MPL, indicate your decision by deleting the provisions above and
33  * replace them with the notice and other provisions required by the LGPL.
34  * If you do not delete the provisions above, a recipient may use your version
35  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
36  *
37  * This library is free software; you can redistribute it and/or modify it
38  * under the terms of the MPL as stated above or under the terms of the GNU
39  * Library General Public License as published by the Free Software Foundation;
40  * either version 2 of the License, or any later version.
41  *
42  * This library is distributed in the hope that it will be useful, but WITHOUT
43  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
44  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
45  * details.
46  *
47  * If you didn't download this code from the following link, you should check if
48  * you aren't using an obsolete version:
49  * http://www.lowagie.com/iText/
50  */

51
52 package com.lowagie.text.rtf;
53
54 import java.io.IOException JavaDoc;
55 import java.io.OutputStream JavaDoc;
56
57 import com.lowagie.text.Chunk;
58 import com.lowagie.text.DocumentException;
59 import com.lowagie.text.ExceptionConverter;
60 import com.lowagie.text.Font;
61
62 /**
63  * This class can be used to insert a table of contents into
64  * the RTF document.
65  * Therefore the field TOC is used. It works great in Word 2000.
66  * StarOffice doesn't support such fields. Other word version
67  * are not tested yet.
68  *
69  * ONLY FOR USE WITH THE RtfWriter NOT with the RtfWriter2.
70  *
71  * This class is based on the RtfWriter-package from Mark Hall.
72  * @author Steffen.Stundzig (Steffen.Stundzig@smb-tec.com)
73  * @version $Revision: 2698 $Date: 2007/03/15 19:26:36 $
74  * @deprecated Please move to the RtfWriter2 and associated classes. com.lowagie.text.rtf.field.RtfTableOfContents replaces the functionality of this class.
75  */

76 public class RtfTOC extends Chunk implements RtfField {
77
78
79     private String JavaDoc defaultText = "Klicken Sie mit der rechten Maustaste auf diesen Text, um das Inhaltsverzeichnis zu aktualisieren!";
80
81     private boolean addTOCAsTOCEntry = false;
82
83     private Font entryFont = null;
84     private String JavaDoc entryName = null;
85
86
87     /**
88      * @param tocName the headline of the table of contents
89      * @param tocFont the font for the headline
90      */

91     public RtfTOC( String JavaDoc tocName, Font tocFont ) {
92         super( tocName, tocFont );
93     }
94
95     /**
96      * @see com.lowagie.text.rtf.RtfField#write(com.lowagie.text.rtf.RtfWriter, java.io.OutputStream)
97      */

98     public void write( RtfWriter writer, OutputStream JavaDoc out ) throws IOException JavaDoc {
99
100         writer.writeInitialFontSignature( out, this );
101         out.write( RtfWriter.filterSpecialChar( getContent(), true ).getBytes() );
102         writer.writeFinishingFontSignature( out, this );
103         
104         if (addTOCAsTOCEntry) {
105             RtfTOCEntry entry = new RtfTOCEntry( entryName, entryFont );
106             entry.hideText();
107             try {
108                 writer.add( entry );
109             } catch ( DocumentException de ) {
110                 throw new ExceptionConverter(de);
111             }
112         }
113
114         // line break after headline
115
out.write( RtfWriter.escape );
116         out.write( RtfWriter.paragraph );
117         out.write( RtfWriter.delimiter );
118
119         // toc field entry
120
out.write( RtfWriter.openGroup );
121         out.write( RtfWriter.escape );
122         out.write( RtfWriter.field );
123             // field initialization stuff
124
out.write( RtfWriter.openGroup );
125             out.write( RtfWriter.escape );
126             out.write( RtfWriter.fieldContent );
127             out.write( RtfWriter.delimiter );
128             out.write( "TOC".getBytes() );
129             // create the TOC based on the 'toc entries'
130
out.write( RtfWriter.delimiter );
131             out.write( RtfWriter.escape );
132             out.write( RtfWriter.escape );
133             out.write( "f".getBytes() );
134             out.write( RtfWriter.delimiter );
135             // create Hyperlink TOC Entrie
136
out.write( RtfWriter.escape );
137             out.write( RtfWriter.escape );
138             out.write( "h".getBytes() );
139             out.write( RtfWriter.delimiter );
140             // create the TOC based on the paragraph level
141
out.write( RtfWriter.delimiter );
142             out.write( RtfWriter.escape );
143             out.write( RtfWriter.escape );
144             out.write( "u".getBytes() );
145             out.write( RtfWriter.delimiter );
146             // create the TOC based on the paragraph headlines 1-5
147
out.write( RtfWriter.delimiter );
148             out.write( RtfWriter.escape );
149             out.write( RtfWriter.escape );
150             out.write( "o".getBytes() );
151             out.write( RtfWriter.delimiter );
152             out.write( "\"1-5\"".getBytes() );
153             out.write( RtfWriter.delimiter );
154             out.write( RtfWriter.closeGroup );
155
156             // field default result stuff
157
out.write( RtfWriter.openGroup );
158             out.write( RtfWriter.escape );
159             out.write( RtfWriter.fieldDisplay );
160             out.write( RtfWriter.delimiter );
161             out.write( defaultText.getBytes() );
162             out.write( RtfWriter.delimiter );
163             out.write( RtfWriter.closeGroup );
164         out.write( RtfWriter.closeGroup );
165     }
166
167     
168     /**
169      * Add a toc entry
170      * @param entryName the name of the entry
171      * @param entryFont the font to be used for the entry
172      */

173     public void addTOCAsTOCEntry( String JavaDoc entryName, Font entryFont ) {
174         this.addTOCAsTOCEntry = true;
175         this.entryFont = entryFont;
176         this.entryName = entryName;
177     }
178
179     
180     /**
181      * Sets the default text of the Table of Contents
182      * @param text the default text
183      */

184     public void setDefaultText( String JavaDoc text ) {
185         this.defaultText = text;
186     }
187 }
188
Popular Tags