org.docx4j.wml.TcPr Java Examples

The following examples show how to use org.docx4j.wml.TcPr. 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: 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 #3
Source File: Docx4j_合并单元格_S4_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/** 
 * @Description: 跨行合并 
 */  
public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {  
    if (col < 0 || fromRow < 0 || toRow < 0) {  
        return;  
    }  
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {  
        Tc tc = getTc(tbl, rowIndex, col);  
        if (tc == null) {  
            break;  
        }  
        TcPr tcPr = getTcPr(tc);  
        VMerge vMerge = tcPr.getVMerge();  
        if (vMerge == null) {  
            vMerge = new VMerge();  
            tcPr.setVMerge(vMerge);  
        }  
        if (rowIndex == fromRow) {  
            vMerge.setVal("restart");  
        } else {  
            vMerge.setVal("continue");  
        }  
    }  
}
 
Example #4
Source File: Docx4j_替换模板.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/** 
 * @Description: 跨行合并 
 */  
public static void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {  
    if (col < 0 || fromRow < 0 || toRow < 0) {  
        return;  
    }  
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {  
        Tc tc = getTc(tbl, rowIndex, col);  
        if (tc == null) {  
            break;  
        }  
        TcPr tcPr = getTcPr(tc);  
        VMerge vMerge = tcPr.getVMerge();  
        if (vMerge == null) {  
            vMerge = factory.createTcPrInnerVMerge();  
            tcPr.setVMerge(vMerge);  
        }  
        if (rowIndex == fromRow) {  
            vMerge.setVal("restart");  
        } else {  
            vMerge.setVal("continue");  
        }  
    }  
}
 
Example #5
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 *   实现思路:
     主要分在当前行上方插入行和在当前行下方插入行。对首尾2行特殊处理,在有跨行合并情况时,在第一行上面或者在最后一行下面插入是不会跨行的但是可能会跨列。
    对于中间的行,主要参照当前行,如果当前行跨行,则新增行也跨行,如果当前行单元格结束跨行,则新增的上方插入行跨行,下方插入行不跨行,如果当前行单元格开始跨行,则新增的上方插入行不跨行,下发插入行跨行。
    主要思路就是这样,插入的时候需要得到真实位置的单元格,代码如下:
 */
// 按位置得到单元格(考虑跨列合并情况)  
public Tc getTcByPosition(List<Tc> tcList, int position) {  
    int k = 0;  
    for (int i = 0, len = tcList.size(); i < len; i++) {  
        Tc tc = tcList.get(i);  
        TcPr trPr = tc.getTcPr();  
        if (trPr != null) {  
            GridSpan gridSpan = trPr.getGridSpan();  
            if (gridSpan != null) {  
                k += gridSpan.getVal().intValue() - 1;  
            }  
        }  
        if (k >= position) {  
            return tcList.get(i);  
        }  
        k++;  
    }  
    if (position < tcList.size()) {  
        return tcList.get(position);  
    }  
    return null;  
}
 
Example #6
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
public Tc getTcByPosition(List<Tc> tcList, int position) {  
    int k = 0;  
    for (int i = 0, len = tcList.size(); i < len; i++) {  
        Tc tc = tcList.get(i);  
        TcPr trPr = tc.getTcPr();  
        if (trPr != null) {  
            GridSpan gridSpan = trPr.getGridSpan();  
            if (gridSpan != null) {  
                k += gridSpan.getVal().intValue() - 1;  
            }
        }  
        if (k >= position) {  
            return tcList.get(i);  
        }  
        k++;  
    }  
    if (position < tcList.size()) {  
        return tcList.get(position);  
    }  
    return null;  
}
 
Example #7
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 得到行的列数
 */
public int getTcCellSizeWithMergeNum(Tr tr) {
    int cellSize = 1;
    List<Tc> tcList = getTrAllCell(tr);
    if (tcList == null || tcList.size() == 0) {
        return cellSize;
    }
    cellSize = tcList.size();
    for (Tc tc : tcList) {
        TcPr tcPr = getTcPr(tc);
        GridSpan gridSpan = tcPr.getGridSpan();
        if (gridSpan != null) {
            cellSize += gridSpan.getVal().intValue() - 1;
        }
    }
    return cellSize;
}
 
Example #8
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 跨行合并
 */
