I have some problems in generating tables.
There are some blanks in my table cells before the content When I use docx4j to generate some tables in ms-word.
When the text is too long,the text will start with a newline.
It looks strange,so I want to move them away.
Could I remove those blanks ? Or could I align the content to the center of the table cell?
Please help me.
Here are my codes:
- Code: Select all
//create a vertival table
public static Tbl createVerticalMergeTable(Object[][] tableDatas,WordprocessingMLPackage wordMLPackage){
if(tableDatas == null || tableDatas.length == 0){
return null;
}else{
ObjectFactory factory = Context.getWmlObjectFactory();
int cols = getMaxLength(tableDatas);
Tbl table = factory.createTbl();
addBorders(table);
for (Object[] objects : tableDatas) {
if(objects != null){
addTableRowWithMergedCells(objects,cols, table,factory,wordMLPackage);
}
}
return table;
}
}
//add borders
private static void addBorders(Tbl table) {
ObjectFactory factory = Context.getWmlObjectFactory();
TblPr tblPr = factory.createTblPr();
TblWidth tblWidth = factory.createTblWidth();
tblWidth.setW(new BigInteger("9000"));
tblWidth.setType(TblWidth.TYPE_DXA); // twips
tblPr.setTblW(tblWidth);
TblStyle tblStyle = factory.createCTTblPrBaseTblStyle();
tblStyle.setVal("TableGrid");
tblPr.setTblStyle(tblStyle);
CTTblLayoutType layout = factory.createCTTblLayoutType();
layout.setType(STTblLayoutType.FIXED);
tblPr.setTblLayout(layout);
table.setTblPr(tblPr);
CTBorder border = new CTBorder();
border.setColor("auto");
border.setSz(new BigInteger("4"));
border.setSpace(new BigInteger("0"));
border.setVal(STBorder.SINGLE);
TblBorders borders = new TblBorders();
borders.setBottom(border);
borders.setLeft(border);
borders.setRight(border);
borders.setTop(border);
borders.setInsideH(border);
borders.setInsideV(border);
table.getTblPr().setTblBorders(borders);
}
//add merge cells
private static void addTableRowWithMergedCells(Object[] objs,int cols, Tbl table,ObjectFactory factory,WordprocessingMLPackage wordMLPackage) {
if(objs == null || table == null || factory == null || wordMLPackage == null){
return;
}
Tr tableRow1 = factory.createTr();
for (int i = 0; i < cols; i++) {
String content = null;
if(i < objs.length){
content = objs[i] == null ? null : objs[i].toString();
}else{
content = "";
}
addMergedColumn(tableRow1, content,factory,wordMLPackage);
}
table.getContent().add(tableRow1);
}
private static void addMergedColumn(Tr row, String content,ObjectFactory factory,WordprocessingMLPackage wordMLPackage) {
if (content == null) {
addMergedCell(row, "", null,factory,wordMLPackage);
} else {
addMergedCell(row, content, "restart",factory,wordMLPackage);
}
}
private static int getMaxLength(Object[][] objects){
int temp = 0;
if(objects == null || objects.length == 0){
temp = 0;
}
for (Object[] objects2 : objects) {
if(objects2 != null && objects2.length > temp){
temp = objects2.length;
}
}
return temp;
}
private static void addMergedCell(Tr row, String content, String vMergeVal,ObjectFactory factory,WordprocessingMLPackage wordMLPackage) {
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) {
P paragraph = wordMLPackage.getMainDocumentPart().createParagraphOfText(content);
List<Object> texts = getAllElementFromObject(paragraph, Text.class);
if(texts != null && !texts.isEmpty()){
for (int i = 0; i < texts.size(); i++) {
String temp = ((Text)texts.get(i)).getValue();
temp = temp == null ? "" : temp.trim();
if(!"".equals(temp)){
paragraph.getContent().remove(texts.get(i));
}
}
}
List<Object> runs = getAllElementFromObject(paragraph, R.class);
if(runs != null && !runs.isEmpty()){
for (int i = 0; i < runs.size(); i++) {
runs.remove(i);
}
}
tableCell.getContent().add(paragraph);
// tableCell.getContent().add(
// wordMLPackage.getMainDocumentPart().
// createParagraphOfText(content));
}
row.getContent().add(tableCell);
}