Search in sources :

Example 1 with HSLFTableCell

use of org.apache.poi.hslf.usermodel.HSLFTableCell in project poi by apache.

the class PowerPointExtractor method extractTableText.

private void extractTableText(StringBuffer ret, HSLFTable table) {
    final int nrows = table.getNumberOfRows();
    final int ncols = table.getNumberOfColumns();
    for (int row = 0; row < nrows; row++) {
        for (int col = 0; col < ncols; col++) {
            HSLFTableCell cell = table.getCell(row, col);
            //defensive null checks; don't know if they're necessary
            if (cell != null) {
                String txt = cell.getText();
                txt = (txt == null) ? "" : txt;
                ret.append(txt);
                if (col < ncols - 1) {
                    ret.append('\t');
                }
            }
        }
        ret.append('\n');
    }
}
Also used : HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell)

Example 2 with HSLFTableCell

use of org.apache.poi.hslf.usermodel.HSLFTableCell in project tika by apache.

the class HSLFExtractor method extractTableText.

private void extractTableText(XHTMLContentHandler xhtml, HSLFTable shape) throws SAXException {
    xhtml.startElement("table");
    for (int row = 0; row < shape.getNumberOfRows(); row++) {
        xhtml.startElement("tr");
        for (int col = 0; col < shape.getNumberOfColumns(); col++) {
            HSLFTableCell cell = shape.getCell(row, col);
            //insert empty string for empty cell if cell is null
            String txt = "";
            if (cell != null) {
                txt = cell.getText();
            }
            xhtml.element("td", txt);
        }
        xhtml.endElement("tr");
    }
    xhtml.endElement("table");
}
Also used : HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell)

Example 3 with HSLFTableCell

use of org.apache.poi.hslf.usermodel.HSLFTableCell in project poi by apache.

the class TableDemo method create2ndTable.

static void create2ndTable(HSLFSlide slide) {
    //two rows, one column
    HSLFTable table2 = slide.createTable(2, 1);
    for (int i = 0; i < txt2.length; i++) {
        for (int j = 0; j < txt2[i].length; j++) {
            HSLFTableCell cell = table2.getCell(i, j);
            HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
            rt.setFontSize(10d);
            rt.setFontFamily("Arial");
            if (i == 0) {
                cell.getFill().setForegroundColor(new Color(0, 51, 102));
                rt.setFontColor(Color.white);
                rt.setBold(true);
                rt.setFontSize(14d);
                cell.setHorizontalCentered(true);
            } else {
                rt.getTextParagraph().setBullet(true);
                rt.setFontSize(12d);
                rt.getTextParagraph().setTextAlign(TextAlign.LEFT);
                cell.setHorizontalCentered(false);
            }
            cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            cell.setText(txt2[i][j]);
        }
    }
    table2.setColumnWidth(0, 300);
    table2.setRowHeight(0, 30);
    table2.setRowHeight(1, 70);
    DrawTableShape dts2 = new DrawTableShape(table2);
    dts2.setOutsideBorders(Color.black, 1.0);
    table2.moveTo(200, 400);
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) Color(java.awt.Color) HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell) DrawTableShape(org.apache.poi.sl.draw.DrawTableShape)

Example 4 with HSLFTableCell

use of org.apache.poi.hslf.usermodel.HSLFTableCell in project poi by apache.

the class TableDemo method create1stTable.

static void create1stTable(HSLFSlide slide) {
    //six rows, two columns
    HSLFTable table1 = slide.createTable(6, 2);
    for (int i = 0; i < txt1.length; i++) {
        for (int j = 0; j < txt1[i].length; j++) {
            HSLFTableCell cell = table1.getCell(i, j);
            HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0);
            rt.setFontFamily("Arial");
            rt.setFontSize(10d);
            if (i == 0) {
                cell.getFill().setForegroundColor(new Color(227, 227, 227));
            } else {
                rt.setBold(true);
            }
            cell.setVerticalAlignment(VerticalAlignment.MIDDLE);
            cell.setHorizontalCentered(true);
            cell.setText(txt1[i][j]);
        }
    }
    DrawTableShape dts1 = new DrawTableShape(table1);
    dts1.setAllBorders(1.0, Color.black);
    table1.setColumnWidth(0, 300);
    table1.setColumnWidth(1, 150);
    int pgWidth = slide.getSlideShow().getPageSize().width;
    table1.moveTo((pgWidth - table1.getAnchor().getWidth()) / 2., 100.);
}
Also used : HSLFTextRun(org.apache.poi.hslf.usermodel.HSLFTextRun) HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) Color(java.awt.Color) HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell) DrawTableShape(org.apache.poi.sl.draw.DrawTableShape)

Example 5 with HSLFTableCell

use of org.apache.poi.hslf.usermodel.HSLFTableCell in project poi by apache.

the class TestTable method testShapeFactory.

/**
     * Test that ShapeFactory works properly and returns <code>Table</code>
     */
@Test
public void testShapeFactory() throws IOException {
    HSLFSlideShow ppt = new HSLFSlideShow();
    HSLFSlide slide = ppt.createSlide();
    HSLFTable tbl = slide.createTable(2, 5);
    HSLFTableCell cell = tbl.getCell(0, 0);
    //table cells have type=TextHeaderAtom.OTHER_TYPE, see bug #46033
    assertEquals(TextHeaderAtom.OTHER_TYPE, cell.getTextParagraphs().get(0).getRunType());
    HSLFShape tblSh = slide.getShapes().get(0);
    assertTrue(tblSh instanceof HSLFTable);
    HSLFTable tbl2 = (HSLFTable) tblSh;
    assertEquals(tbl.getNumberOfColumns(), tbl2.getNumberOfColumns());
    assertEquals(tbl.getNumberOfRows(), tbl2.getNumberOfRows());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ppt.write(out);
    out.close();
    ppt.close();
    ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray()));
    slide = ppt.getSlides().get(0);
    assertTrue(slide.getShapes().get(0) instanceof HSLFTable);
    HSLFTable tbl3 = (HSLFTable) slide.getShapes().get(0);
    assertEquals(tbl.getNumberOfColumns(), tbl3.getNumberOfColumns());
    assertEquals(tbl.getNumberOfRows(), tbl3.getNumberOfRows());
    ppt.close();
}
Also used : HSLFTable(org.apache.poi.hslf.usermodel.HSLFTable) HSLFShape(org.apache.poi.hslf.usermodel.HSLFShape) ByteArrayInputStream(java.io.ByteArrayInputStream) HSLFTableCell(org.apache.poi.hslf.usermodel.HSLFTableCell) ByteArrayOutputStream(java.io.ByteArrayOutputStream) HSLFSlideShow(org.apache.poi.hslf.usermodel.HSLFSlideShow) HSLFSlide(org.apache.poi.hslf.usermodel.HSLFSlide) Test(org.junit.Test)

Aggregations

HSLFTableCell (org.apache.poi.hslf.usermodel.HSLFTableCell)5 HSLFTable (org.apache.poi.hslf.usermodel.HSLFTable)3 Color (java.awt.Color)2 HSLFTextRun (org.apache.poi.hslf.usermodel.HSLFTextRun)2 DrawTableShape (org.apache.poi.sl.draw.DrawTableShape)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 HSLFShape (org.apache.poi.hslf.usermodel.HSLFShape)1 HSLFSlide (org.apache.poi.hslf.usermodel.HSLFSlide)1 HSLFSlideShow (org.apache.poi.hslf.usermodel.HSLFSlideShow)1 Test (org.junit.Test)1