Example usage for org.apache.poi.xslf.usermodel XSLFTable getColumnWidth

List of usage examples for org.apache.poi.xslf.usermodel XSLFTable getColumnWidth

Introduction

In this page you can find the example usage for org.apache.poi.xslf.usermodel XSLFTable getColumnWidth.

Prototype

@Override
    public double getColumnWidth(int idx) 

Source Link

Usage

From source file:com.hp.autonomy.frontend.reports.powerpoint.PowerPointServiceImpl.java

License:MIT License

/**
 * Internal implementation to add a table to a slide.
 * @param slide the slide to add to./*  www .j  a v  a2  s . com*/
 * @param anchor bounding rectangle to draw onto, in PowerPoint coordinates.
 * @param rows number of rows.
 * @param cols number of columns.
 * @param data the data for each cell, laid out row-by-row.
 * @param crop whether we should try and crop the table to the bounding rectangle by removing extra rows.
 *               This doesn't guarantee an exact match, since the font metrics may not exactly match.
 */
private static void addTable(final XSLFSlide slide, final Rectangle2D.Double anchor, final int rows,
        final int cols, final String[] data, final boolean crop) {
    final XSLFTable table = slide.createTable(rows, cols);

    int idx = 0;

    double availWidth = anchor.getWidth();
    double tableW = 0;

    if (cols == 2) {
        // In the most common situation, there's a count column which should be relatively smaller.
        // Make it take 10%, or 70 pixels, whichever is bigger, unless that's more than 50% of the overall space.
        final double minCountWidth = 70;
        final double countColWidth = Math.min(0.5 * availWidth, Math.max(minCountWidth, availWidth * 0.1));
        table.setColumnWidth(0, availWidth - countColWidth);
        table.setColumnWidth(1, countColWidth);
        tableW += table.getColumnWidth(0);
        tableW += table.getColumnWidth(1);
    } else {
        for (int col = 0; col < cols; ++col) {
            table.setColumnWidth(col, availWidth / cols);
            tableW += table.getColumnWidth(col);
        }
    }

    // PowerPoint won't auto-shrink the table for you; and the POI API can't calculate the heights, so we just
    //   have to assume the total row heights add up to be the table height.
    double tableH = 0;

    for (int row = 0; row < rows; ++row) {
        for (int col = 0; col < cols; ++col) {
            final XSLFTableCell cell = table.getCell(row, col);
            cell.setText(data[idx++]);

            for (final TableCell.BorderEdge edge : TableCell.BorderEdge.values()) {
                cell.setBorderColor(edge, Color.BLACK);
            }
        }

        final double nextH = tableH + table.getRowHeight(row);

        if (crop && nextH > anchor.getHeight() && row < rows - 1) {
            // If it doesn't fit, merge all the final row cells together and label them with an ellipsis.
            table.mergeCells(row, row, 0, cols - 1);
            table.getCell(row, 0).setText("\u2026");
            break;
        } else {
            tableH = nextH;
        }
    }

    final double width = Math.min(tableW, availWidth);

    table.setAnchor(new Rectangle2D.Double(anchor.getMinX() + 0.5 * (availWidth - width), anchor.getMinY(),
            width, Math.min(tableH, anchor.getHeight())));
}