Search in sources :

Example 1 with TableRow

use of org.apache.poi.hwpf.usermodel.TableRow in project poi by apache.

the class AbstractWordUtils method buildTableCellEdgesArray.

/**
     * Creates array of all possible cell edges. In HTML (and FO) cells from
     * different rows and same column should have same width, otherwise spanning
     * shall be used.
     * 
     * @param table
     *            table to build cell edges array from
     * @return array of cell edges (including leftest one) in twips
     */
static int[] buildTableCellEdgesArray(Table table) {
    Set<Integer> edges = new TreeSet<Integer>();
    for (int r = 0; r < table.numRows(); r++) {
        TableRow tableRow = table.getRow(r);
        for (int c = 0; c < tableRow.numCells(); c++) {
            TableCell tableCell = tableRow.getCell(c);
            edges.add(Integer.valueOf(tableCell.getLeftEdge()));
            edges.add(Integer.valueOf(tableCell.getLeftEdge() + tableCell.getWidth()));
        }
    }
    Integer[] sorted = edges.toArray(new Integer[edges.size()]);
    int[] result = new int[sorted.length];
    for (int i = 0; i < sorted.length; i++) {
        result[i] = sorted[i].intValue();
    }
    return result;
}
Also used : TableCell(org.apache.poi.hwpf.usermodel.TableCell) TreeSet(java.util.TreeSet) TableRow(org.apache.poi.hwpf.usermodel.TableRow)

Example 2 with TableRow

use of org.apache.poi.hwpf.usermodel.TableRow in project poi by apache.

the class WordToFoConverter method processTable.

protected void processTable(HWPFDocumentCore wordDocument, Element flow, Table table) {
    Element tableHeader = foDocumentFacade.createTableHeader();
    Element tableBody = foDocumentFacade.createTableBody();
    final int[] tableCellEdges = WordToHtmlUtils.buildTableCellEdgesArray(table);
    final int tableRows = table.numRows();
    int maxColumns = Integer.MIN_VALUE;
    for (int r = 0; r < tableRows; r++) {
        maxColumns = Math.max(maxColumns, table.getRow(r).numCells());
    }
    for (int r = 0; r < tableRows; r++) {
        TableRow tableRow = table.getRow(r);
        Element tableRowElement = foDocumentFacade.createTableRow();
        WordToFoUtils.setTableRowProperties(tableRow, tableRowElement);
        // index of current element in tableCellEdges[]
        int currentEdgeIndex = 0;
        final int rowCells = tableRow.numCells();
        for (int c = 0; c < rowCells; c++) {
            TableCell tableCell = tableRow.getCell(c);
            if (tableCell.isVerticallyMerged() && !tableCell.isFirstVerticallyMerged()) {
                currentEdgeIndex += getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
                continue;
            }
            Element tableCellElement = foDocumentFacade.createTableCell();
            WordToFoUtils.setTableCellProperties(tableRow, tableCell, tableCellElement, r == 0, r == tableRows - 1, c == 0, c == rowCells - 1);
            int colSpan = getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
            currentEdgeIndex += colSpan;
            if (colSpan == 0)
                continue;
            if (colSpan != 1)
                tableCellElement.setAttribute("number-columns-spanned", String.valueOf(colSpan));
            final int rowSpan = getNumberRowsSpanned(table, tableCellEdges, r, c, tableCell);
            if (rowSpan > 1)
                tableCellElement.setAttribute("number-rows-spanned", String.valueOf(rowSpan));
            processParagraphes(wordDocument, tableCellElement, tableCell, table.getTableLevel());
            if (!tableCellElement.hasChildNodes()) {
                tableCellElement.appendChild(foDocumentFacade.createBlock());
            }
            tableRowElement.appendChild(tableCellElement);
        }
        if (tableRowElement.hasChildNodes()) {
            if (tableRow.isTableHeader()) {
                tableHeader.appendChild(tableRowElement);
            } else {
                tableBody.appendChild(tableRowElement);
            }
        }
    }
    final Element tableElement = foDocumentFacade.createTable();
    tableElement.setAttribute("table-layout", "fixed");
    if (tableHeader.hasChildNodes()) {
        tableElement.appendChild(tableHeader);
    }
    if (tableBody.hasChildNodes()) {
        tableElement.appendChild(tableBody);
        flow.appendChild(tableElement);
    } else {
        logger.log(POILogger.WARN, "Table without body starting on offset " + table.getStartOffset() + " -- " + table.getEndOffset());
    }
}
Also used : TableCell(org.apache.poi.hwpf.usermodel.TableCell) Element(org.w3c.dom.Element) TableRow(org.apache.poi.hwpf.usermodel.TableRow)

