How to remove Empty Paragraphs

I am using Aspose.Words for java version 14.7.0.


I am creating a word document using the DocumentBuilder. I have a tables that have paragraphs in them. In order to apply paragraph styles to the paragraphs I use the DocumentBuilder.writeLn() method. This is causing an empty paragraph

at the end of a table. If I don’t use the DocumentBuilder.writeln() method the paragraph will not retain the style I associated with it. I then tried to identify the empty paragraphs and remove them but I was not successful.


This empty paragraph is causing an issue in my word document. How do I remove the empty paragraphs or how to get the style to apply to text without having to use the writeln() method.

<w:tbl>
<w:tblPr>
<w:tblStyle w:val=“TableNormal” />
<w:tblW w:w=“5000” w:type=“pct” />
<w:tblBorders>
<w:top w:val=“nil” />
<w:left w:val=“nil” />
<w:bottom w:val=“nil” />
<w:right w:val=“nil” />
<w:insideH w:val=“nil” />
<w:insideV w:val=“nil” />
</w:tblBorders>
<w:tblCellMar>
<w:left w:w=“108” w:type=“dxa” />
<w:right w:w=“108” w:type=“dxa” />
</w:tblCellMar>
</w:tblPr>
<w:tblGrid>
<w:gridCol w:w=“432” />
<w:gridCol w:w=“8208” />
</w:tblGrid>
<w:tr>
<w:tblPrEx>
<w:tblW w:w=“5000” w:type=“pct” />
<w:tblBorders>
<w:top w:val=“nil” />
<w:left w:val=“nil” />
<w:bottom w:val=“nil” />
<w:right w:val=“nil” />
<w:insideH w:val=“nil” />
<w:insideV w:val=“nil” />
</w:tblBorders>
<w:tblCellMar>
<w:left w:w=“108” w:type=“dxa” />
<w:right w:w=“108” w:type=“dxa” />
</w:tblCellMar>
</w:tblPrEx>
<w:tc>
<w:tcPr>
<w:tcW w:w=“250” w:type=“pct” />
</w:tcPr>
<w:p>
<w:pPr>
<w:pStyle w:val=“ACTParagraph” />
<w:jc w:val=“left” />
</w:pPr>
</w:p>
</w:tc>
<w:tc>
<w:tcPr>
<w:tcW w:w=“4750” w:type=“pct” />
</w:tcPr>
<w:p>
<w:pPr>
<w:pStyle w:val=“ACTParagraph” />
<w:jc w:val=“left” />
</w:pPr>
<w:r>
<w:t>Employees are encouraged to fulfill their civic responsibilities by serving as jurors when summoned and will be given time off to do so. Employees who receive a jury duty summons are required to provide [EMPLOYER NAME</w:t>
</w:r>
<w:r>
<w:t>]</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> with a copy of the summons as soon as possible so that arrangements may be made to accommodate the employees</w:t>
</w:r>
<w:r>
<w:t>’</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> absence. [Employees may use any available paid time off for jury duty [extending beyond [NUMBER</w:t>
</w:r>
<w:r>
<w:t>]</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> days</w:t>
</w:r>
<w:r>
<w:t>]</w:t>
</w:r>
<w:r>
<w:t>.</w:t>
</w:r>
<w:r>
<w:t>]</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> Employees and/or [EMPLOYER NAME</w:t>
</w:r>
<w:r>
<w:t>]</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> may request that the employees be excused from jury duty if either believes that the employees</w:t>
</w:r>
<w:r>
<w:t>’</w:t>
</w:r>
<w:r>
<w:t xml:space=“preserve”> absence would create serious operational difficulties.</w:t>
</w:r>
</w:p>
<w:p />
</w:tc>
</w:tr>
</w:tbl>

Hi Linda,


Thanks for your inquiry. You can use DocumentBuilder.write(String) method instead but before using it you need to manually specify the Style the current paragraph will be formatted with. For example, please see the following lines of code:
builder.getCurrentParagraph().getParagraphFormat().setStyleName(“ACT Paragraph”);
builder.write(“Hello World”);
Alternatively, you can simply remove last Paragraphs from the end of target Table Cell by using the following code snippet:
Document doc = new Document(getMyDir() + “6-520-6540.docx”);
Table targetTab = doc.getFirstSection().getBody().getTables().get(2);
targetTab.getRows().get(0).getCells().get(1).getLastParagraph().remove();
doc.save(getMyDir() + “out.docx”);
I hope, this helps.

Best regards,

I don't just want to remove the last paragraph, i need a way to identify the empty paragraphs and only remove those ones.

Hi Linda,


Thanks for your inquiry. You can add the following checks in your code to check whether a paragraph is empty or not:
if (para.getChildNodes().getCount() == 0 || para.toString(SaveFormat.TEXT).trim().equals(""))
I hope, this helps.

Best regards,