Search in sources :

Example 1 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class HeaderFooterTable method main.

public static void main(String[] args) throws IOException {
    XWPFDocument doc = new XWPFDocument();
    // Create a header with a 1 row, 3 column table
    // changes made for issue 57366 allow a new header or footer
    // to be created empty. This is a change. You will have to add
    // either a paragraph or a table to the header or footer for
    // the document to be considered valid.
    XWPFHeader hdr = doc.createHeader(HeaderFooterType.DEFAULT);
    XWPFTable tbl = hdr.createTable(1, 3);
    // Set the padding around text in the cells to 1/10th of an inch
    int pad = (int) (.1 * 1440);
    tbl.setCellMargins(pad, pad, pad, pad);
    // Set table width to 6.5 inches in 1440ths of a point
    tbl.setWidth((int) (6.5 * 1440));
    // Can not yet set table or cell width properly, tables default to
    // autofit layout, and this requires fixed layout
    CTTbl ctTbl = tbl.getCTTbl();
    CTTblPr ctTblPr = ctTbl.addNewTblPr();
    CTTblLayoutType layoutType = ctTblPr.addNewTblLayout();
    layoutType.setType(STTblLayoutType.FIXED);
    // Now set up a grid for the table, cells will fit into the grid
    // Each cell width is 3120 in 1440ths of an inch, or 1/3rd of 6.5"
    BigInteger w = new BigInteger("3120");
    CTTblGrid grid = ctTbl.addNewTblGrid();
    for (int i = 0; i < 3; i++) {
        CTTblGridCol gridCol = grid.addNewGridCol();
        gridCol.setW(w);
    }
    // Add paragraphs to the cells
    XWPFTableRow row = tbl.getRow(0);
    XWPFTableCell cell = row.getCell(0);
    XWPFParagraph p = cell.getParagraphArray(0);
    XWPFRun r = p.createRun();
    r.setText("header left cell");
    cell = row.getCell(1);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header center cell");
    cell = row.getCell(2);
    p = cell.getParagraphArray(0);
    r = p.createRun();
    r.setText("header right cell");
    // Create a footer with a Paragraph
    XWPFFooter ftr = doc.createFooter(HeaderFooterType.DEFAULT);
    p = ftr.createParagraph();
    r = p.createRun();
    r.setText("footer text");
    OutputStream os = new FileOutputStream(new File("headertable.docx"));
    doc.write(os);
    doc.close();
}
Also used : CTTblGridCol(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGridCol) XWPFParagraph(org.apache.poi.xwpf.usermodel.XWPFParagraph) XWPFTableCell(org.apache.poi.xwpf.usermodel.XWPFTableCell) XWPFTable(org.apache.poi.xwpf.usermodel.XWPFTable) XWPFFooter(org.apache.poi.xwpf.usermodel.XWPFFooter) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) CTTblPr(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr) CTTblLayoutType(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblLayoutType) XWPFHeader(org.apache.poi.xwpf.usermodel.XWPFHeader) XWPFTableRow(org.apache.poi.xwpf.usermodel.XWPFTableRow) XWPFRun(org.apache.poi.xwpf.usermodel.XWPFRun) CTTblGrid(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid) FileOutputStream(java.io.FileOutputStream) BigInteger(java.math.BigInteger) XWPFDocument(org.apache.poi.xwpf.usermodel.XWPFDocument) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) File(java.io.File)

Example 2 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFFootnote method addNewTbl.

/**
     * add a new table to the end of the footnote
     *
     * @param table
     * @return the added XWPFTable
     */
public XWPFTable addNewTbl(CTTbl table) {
    CTTbl newTable = ctFtnEdn.addNewTbl();
    newTable.set(table);
    XWPFTable xTable = new XWPFTable(newTable, this);
    tables.add(xTable);
    return xTable;
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)

Example 3 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFFootnote method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     * @see org.apache.poi.xwpf.usermodel.IBody#getTableCell(CTTc cell)
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    if (tableRow == null) {
        return null;
    }
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 4 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFFootnote method insertTable.

/**
     * inserts an existing XWPFTable to the arrays bodyElements and tables
     *
     * @param pos
     * @param table
     * @see org.apache.poi.xwpf.usermodel.IBody#insertTable(int pos, XWPFTable table)
     */
public void insertTable(int pos, XWPFTable table) {
    bodyElements.add(pos, table);
    int i = 0;
    for (CTTbl tbl : ctFtnEdn.getTblArray()) {
        if (tbl == table.getCTTbl()) {
            break;
        }
        i++;
    }
    tables.add(i, table);
}
Also used : CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)

Example 5 with CTTbl

use of org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl in project poi by apache.

the class XWPFHeaderFooter method getTableCell.

/**
     * get the TableCell which belongs to the TableCell
     *
     * @param cell
     */
public XWPFTableCell getTableCell(CTTc cell) {
    XmlCursor cursor = cell.newCursor();
    cursor.toParent();
    XmlObject o = cursor.getObject();
    if (!(o instanceof CTRow)) {
        cursor.dispose();
        return null;
    }
    CTRow row = (CTRow) o;
    cursor.toParent();
    o = cursor.getObject();
    cursor.dispose();
    if (!(o instanceof CTTbl)) {
        return null;
    }
    CTTbl tbl = (CTTbl) o;
    XWPFTable table = getTable(tbl);
    if (table == null) {
        return null;
    }
    XWPFTableRow tableRow = table.getRow(row);
    return tableRow.getTableCell(cell);
}
Also used : XmlObject(org.apache.xmlbeans.XmlObject) CTTbl(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl) CTRow(org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow) XmlCursor(org.apache.xmlbeans.XmlCursor)

Aggregations

CTTbl (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl)31 XmlCursor (org.apache.xmlbeans.XmlCursor)14 XmlObject (org.apache.xmlbeans.XmlObject)13 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)11 CTRow (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRow)5 BigInteger (java.math.BigInteger)4 CTSdtBlock (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSdtBlock)3 CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)3 InputStream (java.io.InputStream)2 POIXMLException (org.apache.poi.POIXMLException)2 XWPFParagraph (org.apache.poi.xwpf.usermodel.XWPFParagraph)2 XWPFTable (org.apache.poi.xwpf.usermodel.XWPFTable)2 XWPFBorderType (org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType)2 XWPFTableCell (org.apache.poi.xwpf.usermodel.XWPFTableCell)2 XWPFTableRow (org.apache.poi.xwpf.usermodel.XWPFTableRow)2 CTTblBorders (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders)2 CTTblGrid (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblGrid)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1