1 50 51 package com.lowagie.text.rtf; 52 53 import java.io.IOException ; 54 import java.io.OutputStream ; 55 import java.io.Reader ; 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 75 public class RtfWriter2 extends DocWriter { 76 79 private RtfDocument rtfDoc = null; 80 81 88 protected RtfWriter2(Document doc, OutputStream os) { 89 super(doc, os); 90 doc.addDocListener(this); 91 rtfDoc = new RtfDocument(); 92 } 93 94 101 public static RtfWriter2 getInstance(Document doc, OutputStream os) { 102 return new RtfWriter2(doc, os); 103 } 104 105 110 public void setHeader(HeaderFooter hf) { 111 this.rtfDoc.getDocumentHeader().setHeader(hf); 112 } 113 114 117 public void resetHeader() { 118 this.rtfDoc.getDocumentHeader().setHeader(null); 119 } 120 121 126 public void setFooter(HeaderFooter hf) { 127 this.rtfDoc.getDocumentHeader().setFooter(hf); 128 } 129 130 133 public void resetFooter() { 134 this.rtfDoc.getDocumentHeader().setFooter(null); 135 } 136 137 141 public void setPageCount(int i) {} 142 143 146 public void resetPageCount() {} 147 148 151 public void clearTextWrap() {} 152 153 157 public void open() { 158 super.open(); 159 this.rtfDoc.open(); 160 } 161 162 166 public void close() { 167 if (open) { 168 rtfDoc.writeDocument(os); 169 super.close(); 170 this.rtfDoc = new RtfDocument(); 171 } 172 } 173 174 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 199 public boolean newPage() { 200 rtfDoc.add(new RtfNewPage(rtfDoc)); 201 return true; 202 } 203 204 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 227 public boolean setPageSize(Rectangle rect) { 228 rtfDoc.getDocumentHeader().getPageSetting().setPageSize(rect); 229 return true; 230 } 231 232 238 public void setAutogenerateTOCEntries(boolean autogenerate) { 239 this.rtfDoc.setAutogenerateTOCEntries(autogenerate); 240 } 241 242 251 public void setDataCacheStyle(int dataCacheStyle) { 252 this.rtfDoc.getDocumentSettings().setDataCacheStyle(dataCacheStyle); 253 } 254 255 260 public RtfDocumentSettings getDocumentSettings() { 261 return this.rtfDoc.getDocumentSettings(); 262 } 263 264 273 public void importRtfDocument(Reader documentSource) throws IOException , 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 292 public void importRtfFragment(Reader documentSource, RtfImportMappings mappings) throws IOException , 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 |