public void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {
    if (col < 0 || fromRow < 0 || toRow < 0) {
        return;
    }
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        Tc tc = getTc(tbl, rowIndex, col);
        if (tc == null) {
            break;
        }
        TcPr tcPr = getTcPr(tc);
        VMerge vMerge = tcPr.getVMerge();
        if (vMerge == null) {
            vMerge = new VMerge();
            tcPr.setVMerge(vMerge);
        }
        if (rowIndex == fromRow) {
            vMerge.setVal("restart");
        } else {
            vMerge.setVal("continue");
        }
    }
}
 
Example #9
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 #10
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 得到行的列数
 */
public static int getTcCellSizeWithMergeNum(Tr tr) {
    int cellSize = 1;
    List<Tc> tcList = getTrAllCell(tr);
    if (tcList == null || tcList.size() == 0) {
        return cellSize;
    }
    cellSize = tcList.size();
    for (Tc tc : tcList) {
        TcPr tcPr = getTcPr(tc);
        GridSpan gridSpan = tcPr.getGridSpan();
        if (gridSpan != null) {
            cellSize += gridSpan.getVal().intValue() - 1;
        }
    }
    return cellSize;
}
 
Example #11
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 跨行合并
 */
public static void mergeCellsVertically(Tbl tbl, int col, int fromRow, int toRow) {
    if (col < 0 || fromRow < 0 || toRow < 0) {
        return;
    }
    for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
        Tc tc = getTc(tbl, rowIndex, col);
        if (tc == null) {
            break;
        }
        TcPr tcPr = getTcPr(tc);
        VMerge vMerge = tcPr.getVMerge();
        if (vMerge == null) {
            vMerge = new VMerge();
            tcPr.setVMerge(vMerge);
        }
        if (rowIndex == fromRow) {
            vMerge.setVal("restart");
        } else {
            vMerge.setVal("continue");
        }
    }
}
 
Example #12
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public static void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = fromCell, len = Math.min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        HMerge hMerge = tcPr.getHMerge();
        if (hMerge == null) {
            hMerge = new HMerge();
            tcPr.setHMerge(hMerge);
        }
        if (cellIndex == fromCell) {
            hMerge.setVal("restart");
        } else {
            hMerge.setVal("continue");
        }
    }
}
 
Example #13
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 6 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public static void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell, int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        if (cellIndex == fromCell) {
            GridSpan gridSpan = tcPr.getGridSpan();
            if (gridSpan == null) {
                gridSpan = new GridSpan();
                tcPr.setGridSpan(gridSpan);
            }
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1, toCell) - fromCell + 1));
        } else {
            tr.getContent().remove(cellIndex);
        }
    }
}
 
Example #14
Source File: FOExporterVisitorGenerator.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
protected static void createFoAttributes(TcPr tcPr, Element foBlockElement){
  	// includes TcPrInner.TcBorders, CTShd, TcMar, CTVerticalJc
  	
if (tcPr==null) {
	return;
}
  	applyFoAttributes(PropertyFactory.createProperties(tcPr), foBlockElement);
  }
 
Example #15
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 设置单元格垂直对齐方式
 */
public void setTcVAlign(Tc tc, STVerticalJc vAlignType) {
    if (vAlignType != null) {
        TcPr tcPr = getTcPr(tc);
        CTVerticalJc vAlign = new CTVerticalJc();
        vAlign.setVal(vAlignType);
        tcPr.setVAlign(vAlign);
    }
}
 
Example #16
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 #17
Source File: TableWriter.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
/**
  * In the FO case, if we need to rotate the text, we do that
  * by inserting a block-container.
  * 
  * @param cellNode
  * @return
  */
@Override
 protected Element interposeBlockContainer(Document doc, Element cellNode, TcPr tcPr) {
 	
	if (tcPr==null || tcPr.getTextDirection()==null) {
		// usual case
		return cellNode;
	} else {
		
		/* We need block-container, something like:
		 * 
        <table-cell>
          <block-container reference-orientation="90">
            <block>Hello</block>
          </block-container>
        </table-cell>
         */
		
		Element ret = doc.createElementNS("http://www.w3.org/1999/XSL/Format", "fo:block-container");
		
		TextDir textDir = new TextDir(tcPr.getTextDirection());
		textDir.setXslFO(ret);
		
		cellNode.appendChild(ret);
		
		if (cellNode.hasAttribute("reference-orientation")) {
			// remove it, since it doesn't work at that level
			cellNode.removeAttribute("reference-orientation");
		}
		
		return ret;
		
	}
 }
 
Example #18
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 #19
Source File: XsltFOFunctions.java    From docx4j-export-FO with Apache License 2.0 5 votes vote down vote up
private static void createFoAttributes(TcPr tcPr, Element foBlockElement){
  	// includes TcPrInner.TcBorders, CTShd, TcMar, CTVerticalJc
  	
if (tcPr==null) {
	return;
}
  	applyFoAttributes(PropertyFactory.createProperties(tcPr), foBlockElement);
  }
 
