ms-wordapache-poialignmentvertical-alignmentline-spacing

Line Spacing Inside the Table in Word Using Apache POI


https://support.content.office.net/en-us/media/fbe67397-658c-4b04-b295-b0d6759e1aaa.jpg

Hello Everyone I need to add Line spacing Before(Spacing about 6pt) effect inside the table.You can see the above image that has a paragraph spacing i need that effect. I am using 5.0.0 version. And checked on many sites. nothing useful. if someone help me that will be great. Thank You in advance :)


Solution

  • To set the paragraph spacing apache poi provides XWPFParagraph.setSpacingBefore and XWPFParagraph.setSpacingAfter. Measurement unit for the int is twentieths of a point. So 8*20 is 8 pt.

    In a table one needs get the first paragraph of a XWPFTableCell to set spacing before:

    ...
    XWPFTableRow tableRow = ...
    ...
    XWPFTableCell cell = tableRow.getCell(1);
    if (cell.getParagraphs().size() > 0) cell.getParagraphs().get(0).setSpacingBefore(8*20);
    ...
    

    But if the requirement is vertical aligning in cell, then XWPFTableCell.setVerticalAlignment should be used. But this only can be rendered properly if there is no spacing after the last paragraph which pushs the paragraph upwards.

    So

    ...
    XWPFTableRow tableRow = ...
    ...
    XWPFTableCell cell = tableRow.getCell(1);
    cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
    if (cell.getParagraphs().size() > 0) cell.getParagraphs().get(cell.getParagraphs().size()-1).setSpacingAfter(0);
    ...