1 50 51 package com.lowagie.text.rtf; 52 53 import java.awt.Color ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.IOException ; 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 59 import com.lowagie.text.Cell; 60 import com.lowagie.text.DocumentException; 61 import com.lowagie.text.Element; 62 import com.lowagie.text.Rectangle; 63 import com.lowagie.text.Row; 64 import com.lowagie.text.Table; 65 66 78 public class RtfRow { 79 80 public static final byte[] tableBorder = "brdrs".getBytes(); 81 82 public static final byte[] tableBorderWidth = "brdrw".getBytes(); 83 84 public static final byte[] tableBorderColor = "brdrcf".getBytes(); 85 86 87 private static final byte[] rowBegin = "trowd".getBytes(); 88 89 private static final byte[] rowEnd = "row".getBytes(); 90 91 private static final byte[] rowAutofit = "trautofit1".getBytes(); 92 private static final byte[] graphLeft = "trgaph".getBytes(); 93 94 private static final byte[] rowBorderLeft = "trbrdrl".getBytes(); 95 96 private static final byte[] rowBorderRight = "trbrdrr".getBytes(); 97 98 private static final byte[] rowBorderTop = "trbrdrt".getBytes(); 99 100 private static final byte[] rowBorderBottom = "trbrdrb".getBytes(); 101 102 private static final byte[] rowBorderInlineHorizontal = "trbrdrh".getBytes(); 103 104 private static final byte[] rowBorderInlineVertical = "trbrdrv".getBytes(); 105 106 private static final byte[] rowSpacingLeft = "trspdl".getBytes(); 107 108 private static final byte[] rowSpacingRight = "trspdr".getBytes(); 109 110 private static final byte[] rowSpacingTop = "trspdt".getBytes(); 111 112 private static final byte[] rowSpacingBottom = "trspdb".getBytes(); 113 114 private static final byte[] rowSpacingLeftStyle = "trspdfl3".getBytes(); 115 116 private static final byte[] rowSpacingRightStyle = "trspdfr3".getBytes(); 117 118 private static final byte[] rowSpacingTopStyle = "trspdft3".getBytes(); 119 120 private static final byte[] rowSpacingBottomStyle = "trspdfb3".getBytes(); 121 122 private static final byte[] rowPaddingLeft = "trpaddl".getBytes(); 123 124 private static final byte[] rowPaddingRight = "trpaddr".getBytes(); 125 126 private static final byte[] rowPaddingLeftStyle = "trpaddfl3".getBytes(); 127 128 private static final byte[] rowPaddingRightStyle = "trpaddfr3".getBytes(); 129 130 private static final byte[] rowWidthStyle = "trftsWidth3".getBytes(); 131 132 private static final byte[] rowWidth = "trwWidth".getBytes(); 133 137 private static final byte[] rowHeader = "trhdr".getBytes(); 138 143 private static final byte[] rowKeep = "trkeep".getBytes(); 144 145 private static final byte[] rowAlignLeft = "trql".getBytes(); 146 147 private static final byte[] rowAlignCenter = "trqc".getBytes(); 148 149 private static final byte[] rowAlignRight = "trqr".getBytes(); 150 151 152 private ArrayList cells = new ArrayList (); 153 154 private RtfWriter writer = null; 155 156 private RtfTable mainTable = null; 157 158 159 private int width = 100; 160 162 private int cellpadding = 115; 163 165 private int cellspacing = 14; 166 167 private int borders = 0; 168 169 private java.awt.Color borderColor = null; 170 171 private float borderWidth = 0; 172 173 174 private Row origRow = null; 175 176 183 public RtfRow(RtfWriter writer, RtfTable mainTable) { 184 super(); 185 this.writer = writer; 186 this.mainTable = mainTable; 187 } 188 189 194 public void pregenerateRows(int columns) { 195 for (int i = 0; i < columns; i++) { 196 RtfCell rtfCell = new RtfCell(writer, mainTable); 197 cells.add(rtfCell); 198 } 199 } 200 201 218 public boolean importRow(Row row, float[] propWidths, int tableWidth, int pageWidth, int cellpadding, 219 int cellspacing, int borders, java.awt.Color borderColor, float borderWidth, 220 int y) { 221 this.origRow = row; 224 this.width = pageWidth * tableWidth / 100; 225 this.cellpadding = cellpadding; 226 this.cellspacing = cellspacing; 227 this.borders = borders; 228 this.borderColor = borderColor; 229 this.borderWidth = borderWidth; 230 231 if (this.borderWidth > 2) this.borderWidth = 2; 232 233 int cellLeft = 0; 234 for (int i = 0; i < row.getColumns(); i++) { 235 Element cell = (Element) row.getCell(i); 236 237 int cellWidth = (int) (width * propWidths[i] / 100); 241 if (cell != null) { 242 if (cell.type() == Element.CELL) { 243 RtfCell rtfCell = (RtfCell) cells.get(i); 244 cellLeft = rtfCell.importCell((Cell) cell, cellLeft, cellWidth, i, y, cellpadding); 245 } 246 } else { 247 RtfCell rtfCell = (RtfCell) cells.get(i); 248 cellLeft = rtfCell.importCell(null, cellLeft, cellWidth, i, y, cellpadding); 249 } 250 } 251 252 int columns = row.getColumns(); 258 for (int i = 0; i < columns; i++) { 259 RtfCell firstCell = (RtfCell) cells.get(i); 260 Cell cell = firstCell.getStore(); 261 int cols = 0; 262 if(cell != null) { 263 cols = cell.getColspan(); 264 } 265 if (cols > 1) { 266 RtfCell lastCell = (RtfCell) cells.get(i + cols - 1); 267 firstCell.setCellRight(lastCell.getCellRight()); 268 int width = firstCell.getCellWidth(); 269 for (int j = i + 1; j < i + cols; j++) { 270 RtfCell cCell = (RtfCell) cells.get(j); 271 width += cCell.getCellWidth(); 272 } 273 firstCell.setCellWidth(width); 274 i += cols - 1; 275 } 276 } 277 return true; 278 } 279 280 291 public boolean writeRow(ByteArrayOutputStream os, int rowNum, Table table) throws DocumentException, 292 IOException { 293 os.write(RtfWriter.escape); 294 os.write(rowBegin); 295 os.write((byte) '\n'); 296 os.write(RtfWriter.escape); 297 os.write(rowWidthStyle); 298 os.write(RtfWriter.escape); 299 os.write(rowWidth); 300 writeInt(os, width); 301 if (mainTable.getOriginalTable().isCellsFitPage()) { 304 os.write(RtfWriter.escape); 305 os.write(rowKeep); 306 } 307 if (rowNum < table.getLastHeaderRow() + 1) { 309 os.write(RtfWriter.escape); 310 os.write(rowHeader); 311 } 312 os.write(RtfWriter.escape); 313 switch (this.origRow.getHorizontalAlignment()) { 314 case Element.ALIGN_LEFT: 315 os.write(rowAlignLeft); 316 break; 317 case Element.ALIGN_CENTER: 318 os.write(rowAlignCenter); 319 break; 320 case Element.ALIGN_RIGHT: 321 os.write(rowAlignRight); 322 break; 323 default : 324 os.write(rowAlignLeft); 325 break; 326 } 327 os.write(RtfWriter.escape); 328 os.write(graphLeft); 329 writeInt(os, 10); 330 if (((borders & Rectangle.LEFT) == Rectangle.LEFT) && (borderWidth > 0)) { 331 writeBorder(os, rowBorderLeft); 332 } 333 if (((borders & Rectangle.TOP) == Rectangle.TOP) && (borderWidth > 0)) { 334 writeBorder(os, rowBorderTop); 335 } 336 if (((borders & Rectangle.BOTTOM) == Rectangle.BOTTOM) && (borderWidth > 0)) { 337 writeBorder(os, rowBorderBottom); 338 } 339 if (((borders & Rectangle.RIGHT) == Rectangle.RIGHT) && (borderWidth > 0)) { 340 writeBorder(os, rowBorderRight); 341 } 342 if (((borders & Rectangle.BOX) == Rectangle.BOX) && (borderWidth > 0)) { 343 writeBorder(os, rowBorderInlineHorizontal); 344 writeBorder(os, rowBorderInlineVertical); 345 } 346 347 if (cellspacing > 0) { 348 os.write(RtfWriter.escape); 349 os.write(rowSpacingLeft); 350 writeInt(os, cellspacing / 2); 351 os.write(RtfWriter.escape); 352 os.write(rowSpacingLeftStyle); 353 os.write(RtfWriter.escape); 354 os.write(rowSpacingTop); 355 writeInt(os, cellspacing / 2); 356 os.write(RtfWriter.escape); 357 os.write(rowSpacingTopStyle); 358 os.write(RtfWriter.escape); 359 os.write(rowSpacingBottom); 360 writeInt(os, cellspacing / 2); 361 os.write(RtfWriter.escape); 362 os.write(rowSpacingBottomStyle); 363 os.write(RtfWriter.escape); 364 os.write(rowSpacingRight); 365 writeInt(os, cellspacing / 2); 366 os.write(RtfWriter.escape); 367 os.write(rowSpacingRightStyle); 368 } 369 os.write(RtfWriter.escape); 370 os.write(rowPaddingLeft); 371 writeInt(os, cellpadding / 2); 372 os.write(RtfWriter.escape); 373 os.write(rowPaddingRight); 374 writeInt(os, cellpadding / 2); 375 os.write(RtfWriter.escape); 376 os.write(rowPaddingLeftStyle); 377 os.write(RtfWriter.escape); 378 os.write(rowPaddingRightStyle); 379 os.write((byte) '\n'); 380 381 Iterator cellIterator = cells.iterator(); 382 while (cellIterator.hasNext()) { 383 RtfCell cell = (RtfCell) cellIterator.next(); 384 cell.writeCellSettings(os); 385 } 386 387 os.write(RtfWriter.escape); 388 os.write("intbl".getBytes()); 389 390 cellIterator = cells.iterator(); 391 while (cellIterator.hasNext()) { 392 RtfCell cell = (RtfCell) cellIterator.next(); 393 cell.writeCellContent(os); 394 } 395 os.write(RtfWriter.delimiter); 396 os.write(RtfWriter.escape); 397 os.write(rowEnd); 398 return true; 399 } 400 401 402 private void writeBorder(ByteArrayOutputStream os, byte[] borderType) throws IOException { 403 os.write(RtfWriter.escape); 405 os.write(borderType); 406 os.write(RtfWriter.escape); 408 os.write(RtfRow.tableBorder); 409 os.write(RtfWriter.escape); 411 os.write(RtfRow.tableBorderWidth); 412 writeInt(os, (int) (borderWidth * RtfWriter.TWIPSFACTOR)); 413 os.write(RtfWriter.escape); 415 os.write(RtfRow.tableBorderColor); 416 if (borderColor == null) { 417 writeInt(os, writer.addColor(new Color (0, 0, 0))); 418 } else { 419 writeInt(os, writer.addColor(borderColor)); 420 } 421 os.write((byte) '\n'); 422 } 423 424 425 435 public void setMerge(int x, int mergeType, RtfCell mergeCell) { 436 RtfCell cell = (RtfCell) cells.get(x); 437 cell.setMerge(mergeType, mergeCell); 438 } 439 440 446 private void writeInt(ByteArrayOutputStream out, int i) throws IOException { 447 out.write(Integer.toString(i).getBytes()); 448 } 449 } 450 | Popular Tags |