Search in sources :

Example 16 with TblWidth

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

the class Docx4j_工具类_S3_Test method setTableCellMargin.

/**
 * @Description: 设置单元格Margin
 */
public void setTableCellMargin(Tbl tbl, String top, String right, String bottom, String left) {
    TblPr tblPr = getTblPr(tbl);
    CTTblCellMar cellMar = tblPr.getTblCellMar();
    if (cellMar == null) {
        cellMar = new CTTblCellMar();
        tblPr.setTblCellMar(cellMar);
    }
    if (StringUtils.isNotBlank(top)) {
        TblWidth topW = new TblWidth();
        topW.setW(new BigInteger(top));
        topW.setType("dxa");
        cellMar.setTop(topW);
    }
    if (StringUtils.isNotBlank(right)) {
        TblWidth rightW = new TblWidth();
        rightW.setW(new BigInteger(right));
        rightW.setType("dxa");
        cellMar.setRight(rightW);
    }
    if (StringUtils.isNotBlank(bottom)) {
        TblWidth btW = new TblWidth();
        btW.setW(new BigInteger(bottom));
        btW.setType("dxa");
        cellMar.setBottom(btW);
    }
    if (StringUtils.isNotBlank(left)) {
        TblWidth leftW = new TblWidth();
        leftW.setW(new BigInteger(left));
        leftW.setType("dxa");
        cellMar.setLeft(leftW);
    }
}
Also used : TblWidth(org.docx4j.wml.TblWidth) BigInteger(java.math.BigInteger) TblPr(org.docx4j.wml.TblPr) CTTblCellMar(org.docx4j.wml.CTTblCellMar)

Example 17 with TblWidth

use of org.docx4j.wml.TblWidth 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 18 with TblWidth

use of org.docx4j.wml.TblWidth 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)

Example 19 with TblWidth

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

the class Docx4j_工具类_S3_Test method setTcWidth.

/**
 * @Description: 设置单元格宽度
 */
public void setTcWidth(Tc tc, String width) {
    if (StringUtils.isNotBlank(width)) {
        TcPr tcPr = getTcPr(tc);
        TblWidth tcW = tcPr.getTcW();
        if (tcW == null) {
            tcW = new TblWidth();
            tcPr.setTcW(tcW);
        }
        tcW.setW(new BigInteger(width));
        tcW.setType("dxa");
    }
}
Also used : TblWidth(org.docx4j.wml.TblWidth) TcPr(org.docx4j.wml.TcPr) BigInteger(java.math.BigInteger)

Example 20 with TblWidth

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

the class SettingColumnWidthForTable method setCellWidth.

/**
 *  本方法创建一个单元格属性集对象和一个表格宽度对象. 将给定的宽度设置到宽度对象然后将其添加到
 *  属性集对象. 最后将属性集对象设置到单元格中.
 */
private static 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

TblWidth (org.docx4j.wml.TblWidth)20 TcPr (org.docx4j.wml.TcPr)14 BigInteger (java.math.BigInteger)10 TblPr (org.docx4j.wml.TblPr)8 Tc (org.docx4j.wml.Tc)7 Tbl (org.docx4j.wml.Tbl)5 Tr (org.docx4j.wml.Tr)4 TblGrid (org.docx4j.wml.TblGrid)3 TblGridCol (org.docx4j.wml.TblGridCol)3 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)2 STHint (org.docx4j.wml.STHint)2 TcMar (org.docx4j.wml.TcMar)2 GridSpan (org.docx4j.wml.TcPrInner.GridSpan)2 VMerge (org.docx4j.wml.TcPrInner.VMerge)2 TrPr (org.docx4j.wml.TrPr)2 File (java.io.File)1 CTTblCellMar (org.docx4j.wml.CTTblCellMar)1 CTVerticalJc (org.docx4j.wml.CTVerticalJc)1 Jc (org.docx4j.wml.Jc)1 ObjectFactory (org.docx4j.wml.ObjectFactory)1