001: /*
002: * $Id: RtfWriter2.java 2623 2007-02-23 22:28:28Z xlv $
003: * $Name$
004: *
005: * Copyright 2001, 2002, 2003, 2004 by Mark Hall
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text.rtf;
052:
053: import java.io.IOException;
054: import java.io.OutputStream;
055: import java.io.Reader;
056:
057: import com.lowagie.text.DocWriter;
058: import com.lowagie.text.Document;
059: import com.lowagie.text.DocumentException;
060: import com.lowagie.text.Element;
061: import com.lowagie.text.HeaderFooter;
062: import com.lowagie.text.Rectangle;
063: import com.lowagie.text.rtf.direct.RtfImportMappings;
064: import com.lowagie.text.rtf.direct.RtfParser;
065: import com.lowagie.text.rtf.document.RtfDocument;
066: import com.lowagie.text.rtf.document.RtfDocumentSettings;
067: import com.lowagie.text.rtf.text.RtfNewPage;
068:
069: /**
070: * The RtfWriter allows the creation of rtf documents via the iText system
071: *
072: * Version: $Id: RtfWriter2.java 2623 2007-02-23 22:28:28Z xlv $
073: * @author Mark Hall (mhall@edu.uni-klu.ac.at)
074: */
075: public class RtfWriter2 extends DocWriter {
076: /**
077: * The RtfDocument this RtfWriter is creating
078: */
079: private RtfDocument rtfDoc = null;
080:
081: /**
082: * Constructs a new RtfWriter that listens to the specified Document and
083: * writes its output to the OutputStream.
084: *
085: * @param doc The Document that this RtfWriter listens to
086: * @param os The OutputStream to write to
087: */
088: protected RtfWriter2(Document doc, OutputStream os) {
089: super (doc, os);
090: doc.addDocListener(this );
091: rtfDoc = new RtfDocument();
092: }
093:
094: /**
095: * Static method to generate RtfWriters
096: *
097: * @param doc The Document that this RtfWriter listens to
098: * @param os The OutputStream to write to
099: * @return The new RtfWriter
100: */
101: public static RtfWriter2 getInstance(Document doc, OutputStream 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: /**
145: * This method is not supported in the RtfWriter
146: */
147: public void resetPageCount() {
148: }
149:
150: /**
151: * This method is not supported in the RtfWriter
152: */
153: public void clearTextWrap() {
154: }
155:
156: /**
157: * Opens the RtfDocument
158: * @throws IOException
159: */
160: public void open() {
161: super .open();
162: this .rtfDoc.open();
163: }
164:
165: /**
166: * Closes the RtfDocument. This causes the document to be written
167: * to the specified OutputStream
168: */
169: public void close() {
170: if (open) {
171: rtfDoc.writeDocument(os);
172: super .close();
173: this .rtfDoc = new RtfDocument();
174: }
175: }
176:
177: /**
178: * Adds an Element to the Document
179: *
180: * @param element The element to be added
181: * @return <code>false</code>
182: * @throws DocumentException
183: */
184: public boolean add(Element element) throws DocumentException {
185: if (pause) {
186: return false;
187: }
188: RtfBasicElement rtfElement = rtfDoc.getMapper().mapElement(
189: element);
190: if (rtfElement != null) {
191: rtfDoc.add(rtfElement);
192: return true;
193: } else {
194: return false;
195: }
196: }
197:
198: /**
199: * Adds a page break
200: *
201: * @return <code>false</code>
202: */
203: public boolean newPage() {
204: rtfDoc.add(new RtfNewPage(rtfDoc));
205: return true;
206: }
207:
208: /**
209: * Sets the page margins
210: *
211: * @param left The left margin
212: * @param right The right margin
213: * @param top The top margin
214: * @param bottom The bottom margin
215: * @return <code>false</code>
216: */
217: public boolean setMargins(float left, float right, float top,
218: float bottom) {
219: rtfDoc.getDocumentHeader().getPageSetting().setMarginLeft(
220: (int) (left * RtfElement.TWIPS_FACTOR));
221: rtfDoc.getDocumentHeader().getPageSetting().setMarginRight(
222: (int) (right * RtfElement.TWIPS_FACTOR));
223: rtfDoc.getDocumentHeader().getPageSetting().setMarginTop(
224: (int) (top * RtfElement.TWIPS_FACTOR));
225: rtfDoc.getDocumentHeader().getPageSetting().setMarginBottom(
226: (int) (bottom * RtfElement.TWIPS_FACTOR));
227: return true;
228: }
229:
230: /**
231: * Sets the size of the page
232: *
233: * @param rect A Rectangle representing the page
234: * @return <code>false</code>
235: */
236: public boolean setPageSize(Rectangle rect) {
237: rtfDoc.getDocumentHeader().getPageSetting().setPageSize(rect);
238: return true;
239: }
240:
241: /**
242: * Whether to automagically generate table of contents entries when
243: * adding Chapters or Sections.
244: *
245: * @param autogenerate Whether to automatically generate TOC entries
246: */
247: public void setAutogenerateTOCEntries(boolean autogenerate) {
248: this .rtfDoc.setAutogenerateTOCEntries(autogenerate);
249: }
250:
251: /**
252: * Sets the rtf data cache style to use. Valid values are given in the
253: * RtfDataCache class.
254: *
255: * @param dataCacheStyle The style to use.
256: * @throws DocumentException If data has already been written into the data cache.
257: * @throws IOException If the disk cache could not be initialised.
258: * @deprecated Use RtfWriter2.getDocumentSettings().setDataCacheStyle(...);
259: */
260: public void setDataCacheStyle(int dataCacheStyle) {
261: this .rtfDoc.getDocumentSettings().setDataCacheStyle(
262: dataCacheStyle);
263: }
264:
265: /**
266: * Gets the RtfDocumentSettings that specify how the rtf document is generated.
267: *
268: * @return The current RtfDocumentSettings.
269: */
270: public RtfDocumentSettings getDocumentSettings() {
271: return this .rtfDoc.getDocumentSettings();
272: }
273:
274: /**
275: * Adds the complete RTF document to the current RTF document being generated.
276: * It will parse the font and color tables and correct the font and color references
277: * so that the imported RTF document retains its formattings.
278: *
279: * @param documentSource The Reader to read the RTF document from.
280: * @throws IOException On errors reading the RTF document.
281: * @throws DocumentException On errors adding to this RTF document.
282: */
283: public void importRtfDocument(Reader documentSource)
284: throws IOException, DocumentException {
285: if (!this .open) {
286: throw new DocumentException(
287: "The document must be open to import RTF documents.");
288: }
289: RtfParser rtfImport = new RtfParser();
290: rtfImport.importRtfDocument(documentSource, this .rtfDoc);
291: }
292:
293: /**
294: * Adds a fragment of an RTF document to the current RTF document being generated.
295: * Since this fragment doesn't contain font or color tables, all fonts and colors
296: * are mapped to the default font and color. If the font and color mappings are
297: * known, they can be specified via the mappings parameter.
298: *
299: * @param documentSource The Reader to read the RTF fragment from.
300: * @param mappings The RtfImportMappings that contain font and color mappings to apply to the fragment.
301: * @throws IOException On errors reading the RTF fragment.
302: * @throws DocumentException On errors adding to this RTF fragment.
303: */
304: public void importRtfFragment(Reader documentSource,
305: RtfImportMappings mappings) throws IOException,
306: DocumentException {
307: if (!this .open) {
308: throw new DocumentException(
309: "The document must be open to import RTF fragments.");
310: }
311: RtfParser rtfImport = new RtfParser();
312: rtfImport.importRtfFragment(documentSource, this.rtfDoc,
313: mappings);
314: }
315: }
|