Java Word Appache POI – Vertically align cell content / remove space after text

My table look like this: Word POI Table

As you can see the text is aligned to the top of a cell. I want to have it centered vertically or aligned to bottom of the cell.. I tried to achieve it with cell.setVerticalAlignment(XWPFVertAlign.CENTER); but it doesn’t change anything. I was able to add margin to the cells by table.setCellMargin(). It added margin to top of the cell, but empty space below the text makes the cell too big. Maybe is there a way to make the cell width fit text height? My intended result would look like this: desired result

And here is part of my code:`

XWPFTable table = document.createTable();
CTTblWidth tableIndentation = table.getCTTbl().getTblPr().addNewTblInd();
tableIndentation.setW(BigInteger.valueOf(720));
tableIndentation.setType(STTblWidth.DXA);

XWPFTableRow tableRowOne = table.getRow(0);
XWPFParagraph paragraphTable = tableRowOne.getCell(0).addParagraph();
table.setCellMargins(0, 50, 0, 0);
XWPFRun runT = paragraphTable.createRun();

runT.setBold(true);
runT.setColor("ffffff");
runT.setText("REFERENCE ACTIVITIES PROVIDED: " + String.valueOf(def.format(c.getEarnedSum())) + " POINTS EARNED");
runT = tableRowOne.addNewTableCell().addParagraph().createRun();
runT = tableRowOne.addNewTableCell().addParagraph().createRun();

tableRowOne.getCell(0).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(1).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(2).getCTTc().addNewTcPr().addNewShd().setFill("8dc63f");
tableRowOne.getCell(0).removeParagraph(0);
tableRowOne.getCell(1).removeParagraph(0);
tableRowOne.getCell(2).removeParagraph(0);`

Answer

The XWPFTableCell.setVerticalAlignment works. But one has to know that each paragraph has a default spacing of 10pt after it. So the paragraph height is text height plus 10pt. And that height is vertical aligned then.

So to get XWPFTableCell.setVerticalAlignment working as you might expect, you needs to set spacing after of the paragraphs to 0.

Complete example:

import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;

public class CreateWordTableVerticalAlign {

 public static void main(String[] args) throws Exception {

  XWPFDocument document= new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run=paragraph.createRun();  
  run.setText("The table:");

  //create the table
  XWPFTable table = document.createTable(3,3);
  table.setWidth("100%");
  for (int r = 0; r < 3; r++) {
   XWPFTableRow row = table.getRow(r);

   row.setHeight(1440/2); // 1/2inch; 1440Twip = 1440/20 == 72pt = 1inch 

   for (int c = 0; c < 3; c++) {
    XWPFTableCell cell = row.getCell(c);
    cell.setText("row " + r + ", col " + c);

    // get first paragraph in cell - this contains the content set above by cell.setText
    XWPFParagraph firstParagraphInCell = cell.getParagraphArray(0);
    // set spacing after to 0 (defaults to 10pt)
    firstParagraphInCell.setSpacingAfter(0);

    if (r == 0) {
     // default vertical align
    } else if (r == 1) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
    } else if (r == 2) {
     cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.BOTTOM);
    }
   }
  }

  FileOutputStream out = new FileOutputStream("CreateWordTableVerticalAlign.docx"); 
  document.write(out);
  out.close();
  document.close();
 }
}

Result:

enter image description here

Leave a Reply

Your email address will not be published. Required fields are marked *