org.docx4j.wml.TblGrid Java Examples

The following examples show how to use org.docx4j.wml.TblGrid. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @param tableWidthPercent
 *            表格占页面宽度百分比
 * @param widthPercent
 *            各列百分比
 */
public void setTableGridCol(WordprocessingMLPackage wordPackage,
		ObjectFactory factory, Tbl table, double tableWidthPercent,
		double[] widthPercent) throws Exception {
	int width = getWritableWidth(wordPackage);
	int tableWidth = (int) (width * tableWidthPercent / 100);
	TblGrid tblGrid = factory.createTblGrid();
	for (int i = 0; i < widthPercent.length; i++) {
		TblGridCol gridCol = factory.createTblGridCol();
		gridCol.setW(BigInteger.valueOf((long) (tableWidth
				* widthPercent[i] / 100)));
		tblGrid.getGridCol().add(gridCol);
	}
	table.setTblGrid(tblGrid);

	TblPr tblPr = table.getTblPr();
	if (tblPr == null) {
		tblPr = factory.createTblPr();
	}
	TblWidth tblWidth = new TblWidth();
	tblWidth.setType("dxa");// 这一行是必须的,不自己设置宽度默认是auto
	tblWidth.setW(new BigInteger(tableWidth + ""));
	tblPr.setTblW(tblWidth);
	table.setTblPr(tblPr);
}
 
Example #2
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void setTableGridCol(WordprocessingMLPackage wordPackage,ObjectFactory factory,Tbl table,double[] widthPercent) throws Exception{  
    int width=getWritableWidth(wordPackage);  
    TblGrid tblGrid = factory.createTblGrid();  
    for (int i = 0; i <widthPercent.length; i++) {  
        TblGridCol gridCol = factory.createTblGridCol();  
        gridCol.setW(BigInteger.valueOf((long) (width*widthPercent[i]/100)));  
        tblGrid.getGridCol().add(gridCol);  
    }  
    table.setTblGrid(tblGrid);  
      
    TblPr tblPr=table.getTblPr();  
    if(tblPr==null){  
        tblPr=factory.createTblPr();  
    }  
    TblWidth tblWidth=new TblWidth();  
    tblWidth.setType("dxa");//这一行是必须的,不自己设置宽度默认是auto  
    tblWidth.setW(new BigInteger(""+width));  
    tblPr.setTblW(tblWidth);  
    table.setTblPr(tblPr);  
}
 
Example #3
Source File: RoundtripXHTMLImporter.java    From docx-html-editor with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
  protected void setupTblGrid(TableBox cssTable, Tbl tbl, TableProperties tableProperties) {

/*
 * Reinstate
 * 
	context.getWmlPackage().setUserData(id+"#Grid", table.getTblGrid());
	
	as set by SessionAwareAbstractTableWriter
	
 */
String id = cssTable.getElement().getAttribute("id");

if (id==null ) {
	log.debug("no id on table " );
	
} else {
	log.debug("processing table with id " + id);

	Object o = wordMLPackage.getUserData(id+"#Grid");
	if (o==null) {
		
		log.debug("no #Grid UserData on table with id " + id);
	
	} else {
		tbl.setTblGrid((TblGrid)o);
		return;
	}
}

super.setupTblGrid( cssTable,  tbl,  tableProperties);

  }
 
Example #4
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @Description:创建表格(默认水平居中,垂直居中)
 */
public static 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;
}
 
Example #5
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
/**
 * @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;
}