KickJava   Java API By Example, From Geeks To Geeks.

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


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

50
51 package com.lowagie.text.rtf;
52
53 import java.io.IOException JavaDoc;
54 import java.io.OutputStream JavaDoc;
55 import java.io.Reader JavaDoc;
56
57 import com.lowagie.text.DocWriter;
58 import com.lowagie.text.Document;
59 import com.lowagie.text.DocumentException;
60 import com.lowagie.text.Element;
61 import com.lowagie.text.HeaderFooter;
62 import com.lowagie.text.Rectangle;
63 import com.lowagie.text.rtf.direct.RtfImportMappings;
64 import com.lowagie.text.rtf.direct.RtfParser;
65 import com.lowagie.text.rtf.document.RtfDocument;
66 import com.lowagie.text.rtf.document.RtfDocumentSettings;
67 import com.lowagie.text.rtf.text.RtfNewPage;
68
69 /**
70  * The RtfWriter allows the creation of rtf documents via the iText system
71  *
72  * Version: $Id: RtfWriter2.java 2623 2007-02-23 22:28:28Z xlv $
73  * @author Mark Hall (mhall@edu.uni-klu.ac.at)
74  */

75 public class RtfWriter2 extends DocWriter {
76     /**
77      * The RtfDocument this RtfWriter is creating
78      */

79     private RtfDocument rtfDoc = null;
80     
81     /**
82      * Constructs a new RtfWriter that listens to the specified Document and
83      * writes its output to the OutputStream.
84      *
85      * @param doc The Document that this RtfWriter listens to
86      * @param os The OutputStream to write to
87      */

88     protected RtfWriter2(Document doc, OutputStream JavaDoc os) {
89         super(doc, os);
90         doc.addDocListener(this);
91         rtfDoc = new RtfDocument();
92     }
93
94     /**
95      * Static method to generate RtfWriters
96      *
97      * @param doc The Document that this RtfWriter listens to
98      * @param os The OutputStream to write to
99      * @return The new RtfWriter
100      */

101     public static RtfWriter2 getInstance(Document doc, OutputStream JavaDoc os) {
102         return new RtfWriter2(doc, os);
103     }
104
105     /**
106      * Sets the header to use
107      *
108      * @param hf The HeaderFooter to use
109      */

110     public void setHeader(HeaderFooter hf) {
111         this.rtfDoc.getDocumentHeader().setHeader(hf);
112     }
113     
114     /**
115      * Resets the header
116      */

117     public void resetHeader() {
118         this.rtfDoc.getDocumentHeader().setHeader(null);
119     }
120     
121     /**
122      * Sets the footer to use
123      *
124      * @param hf The HeaderFooter to use
125      */

126     public void setFooter(HeaderFooter hf) {
127         this.rtfDoc.getDocumentHeader().setFooter(hf);
128     }
129     
130     /**
131      * Resets the footer
132      */

133     public void resetFooter() {
134         this.rtfDoc.getDocumentHeader().setFooter(null);
135     }
136
137     /**
138      * This method is not supported in the RtfWriter
139      * @param i Unused
140      */

141     public void setPageCount(int i) {}
142     
143     /**
144      * This method is not supported in the RtfWriter
145      */

146     public void resetPageCount() {}
147
148     /**
149      * This method is not supported in the RtfWriter
150      */

151     public void clearTextWrap() {}
152
153     /**
154      * Opens the RtfDocument
155      * @throws IOException
156      */

157     public void open() {
158         super.open();
159         this.rtfDoc.open();
160     }
161     
162     /**
163      * Closes the RtfDocument. This causes the document to be written
164      * to the specified OutputStream
165      */

166     public void close() {
167         if (open) {
168             rtfDoc.writeDocument(os);
169             super.close();
170             this.rtfDoc = new RtfDocument();
171         }
172     }
173
174     /**
175      * Adds an Element to the Document
176      *
177      * @param element The element to be added
178      * @return <code>false</code>
179      * @throws DocumentException
180      */

181     public boolean add(Element element) throws DocumentException {
182         if (pause) {
183             return false;
184         }
185         RtfBasicElement rtfElement = rtfDoc.getMapper().mapElement(element);
186         if(rtfElement != null) {
187             rtfDoc.add(rtfElement);
188             return true;
189         } else {
190             return false;
191         }
192     }
193     
194     /**
195      * Adds a page break
196      *
197      * @return <code>false</code>
198      */

199     public boolean newPage() {
200         rtfDoc.add(new RtfNewPage(rtfDoc));
201         return true;
202     }
203
204     /**
205      * Sets the page margins
206      *
207      * @param left The left margin
208      * @param right The right margin
209      * @param top The top margin
210      * @param bottom The bottom margin
211      * @return <code>false</code>
212      */

213     public boolean setMargins(float left, float right, float top, float bottom) {
214         rtfDoc.getDocumentHeader().getPageSetting().setMarginLeft((int) (left * RtfElement.TWIPS_FACTOR));
215         rtfDoc.getDocumentHeader().getPageSetting().setMarginRight((int) (right * RtfElement.TWIPS_FACTOR));
216         rtfDoc.getDocumentHeader().getPageSetting().setMarginTop((int) (top * RtfElement.TWIPS_FACTOR));
217         rtfDoc.getDocumentHeader().getPageSetting().setMarginBottom((int) (bottom * RtfElement.TWIPS_FACTOR));
218         return true;
219     }
220     
221     /**
222      * Sets the size of the page
223      *
224      * @param rect A Rectangle representing the page
225      * @return <code>false</code>
226      */

227     public boolean setPageSize(Rectangle rect) {
228         rtfDoc.getDocumentHeader().getPageSetting().setPageSize(rect);
229         return true;
230     }
231     
232     /**
233      * Whether to automagically generate table of contents entries when
234      * adding Chapters or Sections.
235      *
236      * @param autogenerate Whether to automatically generate TOC entries
237      */

238     public void setAutogenerateTOCEntries(boolean autogenerate) {
239         this.rtfDoc.setAutogenerateTOCEntries(autogenerate);
240     }
241     
242     /**
243      * Sets the rtf data cache style to use. Valid values are given in the
244      * RtfDataCache class.
245      *
246      * @param dataCacheStyle The style to use.
247      * @throws DocumentException If data has already been written into the data cache.
248      * @throws IOException If the disk cache could not be initialised.
249      * @deprecated Use RtfWriter2.getDocumentSettings().setDataCacheStyle(...);
250      */

251     public void setDataCacheStyle(int dataCacheStyle) {
252         this.rtfDoc.getDocumentSettings().setDataCacheStyle(dataCacheStyle);
253     }
254     
255     /**
256      * Gets the RtfDocumentSettings that specify how the rtf document is generated.
257      *
258      * @return The current RtfDocumentSettings.
259      */

260     public RtfDocumentSettings getDocumentSettings() {
261         return this.rtfDoc.getDocumentSettings();
262     }
263     
264     /**
265      * Adds the complete RTF document to the current RTF document being generated.
266      * It will parse the font and color tables and correct the font and color references
267      * so that the imported RTF document retains its formattings.
268      *
269      * @param documentSource The Reader to read the RTF document from.
270      * @throws IOException On errors reading the RTF document.
271      * @throws DocumentException On errors adding to this RTF document.
272      */

273     public void importRtfDocument(Reader JavaDoc documentSource) throws IOException JavaDoc, DocumentException {
274         if(!this.open) {
275             throw new DocumentException("The document must be open to import RTF documents.");
276         }
277         RtfParser rtfImport = new RtfParser();
278         rtfImport.importRtfDocument(documentSource, this.rtfDoc);
279     }
280     
281     /**
282      * Adds a fragment of an RTF document to the current RTF document being generated.
283      * Since this fragment doesn't contain font or color tables, all fonts and colors
284      * are mapped to the default font and color. If the font and color mappings are
285      * known, they can be specified via the mappings parameter.
286      *
287      * @param documentSource The Reader to read the RTF fragment from.
288      * @param mappings The RtfImportMappings that contain font and color mappings to apply to the fragment.
289      * @throws IOException On errors reading the RTF fragment.
290      * @throws DocumentException On errors adding to this RTF fragment.
291      */

292     public void importRtfFragment(Reader JavaDoc documentSource, RtfImportMappings mappings) throws IOException JavaDoc, DocumentException {
293         if(!this.open) {
294             throw new DocumentException("The document must be open to import RTF fragments.");
295         }
296         RtfParser rtfImport = new RtfParser();
297         rtfImport.importRtfFragment(documentSource, this.rtfDoc, mappings);
298     }
299 }
300
Popular Tags