Esempio n. 1
0
  /**
   * Compute column widths of the XWPF table.
   *
   * @param table
   * @return
   */
  public static float[] computeColWidths(XWPFTable table) {

    XWPFTableRow firstRow = getFirstRow(table);
    float[] colWidths;
    // Get first row to know if there is cell which have gridSpan to compute
    // columns number.
    int nbCols = getNumberOfColumns(firstRow);

    // Compare nbCols computed with number of grid colList
    CTTblGrid grid = table.getCTTbl().getTblGrid();
    List<CTTblGridCol> cols = grid.getGridColList();
    if (nbCols > cols.size()) {
      Collection<Float> maxColWidths = null;
      Collection<Float> currentColWidths = null;

      // nbCols computed is not equals to number of grid colList
      // columns width must be computed by looping for each row/cells
      List<XWPFTableRow> rows = table.getRows();
      for (XWPFTableRow row : rows) {
        currentColWidths = computeColWidths(row);
        if (maxColWidths == null) {
          maxColWidths = currentColWidths;
        } else {
          if (currentColWidths.size() > maxColWidths.size()) {
            maxColWidths = currentColWidths;
          }
        }
      }

      colWidths = new float[maxColWidths.size()];
      int i = 0;
      for (Float colWidth : maxColWidths) {
        colWidths[i++] = colWidth;
      }
      return colWidths;

    } else {
      // If w:gridAfter is defined, ignore the last columns defined on the gridColumn
      int nbColumnsToIgnoreBefore = getNbColumnsToIgnore(firstRow, true);
      int nbColumnsToIgnoreAfter = getNbColumnsToIgnore(firstRow, false);
      int nbColumns = cols.size() - nbColumnsToIgnoreBefore - nbColumnsToIgnoreAfter;

      // nbCols computed is equals to number of grid colList
      // columns width can be computed by using the grid colList
      colWidths = new float[nbColumns];
      float colWidth = -1;
      for (int i = nbColumnsToIgnoreBefore; i < colWidths.length; i++) {
        CTTblGridCol tblGridCol = cols.get(i);
        colWidth = tblGridCol.getW().floatValue();
        colWidths[i] = dxa2points(colWidth);
      }
    }
    return colWidths;
  }
Esempio n. 2
0
 /**
  * <w:gridCol list should be filtered to ignore negative value.
  *
  * <p>Ex : <w:gridCol w:w="-54" /> should be ignored. See
  * https://code.google.com/p/xdocreport/issues/detail?id=315
  *
  * @param grid
  * @return
  */
 private static List<CTTblGridCol> getGridColList(CTTblGrid grid) {
   List<CTTblGridCol> newCols = new ArrayList<CTTblGridCol>();
   List<CTTblGridCol> cols = grid.getGridColList();
   for (CTTblGridCol col : cols) {
     if (col.getW().floatValue() >= 0) {
       newCols.add(col);
     }
   }
   return newCols;
 }