Java Code Examples for org.docx4j.wml.Tc#setTcPr()

The following examples show how to use org.docx4j.wml.Tc#setTcPr() . 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: TableWithMergedCells.java    From docx4j-template with Apache License 2.0 8 votes vote down vote up
/**
 *  我们创建一个单元格和单元格属性对象.
 *  也创建了一个纵向合并对象. 如果合并值不为null, 将它设置到合并对象中. 然后将该对象添加到
 *  单元格属性并将属性添加到单元格中. 最后设置单元格内容并将单元格添加到行中.
 *  
 *  如果合并值为'restart', 表明要开始一个新行. 如果为null, 继续按前面的行处理, 也就是合并单元格.
 */
private static void addMergedCell(Tr row, String content, String vMergeVal) {
    Tc tableCell = factory.createTc();
    TcPr tableCellProperties = new TcPr();
 
    VMerge merge = new VMerge();
    if(vMergeVal != null){
        merge.setVal(vMergeVal);
    }
    tableCellProperties.setVMerge(merge);
 
    tableCell.setTcPr(tableCellProperties);
    if(content != null) {
            tableCell.getContent().add(
            wordMLPackage.getMainDocumentPart().
                createParagraphOfText(content));
    }
 
    row.getContent().add(tableCell);
}
 
Example 2
Source File: WordprocessingMLPackageRender.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public void addMergedCell(Tr row, String content, String vMergeVal) {  
    Tc tableCell = factory.createTc();  
    TcPr tableCellProperties = new TcPr();  
   
    VMerge merge = new VMerge();  
    if(vMergeVal != null){  
        merge.setVal(vMergeVal);  
    }  
    tableCellProperties.setVMerge(merge);  
   
    tableCell.setTcPr(tableCellProperties);  
    if(content != null) {  
    	tableCell.getContent().add(wmlPackage.getMainDocumentPart(). createParagraphOfText(content));  
    }  
   
    row.getContent().add(tableCell);  
}
 
Example 3
Source File: DocxBuilder.java    From TranskribusCore with GNU General Public License v3.0 6 votes vote down vote up
private void applyGridSpan( final Tc cell, final int colSpan, final String rowSpan, int w, boolean mergedVertical ) {
	
    TcPr tcPr = factory.createTcPr();
    TblWidth tblWidth = factory.createTblWidth();
    tblWidth.setType( "dxa" );
    tblWidth.setW( BigInteger.valueOf( w*colSpan ) );
    tcPr.setTcW( tblWidth  );
    
    if ( colSpan > 1) {
        GridSpan gridSpan = factory.createTcPrInnerGridSpan();
        gridSpan.setVal(BigInteger.valueOf(colSpan));
        tcPr.setGridSpan(gridSpan);
    }
    
    if ( mergedVertical ) {
    	//logger.debug(" this is vertical span");
    	VMerge gridVSpan = factory.createTcPrInnerVMerge();
    	if (rowSpan != null)
    		gridVSpan.setVal(rowSpan);
        tcPr.setVMerge(gridVSpan);
                    
    }
   
    cell.setTcPr(tcPr);
}
 
Example 4
Source File: WordprocessingMLPackageRender.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
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);  
}
 
Example 5
Source File: Docx4J_例子2.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTableCell(ObjectFactory factory,
		WordprocessingMLPackage wordMLPackage, Tr tableRow, String content,
		RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,
		String backgroudColor) {
	Tc tableCell = factory.createTc();
	P p = factory.createP();
	setParagraphSpacing(factory, p, jcEnumeration, true, "0", "0", null,
			null, true, "240", STLineSpacingRule.AUTO);
	Text t = factory.createText();
	t.setValue(content);
	R run = factory.createR();
	// 设置表格内容字体样式
	run.setRPr(rpr);

	TcPr tcPr = tableCell.getTcPr();
	if (tcPr == null) {
		tcPr = factory.createTcPr();
	}

	CTVerticalJc valign = factory.createCTVerticalJc();
	valign.setVal(STVerticalJc.CENTER);
	tcPr.setVAlign(valign);

	run.getContent().add(t);
	p.getContent().add(run);
	tableCell.getContent().add(p);
	if (hasBgColor) {
		CTShd shd = tcPr.getShd();
		if (shd == null) {
			shd = factory.createCTShd();
		}
		shd.setColor("auto");
		shd.setFill(backgroudColor);
		tcPr.setShd(shd);
	}
	tableCell.setTcPr(tcPr);
	tableRow.getContent().add(tableCell);
}
 