Example 3 with TableRow

use of org.apache.poi.hwpf.usermodel.TableRow in project poi by apache.

the class AbstractWordConverter method getNumberRowsSpanned.

protected int getNumberRowsSpanned(Table table, final int[] tableCellEdges, int currentRowIndex, int currentColumnIndex, TableCell tableCell) {
    if (!tableCell.isFirstVerticallyMerged())
        return 1;
    final int numRows = table.numRows();
    int count = 1;
    for (int r1 = currentRowIndex + 1; r1 < numRows; r1++) {
        TableRow nextRow = table.getRow(r1);
        if (currentColumnIndex >= nextRow.numCells())
            break;
        // we need to skip row if he don't have cells at all
        boolean hasCells = false;
        int currentEdgeIndex = 0;
        for (int c = 0; c < nextRow.numCells(); c++) {
            TableCell nextTableCell = nextRow.getCell(c);
            if (!nextTableCell.isVerticallyMerged() || nextTableCell.isFirstVerticallyMerged()) {
                int colSpan = getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, nextTableCell);
                currentEdgeIndex += colSpan;
                if (colSpan != 0) {
                    hasCells = true;
                    break;
                }
            } else {
                currentEdgeIndex += getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, nextTableCell);
            }
        }
        if (!hasCells)
            continue;
        TableCell nextCell = nextRow.getCell(currentColumnIndex);
        if (!nextCell.isVerticallyMerged() || nextCell.isFirstVerticallyMerged())
            break;
        count++;
    }
    return count;
}
Also used : TableCell(org.apache.poi.hwpf.usermodel.TableCell) TableRow(org.apache.poi.hwpf.usermodel.TableRow)

Example 4 with TableRow

use of org.apache.poi.hwpf.usermodel.TableRow in project poi by apache.

the class WordToHtmlConverter method processTable.

