Search in sources :

Example 31 with Tbl

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

the class Docx4j_工具类_S3_Test method getAllTbl.

/**
 * @Description:得到所有表格
 */
public List<Tbl> getAllTbl(WordprocessingMLPackage wordMLPackage) {
    MainDocumentPart mainDocPart = wordMLPackage.getMainDocumentPart();
    List<Object> objList = getAllElementFromObject(mainDocPart, Tbl.class);
    if (objList == null) {
        return null;
    }
    List<Tbl> tblList = new ArrayList<Tbl>();
    for (Object obj : objList) {
        if (obj instanceof Tbl) {
            Tbl tbl = (Tbl) obj;
            tblList.add(tbl);
        }
    }
    return tblList;
}
Also used : MainDocumentPart(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart) ArrayList(java.util.ArrayList) Tbl(org.docx4j.wml.Tbl)

Example 32 with Tbl

use of org.docx4j.wml.Tbl 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 33 with Tbl

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

Example 34 with Tbl

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

the class DocxElementWmlRender method newTable.

public Tbl newTable(int row, int cell) {
    // 创建表格对象
    Tbl table = factory.createTbl();
    for (int i = 0; i < row; i++) {
        Tr tableRow = factory.createTr();
        for (int j = 0; j < cell; j++) {
            newCell(tableRow, "");
        }
        table.getContent().add(tableRow);
    }
    return table;
}
Also used : Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Example 35 with Tbl

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

the class SettingColumnWidthForTable method main.

/**
 *  创建一个带边框的表格并添加一行. 然后添加两个带内容的单元格并给定宽度.
 */
public static void main(String[] args) throws Docx4JException {
    wordMLPackage = WordprocessingMLPackage.createPackage();
    factory = Context.getWmlObjectFactory();
    Tbl table = factory.createTbl();
    addBorders(table);
    Tr tr = factory.createTr();
    addTableCellWithWidth(tr, "Field 1", 2500);
    addTableCellWithWidth(tr, "Field 2", 0);
    table.getContent().add(tr);
    wordMLPackage.getMainDocumentPart().addObject(table);
    wordMLPackage.save(new java.io.File("src/main/HelloWord133.docx"));
}
Also used : Tr(org.docx4j.wml.Tr) Tbl(org.docx4j.wml.Tbl)

Aggregations

Tbl (org.docx4j.wml.Tbl)36 Tr (org.docx4j.wml.Tr)23 P (org.docx4j.wml.P)10 Tc (org.docx4j.wml.Tc)9 Test (org.junit.Test)8 File (java.io.File)7 TblPr (org.docx4j.wml.TblPr)7 RPr (org.docx4j.wml.RPr)6 TblBorders (org.docx4j.wml.TblBorders)6 BigInteger (java.math.BigInteger)5 WordprocessingMLPackage (org.docx4j.openpackaging.packages.WordprocessingMLPackage)5 MainDocumentPart (org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart)5 R (org.docx4j.wml.R)5 TblWidth (org.docx4j.wml.TblWidth)5 TblStyle (org.docx4j.wml.CTTblPrBase.TblStyle)4 ObjectFactory (org.docx4j.wml.ObjectFactory)4 STHint (org.docx4j.wml.STHint)4 Text (org.docx4j.wml.Text)4 ArrayList (java.util.ArrayList)3 TrpTableCellType (eu.transkribus.core.model.beans.pagecontent_trp.TrpTableCellType)2