Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Outer Border of Apache POI XWPFTable table?

I need to set the outer border of a Apache POI XWPFTable table. I know the below command set insade border, but do not find way for setting outer border.

table.setInsideHBorder( XWPFBorderType.SINGLE, 4, 0, "FF0000");

Any help? Thanks in advance!

like image 978
Diego Quirós Avatar asked Dec 04 '15 16:12

Diego Quirós


3 Answers

I find it:

CTTblPr tblpro = table.getCTTbl().getTblPr();

CTTblBorders borders = tblpro.addNewTblBorders();
borders.addNewBottom().setVal(STBorder.SINGLE); 
borders.addNewLeft().setVal(STBorder.SINGLE);
borders.addNewRight().setVal(STBorder.SINGLE);
borders.addNewTop().setVal(STBorder.SINGLE);
//also inner borders
borders.addNewInsideH().setVal(STBorder.SINGLE);
borders.addNewInsideV().setVal(STBorder.SINGLE);
like image 70
Diego Quirós Avatar answered Sep 18 '22 06:09

Diego Quirós


Maybe is it not what you exactly looking for, but it you what to copy border style from one table to another you can use this method:

private void copyTableBorderStyle(XWPFTable table, XWPFTable newTable) {
    newTable.setInsideHBorder(table.getInsideHBorderType(), table.getInsideHBorderSize(), table.getInsideHBorderSpace(), table.getInsideHBorderColor());
    newTable.setInsideVBorder(table.getInsideVBorderType(), table.getInsideVBorderSize(), table.getInsideVBorderSpace(), table.getInsideVBorderColor());
    newTable.getCTTbl().setTblPr(table.getCTTbl().getTblPr()); 
    newTable.getCTTbl().setTblGrid(table.getCTTbl().getTblGrid());
}

But for your question if you what to change outer border you need to get org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl property and configure it:

CTTbl cttbl = table.getCTTbl();
like image 27
Renat Gatin Avatar answered Sep 19 '22 06:09

Renat Gatin


To remove all borders from table:

tab.getCTTbl().getTblPr().getTblBorders().getLeft().setVal(STBorder.NONE);    
tab.getCTTbl().getTblPr().getTblBorders().getRight().setVal(STBorder.NONE);    
tab.getCTTbl().getTblPr().getTblBorders().getTop().setVal(STBorder.NONE);    
tab.getCTTbl().getTblPr().getTblBorders().getBottom().setVal(STBorder.NONE);    
like image 37
Pavel Avatar answered Sep 22 '22 06:09

Pavel