@Override
protected void processTable(HWPFDocumentCore hwpfDocument, Element flow, Table table) {
    Element tableHeader = htmlDocumentFacade.createTableHeader();
    Element tableBody = htmlDocumentFacade.createTableBody();
    final int[] tableCellEdges = AbstractWordUtils.buildTableCellEdgesArray(table);
    final int tableRows = table.numRows();
    int maxColumns = Integer.MIN_VALUE;
    for (int r = 0; r < tableRows; r++) {
        maxColumns = Math.max(maxColumns, table.getRow(r).numCells());
    }
    for (int r = 0; r < tableRows; r++) {
        TableRow tableRow = table.getRow(r);
        Element tableRowElement = htmlDocumentFacade.createTableRow();
        StringBuilder tableRowStyle = new StringBuilder();
        WordToHtmlUtils.addTableRowProperties(tableRow, tableRowStyle);
        // index of current element in tableCellEdges[]
        int currentEdgeIndex = 0;
        final int rowCells = tableRow.numCells();
        for (int c = 0; c < rowCells; c++) {
            TableCell tableCell = tableRow.getCell(c);
            if (tableCell.isVerticallyMerged() && !tableCell.isFirstVerticallyMerged()) {
                currentEdgeIndex += getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
                continue;
            }
            Element tableCellElement;
            if (tableRow.isTableHeader()) {
                tableCellElement = htmlDocumentFacade.createTableHeaderCell();
            } else {
                tableCellElement = htmlDocumentFacade.createTableCell();
            }
            StringBuilder tableCellStyle = new StringBuilder();
            WordToHtmlUtils.addTableCellProperties(tableRow, tableCell, r == 0, r == tableRows - 1, c == 0, c == rowCells - 1, tableCellStyle);
            int colSpan = getNumberColumnsSpanned(tableCellEdges, currentEdgeIndex, tableCell);
            currentEdgeIndex += colSpan;
            if (colSpan == 0) {
                continue;
            }
            if (colSpan != 1) {
                tableCellElement.setAttribute("colspan", String.valueOf(colSpan));
            }
            final int rowSpan = getNumberRowsSpanned(table, tableCellEdges, r, c, tableCell);
            if (rowSpan > 1) {
                tableCellElement.setAttribute("rowspan", String.valueOf(rowSpan));
            }
            processParagraphes(hwpfDocument, tableCellElement, tableCell, table.getTableLevel());
            if (!tableCellElement.hasChildNodes()) {
                tableCellElement.appendChild(htmlDocumentFacade.createParagraph());
            }
            if (tableCellStyle.length() > 0) {
                htmlDocumentFacade.addStyleClass(tableCellElement, tableCellElement.getTagName(), tableCellStyle.toString());
            }
            tableRowElement.appendChild(tableCellElement);
        }
        if (tableRowStyle.length() > 0) {
            tableRowElement.setAttribute("class", htmlDocumentFacade.getOrCreateCssClass("r", tableRowStyle.toString()));
        }
        if (tableRow.isTableHeader()) {
            tableHeader.appendChild(tableRowElement);
        } else {
            tableBody.appendChild(tableRowElement);
        }
    }
    final Element tableElement = htmlDocumentFacade.createTable();
    tableElement.setAttribute("class", htmlDocumentFacade.getOrCreateCssClass("t", "table-layout:fixed;border-collapse:collapse;border-spacing:0;"));
    if (tableHeader.hasChildNodes()) {
        tableElement.appendChild(tableHeader);
    }
    if (tableBody.hasChildNodes()) {
        tableElement.appendChild(tableBody);
        flow.appendChild(tableElement);
    } else {
        logger.log(POILogger.WARN, "Table without body starting at [", Integer.valueOf(table.getStartOffset()), "; ", Integer.valueOf(table.getEndOffset()), ")");
    }
}
Also used : TableCell(org.apache.poi.hwpf.usermodel.TableCell) Element(org.w3c.dom.Element) TableRow(org.apache.poi.hwpf.usermodel.TableRow)

Example 5 with TableRow

use of org.apache.poi.hwpf.usermodel.TableRow in project poi by apache.

the class WordToTextConverter method processTable.

protected void processTable(HWPFDocumentCore wordDocument, Element flow, Table table) {
    final int tableRows = table.numRows();
    for (int r = 0; r < tableRows; r++) {
        TableRow tableRow = table.getRow(r);
        Element tableRowElement = textDocumentFacade.createTableRow();
        final int rowCells = tableRow.numCells();
        for (int c = 0; c < rowCells; c++) {
            TableCell tableCell = tableRow.getCell(c);
            Element tableCellElement = textDocumentFacade.createTableCell();
            if (c != 0)
                tableCellElement.appendChild(textDocumentFacade.createText("\t"));
            processCharacters(wordDocument, table.getTableLevel(), tableCell, tableCellElement);
            tableRowElement.appendChild(tableCellElement);
        }
        tableRowElement.appendChild(textDocumentFacade.createText("\n"));
        flow.appendChild(tableRowElement);
    }
}
Also used : TableCell(org.apache.poi.hwpf.usermodel.TableCell) TableRow(org.apache.poi.hwpf.usermodel.TableRow) Element(org.w3c.dom.Element)

Aggregations

TableCell (org.apache.poi.hwpf.usermodel.TableCell)6 TableRow (org.apache.poi.hwpf.usermodel.TableRow)6 Element (org.w3c.dom.Element)3 TreeSet (java.util.TreeSet)1 PicturesTable (org.apache.poi.hwpf.model.PicturesTable)1 SavedByTable (org.apache.poi.hwpf.model.SavedByTable)1 StyleDescription (org.apache.poi.hwpf.model.StyleDescription)1 CharacterRun (org.apache.poi.hwpf.usermodel.CharacterRun)1 Field (org.apache.poi.hwpf.usermodel.Field)1 Paragraph (org.apache.poi.hwpf.usermodel.Paragraph)1 Picture (org.apache.poi.hwpf.usermodel.Picture)1 Table (org.apache.poi.hwpf.usermodel.Table)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1