Aspose words java - remove any empty space after document generation

Hi ,

I am looking for a efficitent way to remove any white space- empty space from document after doucment is generated as - in attached document.

Thank you for assistance in advance.

Hi Rita,


Thanks for your inquiry. These vertical spaces are because of empty Paragraphs. You can use the following code to remove them.
Document doc = new Document(getMyDir() + “cleanDoc.docx”);

Node[] paras = doc.getChildNodes(NodeType.PARAGRAPH, true).toArray();
for (int i = 0; i < paras.length; i++)
{
Paragraph p = (Paragraph)paras[i];
if (p.getChildNodes().getCount() == 0 || p.toString(SaveFormat.TEXT).trim().equals(""))
p.remove();
}

doc.save(getMyDir() + “out-java.docx”);

I hope, this helps.

Best regards,