Hello Bruno!
 
Up till now Table only offered the possibility to keep the content of single rows together on one page (cellsFitPage). But in my project there is also need to extend that capability to several rows that make up a group.
 
Therefore I have done the following modifications:
 
a) com.lowagie.text.Cell:
 
 Added class variable
 
/** Does this <CODE>Cell</CODE> force a group change? */
 protected boolean groupChange = true;
 
and 2 methods
 
/**
 * Does this <CODE>Cell</CODE> force a group change?
 *
 * @return a value
 */
 public boolean groupChange() {
  return groupChange;
 }
 
/**
 * Sets group change.
 *
 * @param value the new value
 */
 public void setGroupChange(boolean value) {
  groupChange = value;
 }
 
 
 
 
b) com.lowagie.text.pdf.PdfCell:
 
Added class variable
 
/** This is the number of the group the cell is in. */
 private int groupnumber;
 
and 2 methods
 
/**
 * Gets the number of the group this cell is in..
 *
 * @return a number
 */
  public int groupnumber() {
  return groupnumber;
 }
 
/**
 * Sets the group number.
 */
  void setGroupNumber(int number) {
  groupnumber = number;
 }
 
 
 
 
c) com.lowagie.text.pdf.PdfTable: determine groupNumber/groupChange
 
 // constructors
 
/**
 * Constructs a <CODE>PdfTable</CODE>-object.
 *
 * @param table a <CODE>Table</CODE>
 * @param left the left border on the page
 * @param right the right border on the page
 * @param top  the start position of the top of the table
 */
 
 PdfTable(Table table, float left, float right, float top) {
  // constructs a Rectangle (the bottomvalue will be changed afterwards)
  super(left, top, right, top);
  
  // copying the attributes from class Table
  setBorder(table.border());
  setBorderWidth(table.borderWidth());
  setBorderColor(table.borderColor());
  setBackgroundColor(table.backgroundColor());
  setGrayFill(table.grayFill());
  this.columns = table.columns();
  this.cellpadding = table.cellpadding();
  this.cellspacing = table.cellspacing();
  float[] positions = table.getWidths(left, right - left);
  
  // initialisation of some parameters
  setLeft(positions[0]);
  setRight(positions[positions.length - 1]);
  
  Row row;
  int rowNumber = 0;
  int groupNumber = 0;
  boolean groupChange;
  int firstDataRow = table.firstDataRow();
  Cell cell;
  PdfCell currentCell;
  headercells = new ArrayList();
  cells = new ArrayList();
  int rows = table.size() + 1;
  float[] offsets = new float[rows];
  for (int i = 0; i < rows; i++) {
   offsets[i] = top;
  }
  
  // loop over all the rows
  for (Iterator rowIterator = table.iterator(); rowIterator.hasNext(); ) {
   groupChange = false;
   row = (Row) rowIterator.next();
   if (row.isEmpty()) {
    if (rowNumber < rows - 1 && offsets[rowNumber + 1] > offsets[rowNumber]) offsets[rowNumber + 1] = offsets[rowNumber];
   }
   else {
    for(int i = 0; i < row.columns(); i++) {
     cell = (Cell) row.getCell(i);
     if (cell != null) {
      currentCell = new PdfCell(cell, rowNumber, positions[i], positions[i + cell.colspan()], offsets[rowNumber], cellspacing, cellpadding);                       
      try {
       if (offsets[rowNumber] - currentCell.height() - cellpadding < offsets[rowNumber + currentCell.rowspan()]) {
        offsets[rowNumber + currentCell.rowspan()] = offsets[rowNumber] - currentCell.height() - cellpadding;
       }
      }
      catch(ArrayIndexOutOfBoundsException aioobe) {
       if (offsets[rowNumber] - currentCell.height() < offsets[rows - 1]) {
        offsets[rows - 1] = offsets[rowNumber] - currentCell.height();
       }
      }
      if (rowNumber < firstDataRow) {
       currentCell.setHeader();
       headercells.add(currentCell);
      }
      currentCell.setGroupNumber(groupNumber);
      groupChange |= cell.groupChange();
      cells.add(currentCell);
     }
    }
   }
   rowNumber++;
   if( groupChange ) groupNumber++;
  }
  
  // loop over all the cells
  int n = cells.size();
  for (int i = 0; i < n; i++) {
   currentCell = (PdfCell) cells.get(i);
   try {
    currentCell.setBottom(offsets[currentCell.rownumber() + currentCell.rowspan()]);
   }
   catch(ArrayIndexOutOfBoundsException aioobe) {
    currentCell.setBottom(offsets[rows - 1]);
   }
  }
  setBottom(offsets[rows - 1]);
 }
 
 
 
 
d) com.lowagie.text.pdf.PdfDocument.add(Element) method adjusted:
 
currentRownumber replaced by currentGroupNumber
cell.rownumber() replaced by cell.groupnumber()
 
 
 
Hope these changes can be included in next release.
 
 
Thanks and kind regards,
Gerald.

Reply via email to