Search in sources :

Example 31 with Tr

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

the class Docx4j_工具类_S3_Test method getTblAllTr.

/**
 * @Description: 得到表格所有的行
 */
public List<Tr> getTblAllTr(Tbl tbl) {
    List<Object> objList = getAllElementFromObject(tbl, Tr.class);
    List<Tr> trList = new ArrayList<Tr>();
    if (objList == null) {
        return trList;
    }
    for (Object obj : objList) {
        if (obj instanceof Tr) {
            Tr tr = (Tr) obj;
            trList.add(tr);
        }
    }
    return trList;
}
Also used : ArrayList(java.util.ArrayList) Tr(org.docx4j.wml.Tr)

Example 32 with Tr

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

the class Docx4j_工具类_S3_Test method setTblAllJcAlign.

/**
 * @Description: 设置表格水平对齐方式(包括单元格),只对该方法前面产生的单元格起作用
 */
public void setTblAllJcAlign(Tbl tbl, JcEnumeration jcType) {
    if (jcType != null) {
        setTblJcAlign(tbl, jcType);
        List<Tr> trList = getTblAllTr(tbl);
        for (Tr tr : trList) {
            List<Tc> tcList = getTrAllCell(tr);
            for (Tc tc : tcList) {
                setTcJcAlign(tc, jcType);
            }
        }
    }
}
Also used : Tr(org.docx4j.wml.Tr) Tc(org.docx4j.wml.Tc)

Example 33 with Tr

use of org.docx4j.wml.Tr 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 34 with Tr

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

Example 35 with Tr

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

the class WMLPackageUtils method replaceTable.

public static void replaceTable(String[] placeholders, List<Map<String, String>> textToAdd, WordprocessingMLPackage template) throws Docx4JException, JAXBException {
    List<Tbl> tables = getTargetElements(template.getMainDocumentPart(), Tbl.class);
    // 1. find the table
    Tbl tempTable = getTable(tables, placeholders[0]);
    List<Tr> rows = getTargetElements(tempTable, Tr.class);
    // first row is header, second row is content
    if (rows.size() == 2) {
        // this is our template row
        Tr templateRow = (Tr) rows.get(1);
        for (Map<String, String> replacements : textToAdd) {
            // 2 and 3 are done in this method
            addRowToTable(tempTable, templateRow, replacements);
        }
        // 4. remove the template row
        tempTable.getContent().remove(templateRow);
    }
}
Also used : Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Aggregations

Tr (org.docx4j.wml.Tr)39 Tbl (org.docx4j.wml.Tbl)23 Tc (org.docx4j.wml.Tc)17 P (org.docx4j.wml.P)10 TblPr (org.docx4j.wml.TblPr)7 TcPr (org.docx4j.wml.TcPr)7 File (java.io.File)6 R (org.docx4j.wml.R)6 RPr (org.docx4j.wml.RPr)6 ArrayList (java.util.ArrayList)5 STHint (org.docx4j.wml.STHint)5 BigInteger (java.math.BigInteger)4 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)4 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)4 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)4 TblWidth (org.docx4j.wml.TblWidth)4 Text (org.docx4j.wml.Text)4 ObjectFactory (org.docx4j.wml.ObjectFactory)3 TblGrid (org.docx4j.wml.TblGrid)3 TblGridCol (org.docx4j.wml.TblGridCol)3