Example #20
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 #21
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 #22
Source File: Docx4j_合并单元格_S4_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/** 
 * @Description: 跨列合并 
 */  
public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {  
    if (row < 0 || fromCell < 0 || toCell < 0) {  
        return;  
    }  
    List<Tr> trList = getTblAllTr(tbl);  
    if (row > trList.size()) {  
        return;  
    }  
    Tr tr = trList.get(row);  
    List<Tc> tcList = getTrAllCell(tr);  
    for (int cellIndex = fromCell, len = Math  
            .min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {  
        Tc tc = tcList.get(cellIndex);  
        TcPr tcPr = getTcPr(tc);  
        HMerge hMerge = tcPr.getHMerge();  
        if (hMerge == null) {  
            hMerge = new HMerge();  
            tcPr.setHMerge(hMerge);  
        }  
        if (cellIndex == fromCell) {  
            hMerge.setVal("restart");  
        } else {  
            hMerge.setVal("continue");  
        }  
    }  
}
 
Example #23
Source File: Docx4j_合并单元格_S4_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell,  
        int toCell) {  
    if (row < 0 || fromCell < 0 || toCell < 0) {  
        return;  
    }  
    List<Tr> trList = getTblAllTr(tbl);  
    if (row > trList.size()) {  
        return;  
    }  
    Tr tr = trList.get(row);  
    List<Tc> tcList = getTrAllCell(tr);  
    for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {  
        Tc tc = tcList.get(cellIndex);  
        TcPr tcPr = getTcPr(tc);  
        if (cellIndex == fromCell) {  
            GridSpan gridSpan = tcPr.getGridSpan();  
            if (gridSpan == null) {  
                gridSpan = new GridSpan();  
                tcPr.setGridSpan(gridSpan);  
            }  
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1,  
                    toCell) - fromCell + 1));  
        } else {  
            tr.getContent().remove(cellIndex);  
        }  
    }  
}
 
Example #24
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @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");
    }
}
 
Example #25
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public void mergeCellsHorizontal(Tbl tbl, int row, int fromCell, int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = fromCell, len = Math
            .min(tcList.size() - 1, toCell); cellIndex <= len; cellIndex++) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        HMerge hMerge = tcPr.getHMerge();
        if (hMerge == null) {
            hMerge = new HMerge();
            tcPr.setHMerge(hMerge);
        }
        if (cellIndex == fromCell) {
            hMerge.setVal("restart");
        } else {
            hMerge.setVal("continue");
        }
    }
}
 
Example #26
Source File: Docx4j_工具类_S3_Test.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 跨列合并
 */
public void mergeCellsHorizontalByGridSpan(Tbl tbl, int row, int fromCell,
        int toCell) {
    if (row < 0 || fromCell < 0 || toCell < 0) {
        return;
    }
    List<Tr> trList = getTblAllTr(tbl);
    if (row > trList.size()) {
        return;
    }
    Tr tr = trList.get(row);
    List<Tc> tcList = getTrAllCell(tr);
    for (int cellIndex = Math.min(tcList.size() - 1, toCell); cellIndex >= fromCell; cellIndex--) {
        Tc tc = tcList.get(cellIndex);
        TcPr tcPr = getTcPr(tc);
        if (cellIndex == fromCell) {
            GridSpan gridSpan = tcPr.getGridSpan();
            if (gridSpan == null) {
                gridSpan = new GridSpan();
                tcPr.setGridSpan(gridSpan);
            }
            gridSpan.setVal(BigInteger.valueOf(Math.min(tcList.size() - 1,
                    toCell) - fromCell + 1));
        } else {
            tr.getContent().remove(cellIndex);
        }
    }
}
 
Example #27
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 #28
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 设置单元格垂直对齐方式
 */
public static void setTcVAlign(Tc tc, STVerticalJc vAlignType) {
    if (vAlignType != null) {
        TcPr tcPr = getTcPr(tc);
        CTVerticalJc vAlign = new CTVerticalJc();
        vAlign.setVal(vAlignType);
        tcPr.setVAlign(vAlign);
    }
}
 
Example #29
Source File: WmlElementUtils.java    From docx4j-template with Apache License 2.0 5 votes vote down vote up
/**
 * @Description: 设置单元格宽度
 */
public static 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");
    }
}
 
Example #30
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);  
}