1 50 51 package com.lowagie.text.rtf.document; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.HeaderFooter; 58 import com.lowagie.text.rtf.RtfElement; 59 import com.lowagie.text.rtf.document.output.RtfNilOutputStream; 60 import com.lowagie.text.rtf.headerfooter.RtfHeaderFooter; 61 import com.lowagie.text.rtf.headerfooter.RtfHeaderFooterGroup; 62 import com.lowagie.text.rtf.list.RtfList; 63 import com.lowagie.text.rtf.list.RtfListTable; 64 import com.lowagie.text.rtf.style.RtfColor; 65 import com.lowagie.text.rtf.style.RtfColorList; 66 import com.lowagie.text.rtf.style.RtfFont; 67 import com.lowagie.text.rtf.style.RtfFontList; 68 import com.lowagie.text.rtf.style.RtfParagraphStyle; 69 import com.lowagie.text.rtf.style.RtfStylesheetList; 70 71 72 80 public class RtfDocumentHeader extends RtfElement { 81 84 private static final byte[] TITLE_PAGE = "\\titlepg".getBytes(); 85 88 private static final byte[] FACING_PAGES = "\\facingp".getBytes(); 89 90 93 private RtfCodePage codePage = null; 94 97 private RtfColorList colorList = null; 98 101 private RtfFontList fontList = null; 102 105 private RtfListTable listTable = null; 106 109 private RtfStylesheetList stylesheetList = null; 110 113 private RtfInfoGroup infoGroup = null; 114 117 private RtfPageSetting pageSetting = null; 118 121 private HeaderFooter header = null; 122 125 private HeaderFooter footer = null; 126 127 132 protected RtfDocumentHeader(RtfDocument doc) { 133 super(doc); 134 } 135 136 139 protected void init() { 140 this.codePage = new RtfCodePage(this.document); 141 this.colorList = new RtfColorList(this.document); 142 this.fontList = new RtfFontList(this.document); 143 this.listTable = new RtfListTable(this.document); 144 this.stylesheetList = new RtfStylesheetList(this.document); 145 this.infoGroup = new RtfInfoGroup(this.document); 146 this.pageSetting = new RtfPageSetting(this.document); 147 this.header = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_HEADER); 148 this.footer = new RtfHeaderFooterGroup(this.document, RtfHeaderFooter.TYPE_FOOTER); 149 } 150 151 157 public byte[] write() { 158 ByteArrayOutputStream result = new ByteArrayOutputStream (); 159 try { 160 writeContent(result); 161 } catch(IOException ioe) { 162 ioe.printStackTrace(); 163 } 164 return result.toByteArray(); 165 } 166 169 public void writeContent(final OutputStream result) throws IOException 170 { 171 try { 172 writeSectionDefinition(new RtfNilOutputStream()); 176 177 185 this.codePage.writeDefinition(result); 186 this.fontList.writeDefinition(result); 187 this.colorList.writeDefinition(result); 188 this.stylesheetList.writeDefinition(result); 189 this.listTable.writeDefinition(result); 190 this.infoGroup.writeContent(result); 191 this.pageSetting.writeDefinition(result); 192 193 writeSectionDefinition(result); 195 } catch(IOException ioe) { 196 ioe.printStackTrace(); 197 } 198 } 199 200 206 public byte[] writeSectionDefinition() 207 { 208 ByteArrayOutputStream result = new ByteArrayOutputStream (); 209 writeSectionDefinition(result); 210 return result.toByteArray(); 211 } 212 216 public void writeSectionDefinition(final OutputStream result) 217 { 218 try { 219 RtfHeaderFooterGroup header = convertHeaderFooter(this.header, RtfHeaderFooter.TYPE_HEADER); 220 RtfHeaderFooterGroup footer = convertHeaderFooter(this.footer, RtfHeaderFooter.TYPE_FOOTER); 221 if(header.hasTitlePage() || footer.hasTitlePage()) { 222 result.write(TITLE_PAGE); 223 header.setHasTitlePage(); 224 footer.setHasTitlePage(); 225 } 226 if(header.hasFacingPages() || footer.hasFacingPages()) { 227 result.write(FACING_PAGES); 228 header.setHasFacingPages(); 229 footer.setHasFacingPages(); 230 } 231 footer.writeContent(result); 233 header.writeContent(result); 235 pageSetting.writeSectionDefinition(result); 237 } catch(IOException ioe) { 238 ioe.printStackTrace(); 239 } 240 } 241 242 248 public int getFontNumber(RtfFont font) { 249 return this.fontList.getFontNumber(font); 250 } 251 252 258 public int getColorNumber(RtfColor color) { 259 return this.colorList.getColorNumber(color); 260 } 261 262 268 public int getListNumber(RtfList list) { 269 return this.listTable.getListNumber(list); 270 } 271 272 278 public RtfParagraphStyle getRtfParagraphStyle(String styleName) { 279 return this.stylesheetList.getRtfParagraphStyle(styleName); 280 } 281 282 287 public void freeListNumber(RtfList list) { 288 this.listTable.freeListNumber(list); 289 } 290 291 296 public RtfPageSetting getPageSetting() { 297 return this.pageSetting; 298 } 299 300 305 public void addInfoElement(RtfInfoElement rtfInfoElement) { 306 this.infoGroup.add(rtfInfoElement); 307 } 308 309 314 public void setHeader(HeaderFooter header) { 315 this.header = header; 316 } 317 318 323 public void setFooter(HeaderFooter footer) { 324 this.footer = footer; 325 } 326 327 332 public void registerParagraphStyle(RtfParagraphStyle rtfParagraphStyle) { 333 this.stylesheetList.registerParagraphStyle(rtfParagraphStyle); 334 } 335 336 346 private RtfHeaderFooterGroup convertHeaderFooter(HeaderFooter hf, int type) { 347 if(hf != null) { 348 if(hf instanceof RtfHeaderFooterGroup) { 349 return new RtfHeaderFooterGroup(this.document, (RtfHeaderFooterGroup) hf, type); 350 } else if(hf instanceof RtfHeaderFooter) { 351 return new RtfHeaderFooterGroup(this.document, (RtfHeaderFooter) hf, type); 352 } else { 353 return new RtfHeaderFooterGroup(this.document, hf, type); 354 } 355 } else { 356 return new RtfHeaderFooterGroup(this.document, type); 357 } 358 } 359 } 360 | Popular Tags |