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

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

Introduction

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

Prototype

@Override
    public void setColumnWidth(int idx, double width) 

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./* ww  w  .j  ava  2s .  c o  m*/
 * @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())));
}

From source file:poi.xslf.usermodel.Tutorial4.java

License:Apache License

public static void main(String[] args) throws IOException {
    XMLSlideShow ppt = new XMLSlideShow();

    // XSLFSlide#createSlide() with no arguments creates a blank slide
    XSLFSlide slide = ppt.createSlide();

    XSLFTable tbl = slide.createTable();
    tbl.setAnchor(new Rectangle2D.Double(50, 50, 450, 300));

    int numColumns = 3;
    int numRows = 5;
    XSLFTableRow headerRow = tbl.addRow();
    headerRow.setHeight(50);/*from  w ww . j a v  a 2s  .co  m*/
    // header
    for (int i = 0; i < numColumns; i++) {
        XSLFTableCell th = headerRow.addCell();
        XSLFTextParagraph p = th.addNewTextParagraph();
        p.setTextAlign(TextAlign.CENTER);
        XSLFTextRun r = p.addNewTextRun();
        r.setText("Header " + (i + 1));
        r.setBold(true);
        r.setFontColor(Color.white);
        th.setFillColor(new Color(79, 129, 189));
        th.setBorderBottom(2);
        th.setBorderBottomColor(Color.white);

        tbl.setColumnWidth(i, 150); // all columns are equally sized
    }

    // rows

    for (int rownum = 0; rownum < numRows; rownum++) {
        XSLFTableRow tr = tbl.addRow();
        tr.setHeight(50);
        // header
        for (int i = 0; i < numColumns; i++) {
            XSLFTableCell cell = tr.addCell();
            XSLFTextParagraph p = cell.addNewTextParagraph();
            XSLFTextRun r = p.addNewTextRun();

            r.setText("Cell " + (i + 1));
            if (rownum % 2 == 0)
                cell.setFillColor(new Color(208, 216, 232));
            else
                cell.setFillColor(new Color(233, 247, 244));

        }

    }

    FileOutputStream out = new FileOutputStream("table.pptx");
    ppt.write(out);
    out.close();
}