Search in sources :

Example 36 with TcPr

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

the class Docx4j_合并单元格_S4_Test method mergeCellsVertically.

/**
 * @Description: 跨行合并
 */
public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {
    if (col < 0 || fromRow < 0 || toRow < 0) {
        return;
    }
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        Tc tc = getTc(tbl, rowIndex, col);
        if (tc == null) {
            break;
        }
        TcPr tcPr = getTcPr(tc);
        VMerge vMerge = tcPr.getVMerge();
        if (vMerge == null) {
            vMerge = new VMerge();
            tcPr.setVMerge(vMerge);
        }
        if (rowIndex == fromRow) {
            vMerge.setVal("restart");
        } else {
            vMerge.setVal("continue");
        }
    }
}
Also used : TcPr(org.docx4j.wml.TcPr) Tc(org.docx4j.wml.Tc) VMerge(org.docx4j.wml.TcPrInner.VMerge)

Example 37 with TcPr

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

the class Docx4j_合并单元格_S4_Test method getTcPr.

public TcPr getTcPr(Tc tc) {
    TcPr tcPr = tc.getTcPr();
    if (tcPr == null) {
        tcPr = new TcPr();
        tc.setTcPr(tcPr);
    }
    return tcPr;
}
Also used : TcPr(org.docx4j.wml.TcPr)

Example 38 with TcPr

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

the class Docx4j_工具类_S3_Test method getTcByPosition.

/**
 *   实现思路:
 *	        主要分在当前行上方插入行和在当前行下方插入行。对首尾2行特殊处理,在有跨行合并情况时,在第一行上面或者在最后一行下面插入是不会跨行的但是可能会跨列。
 *	       对于中间的行,主要参照当前行,如果当前行跨行,则新增行也跨行,如果当前行单元格结束跨行,则新增的上方插入行跨行,下方插入行不跨行,如果当前行单元格开始跨行,则新增的上方插入行不跨行,下发插入行跨行。
 *	       主要思路就是这样,插入的时候需要得到真实位置的单元格,代码如下:
 */
// 按位置得到单元格(考虑跨列合并情况)
public Tc getTcByPosition(List<Tc> tcList, int position) {
    int k = 0;
    for (int i = 0, len = tcList.size(); i < len; i++) {
        Tc tc = tcList.get(i);
        TcPr trPr = tc.getTcPr();
        if (trPr != null) {
            GridSpan gridSpan = trPr.getGridSpan();
            if (gridSpan != null) {
                k += gridSpan.getVal().intValue() - 1;
            }
        }
        if (k >= position) {
            return tcList.get(i);
        }
        k++;
    }
    if (position < tcList.size()) {
        return tcList.get(position);
    }
    return null;
}
Also used : TcPr(org.docx4j.wml.TcPr) Tc(org.docx4j.wml.Tc) GridSpan(org.docx4j.wml.TcPrInner.GridSpan)

Example 39 with TcPr

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

the class Docx4j_工具类_S3_Test method createTable.

/**
 * @Description:创建表格(默认水平居中,垂直居中)
 */
