Save This Page
Home » iText-src-2.1.3 » com.lowagie » text » rtf » [javadoc | source]
    1   /*
    2    * $Id: RtfWriter2.java 3440 2008-05-25 18:16:48Z howard_s $
    3    *
    4    * Copyright 2001, 2002, 2003, 2004 by Mark Hall
    5    *
    6    * The contents of this file are subject to the Mozilla Public License Version 1.1
    7    * (the "License"); you may not use this file except in compliance with the License.
    8    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
    9    *
   10    * Software distributed under the License is distributed on an "AS IS" basis,
   11    * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   12    * for the specific language governing rights and limitations under the License.
   13    *
   14    * The Original Code is 'iText, a free JAVA-PDF library'.
   15    *
   16    * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
   17    * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
   18    * All Rights Reserved.
   19    * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
   20    * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
   21    *
   22    * Contributor(s): all the names of the contributors are added in the source code
   23    * where applicable.
   24    *
   25    * Alternatively, the contents of this file may be used under the terms of the
   26    * LGPL license (the ?GNU LIBRARY GENERAL PUBLIC LICENSE?), in which case the
   27    * provisions of LGPL are applicable instead of those above.  If you wish to
   28    * allow use of your version of this file only under the terms of the LGPL
   29    * License and not to allow others to use your version of this file under
   30    * the MPL, indicate your decision by deleting the provisions above and
   31    * replace them with the notice and other provisions required by the LGPL.
   32    * If you do not delete the provisions above, a recipient may use your version
   33    * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
   34    *
   35    * This library is free software; you can redistribute it and/or modify it
   36    * under the terms of the MPL as stated above or under the terms of the GNU
   37    * Library General Public License as published by the Free Software Foundation;
   38    * either version 2 of the License, or any later version.
   39    *
   40    * This library is distributed in the hope that it will be useful, but WITHOUT
   41    * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   42    * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
   43    * details.
   44    *
   45    * If you didn't download this code from the following link, you should check if
   46    * you aren't using an obsolete version:
   47    * http://www.lowagie.com/iText/
   48    */
   49   
   50   package com.lowagie.text.rtf;
   51   
   52   import java.io.FileInputStream;
   53   import java.io.IOException;
   54   import java.io.InputStream;
   55   import java.io.OutputStream;
   56   import java.util.EventListener;
   57   
   58   import com.lowagie.text.DocWriter;
   59   import com.lowagie.text.Document;
   60   import com.lowagie.text.DocumentException;
   61   import com.lowagie.text.Element;
   62   import com.lowagie.text.HeaderFooter;
   63   import com.lowagie.text.Rectangle;
   64   import com.lowagie.text.rtf.document.RtfDocument;
   65   import com.lowagie.text.rtf.document.RtfDocumentSettings;
   66   import com.lowagie.text.rtf.parser.RtfImportMappings;
   67   import com.lowagie.text.rtf.parser.RtfParser;
   68   import com.lowagie.text.rtf.text.RtfNewPage;
   69   
   70   /**
   71    * The RtfWriter allows the creation of rtf documents via the iText system
   72    *
   73    * Version: $Id: RtfWriter2.java 3440 2008-05-25 18:16:48Z howard_s $
   74    * @author Mark Hall (Mark.Hall@mail.room3b.eu)
   75    */
   76   public class RtfWriter2 extends DocWriter {
   77       /**
   78        * The RtfDocument this RtfWriter is creating
   79        */
   80       private RtfDocument rtfDoc = null;
   81       
   82       /**
   83        * Constructs a new RtfWriter that listens to the specified Document and
   84        * writes its output to the OutputStream.
   85        * 
   86        * @param doc The Document that this RtfWriter listens to
   87        * @param os The OutputStream to write to
   88        */
   89       protected RtfWriter2(Document doc, OutputStream os) {
   90           super(doc, os);
   91           doc.addDocListener(this);
   92           rtfDoc = new RtfDocument();
   93       }
   94   
   95       /**
   96        * Static method to generate RtfWriters
   97        * 
   98        * @param doc The Document that this RtfWriter listens to
   99        * @param os The OutputStream to write to
  100        * @return The new RtfWriter
  101        */
  102       public static RtfWriter2 getInstance(Document doc, OutputStream os) {
  103           return new RtfWriter2(doc, os);
  104       }
  105   
  106       /**
  107        * Sets the header to use
  108        * 
  109        * @param hf The HeaderFooter to use
  110        */
  111       public void setHeader(HeaderFooter hf) {
  112           this.rtfDoc.getDocumentHeader().setHeader(hf);
  113       }
  114       
  115       /**
  116        * Resets the header
  117        */
  118       public void resetHeader() {
  119           this.rtfDoc.getDocumentHeader().setHeader(null);
  120       }
  121       
  122       /**
  123        * Sets the footer to use
  124        * 
  125        * @param hf The HeaderFooter to use
  126        */
  127       public void setFooter(HeaderFooter hf) {
  128           this.rtfDoc.getDocumentHeader().setFooter(hf);
  129       }
  130       
  131       /**
  132        * Resets the footer
  133        */
  134       public void resetFooter() {
  135           this.rtfDoc.getDocumentHeader().setFooter(null);
  136       }
  137   
  138       /**
  139        * This method is not supported in the RtfWriter
  140        * @param i Unused
  141        */
  142       public void setPageCount(int i) {}
  143       
  144       /**
  145        * This method is not supported in the RtfWriter
  146        */
  147       public void resetPageCount() {}
  148   
  149       /**
  150        * This method is not supported in the RtfWriter
  151        */
  152       public void clearTextWrap() {}
  153   
  154       /**
  155        * Opens the RtfDocument
  156        * @throws IOException 
  157        */
  158       public void open() {
  159       	super.open();
  160           this.rtfDoc.open();
  161       }
  162       
  163       /**
  164        * Closes the RtfDocument. This causes the document to be written
  165        * to the specified OutputStream
  166        */
  167       public void close() {
  168           if (open) {
  169               rtfDoc.writeDocument(os);
  170               super.close();
  171               this.rtfDoc = new RtfDocument();
  172           }
  173       }
  174   
  175       /**
  176        * Adds an Element to the Document
  177        *
  178        * @param element The element to be added
  179        * @return <code>false</code>
  180        * @throws DocumentException
  181        */
  182       public boolean add(Element element) throws DocumentException {
  183           if (pause) {
  184               return false;
  185           }
  186           RtfBasicElement[] rtfElements = rtfDoc.getMapper().mapElement(element);
  187           if(rtfElements.length != 0) {
  188               for(int i = 0; i < rtfElements.length; i++) {
  189                   if(rtfElements[i] != null) {
  190                       rtfDoc.add(rtfElements[i]);
  191                   }
  192               }
  193               return true;
  194           } else {
  195               return false;
  196           }
  197       }
  198       
  199       /**
  200        * Adds a page break
  201        *
  202        * @return <code>false</code>
  203        */
  204       public boolean newPage() {
  205           rtfDoc.add(new RtfNewPage(rtfDoc));
  206           return true;
  207       }
  208   
  209       /**
  210        * Sets the page margins
  211        *
  212        * @param left The left margin
  213        * @param right The right margin
  214        * @param top The top margin
  215        * @param bottom The bottom margin
  216        * @return <code>false</code>
  217        */
  218       public boolean setMargins(float left, float right, float top, float bottom) {
  219           rtfDoc.getDocumentHeader().getPageSetting().setMarginLeft((int) (left * RtfElement.TWIPS_FACTOR));
  220           rtfDoc.getDocumentHeader().getPageSetting().setMarginRight((int) (right * RtfElement.TWIPS_FACTOR));
  221           rtfDoc.getDocumentHeader().getPageSetting().setMarginTop((int) (top * RtfElement.TWIPS_FACTOR));
  222           rtfDoc.getDocumentHeader().getPageSetting().setMarginBottom((int) (bottom * RtfElement.TWIPS_FACTOR));
  223           return true;
  224       }
  225       
  226       /**
  227        * Sets the size of the page
  228        *
  229        * @param rect A Rectangle representing the page
  230        * @return <code>false</code>
  231        */
  232       public boolean setPageSize(Rectangle rect) {
  233           rtfDoc.getDocumentHeader().getPageSetting().setPageSize(rect);
  234           return true;
  235       }
  236       
  237       /**
  238        * Whether to automagically generate table of contents entries when
  239        * adding Chapters or Sections.
  240        * 
  241        * @param autogenerate Whether to automatically generate TOC entries
  242        */
  243       public void setAutogenerateTOCEntries(boolean autogenerate) {
  244           this.rtfDoc.setAutogenerateTOCEntries(autogenerate);
  245       }
  246       
  247       /**
  248        * Gets the RtfDocumentSettings that specify how the rtf document is generated.
  249        * 
  250        * @return The current RtfDocumentSettings.
  251        */
  252       public RtfDocumentSettings getDocumentSettings() {
  253           return this.rtfDoc.getDocumentSettings();
  254       }
  255       
  256       /**
  257        * Adds the complete RTF document to the current RTF document being generated.
  258        * It will parse the font and color tables and correct the font and color references
  259        * so that the imported RTF document retains its formattings.
  260        * 
  261        * @param documentSource The Reader to read the RTF document from.
  262        * @throws IOException On errors reading the RTF document.
  263        * @throws DocumentException On errors adding to this RTF document.
  264        * @since 2.1.0
  265        */
  266       public void importRtfDocument(FileInputStream documentSource) throws IOException, DocumentException {
  267           importRtfDocument(documentSource, null);
  268       }
  269       
  270       /**
  271        * Adds the complete RTF document to the current RTF document being generated.
  272        * It will parse the font and color tables and correct the font and color references
  273        * so that the imported RTF document retains its formattings.
  274        * Uses new RtfParser object.
  275        * 
  276        * (author: Howard Shank)
  277        * 
  278        * @param documentSource The InputStream to read the RTF document from.
  279   	 * @param events The array of event listeners. May be null
  280        * @throws IOException
  281        * @throws DocumentException
  282        * 
  283        * @see RtfParser
  284        * @see RtfParser#importRtfDocument(InputStream, RtfDocument)
  285        * @since 2.0.8
  286        */
  287       public void importRtfDocument(InputStream documentSource, EventListener[] events ) throws IOException, DocumentException {
  288           if(!this.open) {
  289               throw new DocumentException("The document must be open to import RTF documents.");
  290           }
  291       	RtfParser rtfImport = new RtfParser(this.document);
  292       	if(events != null) {
  293       		for(int idx=0;idx<events.length;idx++) {
  294           		rtfImport.addListener(events[idx]);
  295       		}
  296       	}
  297       	rtfImport.importRtfDocument(documentSource, this.rtfDoc);
  298       }
  299       
  300       /**
  301        * Adds a fragment of an RTF document to the current RTF document being generated.
  302        * Since this fragment doesn't contain font or color tables, all fonts and colors
  303        * are mapped to the default font and color. If the font and color mappings are
  304        * known, they can be specified via the mappings parameter.
  305        * 
  306        * @param documentSource The InputStream to read the RTF fragment from.
  307        * @param mappings The RtfImportMappings that contain font and color mappings to apply to the fragment.
  308        * @throws IOException On errors reading the RTF fragment.
  309        * @throws DocumentException On errors adding to this RTF fragment.
  310        * @since 2.1.0
  311        */
  312       public void importRtfFragment(InputStream documentSource, RtfImportMappings mappings) throws IOException, DocumentException {
  313           importRtfFragment(documentSource, mappings, null);
  314       }
  315       
  316       /**
  317        * Adds a fragment of an RTF document to the current RTF document being generated.
  318        * Since this fragment doesn't contain font or color tables, all fonts and colors
  319        * are mapped to the default font and color. If the font and color mappings are
  320        * known, they can be specified via the mappings parameter.
  321        * Uses new RtfParser object.
  322        * 
  323        * (author: Howard Shank)
  324        * 
  325        * @param documentSource The InputStream to read the RTF fragment from.
  326        * @param mappings The RtfImportMappings that contain font and color mappings to apply to the fragment.
  327   	 * @param events The array of event listeners. May be null
  328        * @throws IOException On errors reading the RTF fragment.
  329        * @throws DocumentException On errors adding to this RTF fragment.
  330        * 
  331        * @see RtfImportMappings
  332        * @see RtfParser
  333        * @see RtfParser#importRtfFragment(InputStream, RtfDocument, RtfImportMappings)
  334        * @since 2.0.8
  335        */
  336       public void importRtfFragment(InputStream documentSource, RtfImportMappings mappings, EventListener[] events ) throws IOException, DocumentException {
  337           if(!this.open) {
  338               throw new DocumentException("The document must be open to import RTF fragments.");
  339           }
  340       	RtfParser rtfImport = new RtfParser(this.document);
  341       	if(events != null) {
  342       		for(int idx=0;idx<events.length;idx++) {
  343           		rtfImport.addListener(events[idx]);
  344       		}
  345       	}
  346       	rtfImport.importRtfFragment(documentSource, this.rtfDoc, mappings);
  347       }
  348   }

Save This Page
Home » iText-src-2.1.3 » com.lowagie » text » rtf » [javadoc | source]