Set Outer Border of Apache POI XWPFTable table?
Asked Answered
S

3

5

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!

Sahaptin answered 4/12, 2015 at 16:28 Comment(0)
S
11

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);
Sahaptin answered 4/12, 2015 at 16:46 Comment(2)
STBorder.SINGLE didn't work for me, I used STBorder.NONE insteadTowner
its helped me for making the borderless table with NONE, thank youRossner
B
2

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);    
Blalock answered 17/9, 2020 at 13:33 Comment(1)
This works, but doesn't remove the inner borders - for this you also need to add: ...Borders().getInsideH().setVal(STBorder.NONE); ...Borders().getInsideV().setVal(STBorder.NONE);Gobo
U
1

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();
Untouchability answered 30/8, 2016 at 1:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.