Apache POI XWPFTableRow drawing with double height

1.1k views Asked by At

I am trying to draw a table in word document using Apache POI XWPF. But the table is drawing with double height rows like thisenter image description here

Here is my source

             XWPFTable table = document.createTable(5, 1);
            for (Test t : tests) {
                    XWPFTableRow row = table.getRow(k - 1);
                    XWPFTableCell cell = row.getCell(0);
                    XWPFParagraph ansParagraph = new XWPFParagraph(cell.getCTTc().insertNewP(0), cell);
                    XWPFRun ansRun = ansParagraph.createRun();
                    ansRun.setText(k + ") ");
                    cell.addParagraph(ansParagraph);
                    k++;
                }

how can I reduced the height of row.

1

There are 1 answers

0
bbhar On

When you create a table in Apache POI XWPF it already has a blank paragraph in every cell. You are getting double height rows because you are also adding a paragraph in it.

So you replace the line

XWPFParagraph ansParagraph = new XWPFParagraph(cell.getCTTc().insertNewP(0), cell);

with

XWPFParagraph ansParagraph = cell.getParagraphs().get(0);

And remove the line

cell.addParagraph(ansParagraph);

Then it will work OK.