New paragraph automatically appended when saving

Hi,

I’m facing the following problem with Aspose.Words 2.7.0 for Java. A document is created. After all processing, there are X paragraphs in the doc. After saving, there are X+1 paragraphs in the document. It seems that a new paragraph is automatically appended to the end of the document.

Here is the code representing the problem, I have also attached the concerned doc to this post (named input.doc).

try
{
    Document d = new Document("c:\input.doc");
    NodeCollection nodes = d.getChildNodes(NodeType.PARAGRAPH, true);
    nodes.removeAt(nodes.getCount() - 1);
    System.out.println("Paragraph count before saving: " + nodes.getCount());
    d.save("c:\output.doc", SaveFormat.DOC);
    nodes = d.getChildNodes(NodeType.PARAGRAPH, true);
    System.out.println("Paragraph count after saving: " + nodes.getCount());
}
catch (Exception e)
{
    e.printStackTrace();
}

Thanks in advance,

Sebastien.

Hi Sebastien,

Thanks for your inquiry. This occurs because using your code you remove the last paragraph of the document, and after removing your document ends with Table node. This is not valid and Aspose.Words adds additional paragraph after the Table at the end of the document. Aspose.Words also will add paragraphs into table cells if you remove all child nodes from cells. This is because minimum requirements of Word documents.

Best regards.

ok, that is what I thought. So I just have to check the last node of the document to control the last paragraph:

if(doc.getLastSection().getBody().getLastChild().getNodeType() != NodeType.PARAGRAPH){
    doc.getLastSection().getBody().appendChild(new Paragraph(doc));
}

Thanks for information,

Sebastien.