Extra empty paragraph is added by DocumentBuilder.insertHtml using Java

Hi guys,

I’m running Aspose Words for Java and I noticed that when you insert HTML like this:

    final Document document = new Document();
    final DocumentBuilder builder = new DocumentBuilder(document);

    builder.insertHtml(
            "<p>Paragraph 1 has an extra space inserted after it.</p>" +
                    "<p>Paragraph 2 has an extra line break inserted after it.</p>");
    document.save(BASE_PATH + "result.docx");

The resulting file has an extra newline after the second sentence. This is more noticeable when replacing the HTML inside a table.

Is there a way for me to fix it?

Best regards,
Hugo Freixo

@Hugo_Freixo

Please note that a valid empty document has one Paragraph node. When you insert two HTML paragraphs into document, the output document will has three paragraphs. However, you can remove the last empty paragraph of document using Node.Remove method.

A valid empty cell has one empty paragraph. After inserting HTML into table’s cell, the empty paragraph will remain in the cell. You can get the last paragraph of cell using Cell.LastParagraph property and remove it using Node.Remove method.

Hi @tahir.manzoor,

How can I go to the nearest cell from the current node I’m currently in?
When I’m on a node inside a table I want to get the current cell to remove the last paragraph.

Best regards,
Hugo Freixo

@Hugo_Freixo

Please use the following code snippet to get the desired output. Hope this helps you.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

//Your code...

Cell cell  = (Cell)builder.getCurrentNode().getAncestor(NodeType.CELL);
if(cell != null && cell.getLastParagraph().toString(SaveFormat.TEXT).trim().length() == 0)
    cell.getLastParagraph().remove();