public Tbl createTable(int rowNum, int colsNum, int[] widthArr) throws Exception {
    colsNum = Math.max(1, Math.min(colsNum, widthArr.length));
    rowNum = Math.max(1, rowNum);
    Tbl tbl = new Tbl();
    StringBuffer tblSb = new StringBuffer();
    tblSb.append("<w:tblPr ").append(Namespaces.W_NAMESPACE_DECLARATION).append(">");
    tblSb.append("<w:tblStyle w:val=\"TableGrid\"/>");
    tblSb.append("<w:tblW w:w=\"0\" w:type=\"auto\"/>");
    // 上边框
    tblSb.append("<w:tblBorders>");
    tblSb.append("<w:top w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 左边框
    tblSb.append("<w:left w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 下边框
    tblSb.append("<w:bottom w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    // 右边框
    tblSb.append("<w:right w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideH w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("<w:insideV w:val=\"single\" w:sz=\"1\" w:space=\"0\" w:color=\"auto\"/>");
    tblSb.append("</w:tblBorders>");
    tblSb.append("</w:tblPr>");
    TblPr tblPr = null;
    tblPr = (TblPr) XmlUtils.unmarshalString(tblSb.toString());
    Jc jc = new Jc();
    // 单元格居中对齐
    jc.setVal(JcEnumeration.CENTER);
    tblPr.setJc(jc);
    tbl.setTblPr(tblPr);
    // 设定各单元格宽度
    TblGrid tblGrid = new TblGrid();
    tbl.setTblGrid(tblGrid);
    for (int i = 0; i < colsNum; i++) {
        TblGridCol gridCol = new TblGridCol();
        gridCol.setW(BigInteger.valueOf(widthArr[i]));
        tblGrid.getGridCol().add(gridCol);
    }
    // 新增行
    for (int j = 0; j < rowNum; j++) {
        Tr tr = new Tr();
        tbl.getContent().add(tr);
        // 列
        for (int i = 0; i < colsNum; i++) {
            Tc tc = new Tc();
            tr.getContent().add(tc);
            TcPr tcPr = new TcPr();
            TblWidth cellWidth = new TblWidth();
            cellWidth.setType("dxa");
            cellWidth.setW(BigInteger.valueOf(widthArr[i]));
            tcPr.setTcW(cellWidth);
            tc.setTcPr(tcPr);
            // 垂直居中
            setTcVAlign(tc, STVerticalJc.CENTER);
            P p = new P();
            PPr pPr = new PPr();
            pPr.setJc(jc);
            p.setPPr(pPr);
            R run = new R();
            p.getContent().add(run);
            tc.getContent().add(p);
        }
    }
    return tbl;
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TblPr(org.docx4j.wml.TblPr) Tc(org.docx4j.wml.Tc) P(org.docx4j.wml.P) R(org.docx4j.wml.R) PPr(org.docx4j.wml.PPr) TcPr(org.docx4j.wml.TcPr) 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) Tbl(org.docx4j.wml.Tbl) TblGridCol(org.docx4j.wml.TblGridCol)

Example 40 with TcPr

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

the class WordprocessingMLPackageRender method setCellWidth.

/**
 *  本方法创建一个单元格属性集对象和一个表格宽度对象. 将给定的宽度设置到宽度对象然后将其添加到 属性集对象. 最后将属性集对象设置到单元格中.
 */
public void setCellWidth(Tc tableCell, int width) {
    TcPr tableCellProperties = new TcPr();
    TblWidth tableWidth = new TblWidth();
    tableWidth.setW(BigInteger.valueOf(width));
    tableCellProperties.setTcW(tableWidth);
    tableCell.setTcPr(tableCellProperties);
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr)

Aggregations

TcPr (org.docx4j.wml.TcPr)46 Tc (org.docx4j.wml.Tc)22 TblWidth (org.docx4j.wml.TblWidth)14 BigInteger (java.math.BigInteger)11 VMerge (org.docx4j.wml.TcPrInner.VMerge)10 GridSpan (org.docx4j.wml.TcPrInner.GridSpan)9 CTVerticalJc (org.docx4j.wml.CTVerticalJc)7 Tr (org.docx4j.wml.Tr)7 CTShd (org.docx4j.wml.CTShd)6 P (org.docx4j.wml.P)5 R (org.docx4j.wml.R)5 Text (org.docx4j.wml.Text)4 Tbl (org.docx4j.wml.Tbl)3 TblPr (org.docx4j.wml.TblPr)3 HMerge (org.docx4j.wml.TcPrInner.HMerge)3 BooleanDefaultTrue (org.docx4j.wml.BooleanDefaultTrue)2 CTBorder (org.docx4j.wml.CTBorder)2 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)2 PPr (org.docx4j.wml.PPr)2 TcMar (org.docx4j.wml.TcMar)2