Search in sources :

Example 1 with STVerticalJc

use of org.docx4j.wml.STVerticalJc in project docx4j-template by vindell.

the class Docx4j_工具类_S3_Test method addTrByIndex.

/**
 * @Description: 在表格指定位置新增一行(默认按表格定义的列数添加)
 */
public void addTrByIndex(Tbl tbl, int index, STVerticalJc vAlign, JcEnumeration hAlign) {
    TblGrid tblGrid = tbl.getTblGrid();
    Tr tr = new Tr();
    if (tblGrid != null) {
        List<TblGridCol> gridList = tblGrid.getGridCol();
        for (TblGridCol tblGridCol : gridList) {
            Tc tc = new Tc();
            setTcWidth(tc, tblGridCol.getW().toString());
            if (vAlign != null) {
                // 垂直居中
                setTcVAlign(tc, vAlign);
            }
            P p = new P();
            if (hAlign != null) {
                PPr pPr = new PPr();
                Jc jc = new Jc();
                // 单元格居中对齐
                jc.setVal(hAlign);
                pPr.setJc(jc);
                p.setPPr(pPr);
            }
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
            tr.getContent().add(tc);
        }
    } else {
        // 大部分情况都不会走到这一步
        Tr firstTr = getTblAllTr(tbl).get(0);
        int cellSize = getTcCellSizeWithMergeNum(firstTr);
        for (int i = 0; i < cellSize; i++) {
            Tc tc = new Tc();
            if (vAlign != null) {
                // 垂直居中
                setTcVAlign(tc, vAlign);
            }
            P p = new P();
            if (hAlign != null) {
                PPr pPr = new PPr();
                Jc jc = new Jc();
                // 单元格居中对齐
                jc.setVal(hAlign);
                pPr.setJc(jc);
                p.setPPr(pPr);
            }
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
            tr.getContent().add(tc);
        }
    }
    if (index >= 0 && index < tbl.getContent().size()) {
        tbl.getContent().add(index, tr);
    } else {
        tbl.getContent().add(tr);
    }
}
Also used : P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) STVerticalJc(org.docx4j.wml.STVerticalJc) Jc(org.docx4j.wml.Jc) CTVerticalJc(org.docx4j.wml.CTVerticalJc) TblGrid(org.docx4j.wml.TblGrid) Tr(org.docx4j.wml.Tr) TblGridCol(org.docx4j.wml.TblGridCol) Tc(org.docx4j.wml.Tc)

Aggregations

CTVerticalJc (org.docx4j.wml.CTVerticalJc)1 Jc (org.docx4j.wml.Jc)1 P (org.docx4j.wml.P)1 PPr (org.docx4j.wml.PPr)1 R (org.docx4j.wml.R)1 STVerticalJc (org.docx4j.wml.STVerticalJc)1 TblGrid (org.docx4j.wml.TblGrid)1 TblGridCol (org.docx4j.wml.TblGridCol)1 Tc (org.docx4j.wml.Tc)1 Tr (org.docx4j.wml.Tr)1