1 50 51 package com.lowagie.text.rtf.headerfooter; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.IOException ; 55 import java.io.OutputStream ; 56 57 import com.lowagie.text.DocumentException; 58 import com.lowagie.text.Element; 59 import com.lowagie.text.HeaderFooter; 60 import com.lowagie.text.Image; 61 import com.lowagie.text.Paragraph; 62 import com.lowagie.text.Phrase; 63 import com.lowagie.text.Table; 64 import com.lowagie.text.rtf.RtfBasicElement; 65 import com.lowagie.text.rtf.document.RtfDocument; 66 import com.lowagie.text.rtf.field.RtfPageNumber; 67 68 69 77 public class RtfHeaderFooter extends HeaderFooter implements RtfBasicElement { 78 79 82 public static final int TYPE_HEADER = 1; 83 86 public static final int TYPE_FOOTER = 2; 87 90 public static final int DISPLAY_FIRST_PAGE = 0; 91 94 public static final int DISPLAY_ALL_PAGES = 1; 95 98 public static final int DISPLAY_LEFT_PAGES = 2; 99 102 public static final int DISPLAY_RIGHT_PAGES = 4; 103 104 107 private static final byte[] HEADER_ALL = "\\header".getBytes(); 108 111 private static final byte[] HEADER_FIRST = "\\headerf".getBytes(); 112 115 private static final byte[] HEADER_LEFT = "\\headerl".getBytes(); 116 119 private static final byte[] HEADER_RIGHT = "\\headerr".getBytes(); 120 123 private static final byte[] FOOTER_ALL = "\\footer".getBytes(); 124 127 private static final byte[] FOOTER_FIRST = "\\footerf".getBytes(); 128 131 private static final byte[] FOOTER_LEFT = "\\footerl".getBytes(); 132 135 private static final byte[] FOOTER_RIGHT = "\\footerr".getBytes(); 136 137 140 private RtfDocument document = null; 141 144 private Object [] content = null; 145 148 private int type = TYPE_HEADER; 149 153 private int displayAt = DISPLAY_ALL_PAGES; 154 155 164 protected RtfHeaderFooter(RtfDocument doc, HeaderFooter headerFooter, int type, int displayAt) { 165 super(new Phrase(""), false); 166 this.document = doc; 167 this.type = type; 168 this.displayAt = displayAt; 169 Paragraph par = new Paragraph(); 170 par.setAlignment(headerFooter.alignment()); 171 if (headerFooter.getBefore() != null) { 172 par.add(headerFooter.getBefore()); 173 } 174 if (headerFooter.isNumbered()) { 175 par.add(new RtfPageNumber(this.document)); 176 } 177 if (headerFooter.getAfter() != null) { 178 par.add(headerFooter.getAfter()); 179 } 180 try { 181 this.content = new Object [1]; 182 if(this.document != null) { 183 this.content[0] = this.document.getMapper().mapElement(par); 184 ((RtfBasicElement) this.content[0]).setInHeader(true); 185 } else { 186 this.content[0] = par; 187 } 188 } catch(DocumentException de) { 189 de.printStackTrace(); 190 } 191 } 192 193 201 protected RtfHeaderFooter(RtfDocument doc, RtfHeaderFooter headerFooter, int displayAt) { 202 super(new Phrase(""), false); 203 this.document = doc; 204 this.content = headerFooter.getContent(); 205 this.displayAt = displayAt; 206 for(int i = 0; i < this.content.length; i++) { 207 if(this.content[i] instanceof Element) { 208 try { 209 this.content[i] = this.document.getMapper().mapElement((Element) this.content[i]); 210 } catch(DocumentException de) { 211 de.printStackTrace(); 212 } 213 } 214 if(this.content[i] instanceof RtfBasicElement) { 215 ((RtfBasicElement) this.content[i]).setInHeader(true); 216 } 217 } 218 } 219 220 226 protected RtfHeaderFooter(RtfDocument doc, HeaderFooter headerFooter) { 227 super(new Phrase(""), false); 228 this.document = doc; 229 Paragraph par = new Paragraph(); 230 par.setAlignment(headerFooter.alignment()); 231 if (headerFooter.getBefore() != null) { 232 par.add(headerFooter.getBefore()); 233 } 234 if (headerFooter.isNumbered()) { 235 par.add(new RtfPageNumber(this.document)); 236 } 237 if (headerFooter.getAfter() != null) { 238 par.add(headerFooter.getAfter()); 239 } 240 try { 241 this.content = new Object [1]; 242 this.content[0] = doc.getMapper().mapElement(par); 243 ((RtfBasicElement) this.content[0]).setInHeader(true); 244 } catch(DocumentException de) { 245 de.printStackTrace(); 246 } 247 } 248 249 254 public RtfHeaderFooter(Element element) { 255 this(new Element[]{element}); 256 } 257 258 263 public RtfHeaderFooter(Element[] elements) { 264 super(new Phrase(""), false); 265 this.content = new Object [elements.length]; 266 for(int i = 0; i < elements.length; i++) { 267 this.content[i] = elements[i]; 268 } 269 } 270 271 276 public void setRtfDocument(RtfDocument doc) { 277 this.document = doc; 278 if(this.document != null) { 279 for(int i = 0; i < this.content.length; i++) { 280 try { 281 if(this.content[i] instanceof Element) { 282 this.content[i] = this.document.getMapper().mapElement((Element) this.content[i]); 283 ((RtfBasicElement) this.content[i]).setInHeader(true); 284 } else if(this.content[i] instanceof RtfBasicElement){ 285 ((RtfBasicElement) this.content[i]).setRtfDocument(this.document); 286 ((RtfBasicElement) this.content[i]).setInHeader(true); 287 } 288 } catch(DocumentException de) { 289 de.printStackTrace(); 290 } 291 } 292 } 293 } 294 295 301 public byte[] write() 302 { 303 ByteArrayOutputStream result = new ByteArrayOutputStream (); 304 try { 305 writeContent(result); 306 } catch(IOException ioe) { 307 ioe.printStackTrace(); 308 } 309 return result.toByteArray(); 310 } 311 314 public void writeContent(final OutputStream result) throws IOException 315 { 316 result.write(OPEN_GROUP); 317 if(this.type == TYPE_HEADER) { 318 if(this.displayAt == DISPLAY_ALL_PAGES) { 319 result.write(HEADER_ALL); 320 } else if(this.displayAt == DISPLAY_FIRST_PAGE) { 321 result.write(HEADER_FIRST); 322 } else if(this.displayAt == DISPLAY_LEFT_PAGES) { 323 result.write(HEADER_LEFT); 324 } else if(this.displayAt == DISPLAY_RIGHT_PAGES) { 325 result.write(HEADER_RIGHT); 326 } 327 } else { 328 if(this.displayAt == DISPLAY_ALL_PAGES) { 329 result.write(FOOTER_ALL); 330 } else if(this.displayAt == DISPLAY_FIRST_PAGE) { 331 result.write(FOOTER_FIRST); 332 } else if(this.displayAt == DISPLAY_LEFT_PAGES) { 333 result.write(FOOTER_LEFT); 334 } else if(this.displayAt == DISPLAY_RIGHT_PAGES) { 335 result.write(FOOTER_RIGHT); 336 } 337 } 338 result.write(DELIMITER); 339 for(int i = 0; i < this.content.length; i++) { 340 if(this.content[i] instanceof RtfBasicElement) { 341 RtfBasicElement rbe = (RtfBasicElement)this.content[i]; 343 rbe.writeContent(result); 344 } 345 } 346 result.write(CLOSE_GROUP); 347 } 348 349 350 355 public void setDisplayAt(int displayAt) { 356 this.displayAt = displayAt; 357 } 358 359 364 public void setType(int type) { 365 this.type = type; 366 } 367 368 373 private Object [] getContent() { 374 return this.content; 375 } 376 377 381 public void setInTable(boolean inTable) { 382 } 383 384 388 public void setInHeader(boolean inHeader) { 389 } 390 391 395 public void setAlignment(int alignment) { 396 super.setAlignment(alignment); 397 for(int i = 0; i < this.content.length; i++) { 398 if(this.content[i] instanceof Paragraph) { 399 ((Paragraph) this.content[i]).setAlignment(alignment); 400 } else if(this.content[i] instanceof Table) { 401 ((Table) this.content[i]).setAlignment(alignment); 402 } else if(this.content[i] instanceof Image) { 403 ((Image) this.content[i]).setAlignment(alignment); 404 } 405 } 406 } 407 } 408 | Popular Tags |