Example 6
Source File: SettingColumnWidthForTable.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 *  本方法创建一个单元格属性集对象和一个表格宽度对象. 将给定的宽度设置到宽度对象然后将其添加到
 *  属性集对象. 最后将属性集对象设置到单元格中.
 */
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);
}
 
Example 7
Source File: Docx4J_简单例子.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void addTableCell(ObjectFactory factory,
		WordprocessingMLPackage wordMLPackage, Tr tableRow, String content,
		RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,
		String backgroudColor) {
	Tc tableCell = factory.createTc();
	P p = factory.createP();
	setParagraphAlign(factory, p, jcEnumeration);
	Text t = factory.createText();
	t.setValue(content);
	R run = factory.createR();
	// 设置表格内容字体样式
	run.setRPr(rpr);
	run.getContent().add(t);
	p.getContent().add(run);
	tableCell.getContent().add(p);

	if (hasBgColor) {
		TcPr tcPr = tableCell.getTcPr();
		if (tcPr == null) {
			tcPr = factory.createTcPr();
		}
		CTShd shd = tcPr.getShd();
		if (shd == null) {
			shd = factory.createCTShd();
		}
		shd.setColor("auto");
		shd.setFill(backgroudColor);
		tcPr.setShd(shd);
		tableCell.setTcPr(tcPr);
	}
	tableRow.getContent().add(tableCell);
}
 
Example 8
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 setupTcPr(TableCellBox tcb, Tc tc, TableProperties tableProperties) {

/*
 * Reinstate
 * 
	context.getWmlPackage().setUserData(cellId+"#Pr", cell.getTcPr() );
	
	as set by SessionAwareAbstractTableWriter
	
 */

String id = tcb.getElement().getAttribute("id");

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

	Object o = wordMLPackage.getUserData(id+"#Pr");
	if (o==null) {
		
		log.debug("no #Pr UserData on tc with id " + id);
	
	} else {
		tc.setTcPr((TcPr)o);
		return;
	}
}

super.setupTcPr( tcb,  tc,  tableProperties);
  }
 
Example 9
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 10
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;
}
 
Example 11
Source File: Docx4J_简单例子2.java    From docx4j-template with Apache License 2.0 4 votes vote down vote up
public void addTableCell(ObjectFactory factory,  
        WordprocessingMLPackage wordMLPackage, Tr tableRow, String content,  
        RPr rpr, JcEnumeration jcEnumeration, boolean hasBgColor,  
        String backgroudColor) {  
    Tc tableCell = factory.createTc();  
    P p = factory.createP();  
    setParagraphAlign(factory, p, jcEnumeration);  
    Text t = factory.createText();  
    t.setValue(content);  
    R run = factory.createR();  
    // 设置表格内容字体样式  
    run.setRPr(rpr);  
      
    TcPr tcPr = tableCell.getTcPr();  
    if (tcPr == null) {  
        tcPr = factory.createTcPr();  
    }  
      
    CTVerticalJc valign = factory.createCTVerticalJc();  
    valign.setVal(STVerticalJc.CENTER);  
    tcPr.setVAlign(valign);  
      
    run.getContent().add(t);  
    p.getContent().add(run);  
      
    PPr ppr=p.getPPr();  
    if(ppr==null){  
        ppr=factory.createPPr();  
    }  
    //设置段后距离  
    Spacing spacing=new Spacing();  
    spacing.setAfter(new BigInteger("0"));  
    spacing.setLineRule(STLineSpacingRule.AUTO);  
    ppr.setSpacing(spacing);  
    p.setPPr(ppr);  
      
    tableCell.getContent().add(p);  
    if (hasBgColor) {  
        CTShd shd = tcPr.getShd();  
        if (shd == null) {  
            shd = factory.createCTShd();  
        }  
        shd.setColor("auto");  
        shd.setFill(backgroudColor);  
        tcPr.setShd(shd);  
        tableCell.setTcPr(tcPr);  
    }  
    tableRow.getContent().add(tableCell);  
}