We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Empty line at the end of table cells containing HTML

Hey Aspose Support Team,


I started working with Aspose Words a month ago and we are currently trying to insert HTML paragraphs into table cells using the document builder in Aspose Words. Here is a simplified code snippet:

documentBuilder.startTable();

documentBuilder.insertCell();
documentBuilder.insertHtml(“

inner HTML

”);
documentBuilder.endRow();

documentBuilder.insertCell();
documentBuilder.insertHtml(“

inner HTML

”);
documentBuilder.endRow();

documentBuilder.endTable();

Our problem is we get an empty line at the end of each cell as the inserted paragraph closes with a paragraph break and the control character that marks the end of the cell (I don’t know how to call it) is written in the next line.

How can we achieve to remove the paragraph break at the end of the inserted HTML and move the cell end character to that position instead? Neither the range nor the text content of the last Run of documentBuilder.getCurrentParagraph().getPreviousSibling() contains a ControlChar.PARAGRAPH_BREAK that could be removed or replaced.

Thanks and regards,
Sören

Hi Sören,

Thanks for your inquiry. Please use the following code example to achieve your requirements. Hope this helps you. Please let us know if you have any more queries.

Document document = new Document();

DocumentBuilder documentBuilder = new DocumentBuilder(document);

Table table = documentBuilder.startTable();

documentBuilder.insertCell();

documentBuilder.insertHtml("

inner HTML

");

documentBuilder.endRow();

documentBuilder.insertCell();

documentBuilder.insertHtml("

inner HTML

");

documentBuilder.endRow();

documentBuilder.endTable();

for (Cell cell : (Iterable)table.getChildNodes(NodeType.CELL, true))

{

if(cell.getLastParagraph().toString(SaveFormat.TEXT).trim().equals(""))

cell.getLastParagraph().remove();

}

document.save(MyDir + "out.docx");


Hey Tahir,


thanks for the quick response. I tested your solution and it worked like a charm!

Thankful regards,
Sören

Hi Sören,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.