Bryan
Bryan

Reputation: 55

Apache POI word, how to remove the white space (new line) from beginning of table cell?

I am only able to style the contents of a table cell using paragraph element to change the color of the text for example. When i create a paragraph inside the table it creates a new line. For my project i need these cell to be as small as possible for that i am using

   tr.setHeight(300);
   tr.getCtRow().getTrPr().getTrHeightArray(0).setHRule(STHeightRule.EXACT);

that works when there is no blank lines.

the code below is the problem

    XWPFTable tb = document.createTable(2,2);
    XWPFParagraph p1 =  tb.getRow(0).getCell(0).addParagraph();
    XWPFRun p1run = p1.createRun();
    p1run.setText("why is there a space");
    XWPFParagraph p2 =  tb.getRow(1).getCell(1).addParagraph();
    XWPFRun p2run = p2.createRun();
    p2run.setText("still there ");

This create this output

https://i.sstatic.net/bSONW.png

Upvotes: 1

Views: 4969

Answers (3)

Rone'
Rone'

Reputation: 1

This works >>

Use cell.getParagraphs().get(0) instead of cell.addParagraph().

Upvotes: 0

user11725513
user11725513

Reputation: 93

Before adding a paragraph clearing text in cell worked for me

cell.clearText();

Upvotes: 0

Bryan
Bryan

Reputation: 55

I found the solution here.. Duplicate Table Paragraphs in Docx created with Apache POI

It seems you add a paragraph then remove the original with this code:

XWPFParagraph paragraph = cell.addParagraph();
cell.removeParagraph(0);

Upvotes: 4

Related Questions