Contents fulfillment issue in table cell

I want to fulfill rich text content into words table cell, which will translate from html to doc.
The following doc is useful.
https://docs.aspose.com/words/java/insert-and-append-documents/

However, the problem is:
After operation, there always exist blank space in the end of cell.
and the blank paragraph seems special. I can not delete it, or aspose will throw out exception when I create another new cell.

Please help.

Hi Amarjeet,

Thanks for your inquiry. Please use the following code example to remove last empty paragraph of table’s cell. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
Paragraph para = table.getFirstRow().getCells().get(0).getLastParagraph();
if (para.toString(SaveFormat.TEXT).trim().equals(""))
    para.remove();
doc.save(MyDir + "Out.docx");

If you still face problem, please share following detail for investigation purposes.

  • Please attach your input Word document.
  • Please

create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate
your output document

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior. You can use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

hi Tahir,

Thank you for your feedback.
As suggestion, I present code for you.

The problem line is:

/**
* Here is the problem.
* Comment it out, we will get the blank line in the end of cell.
*/
insertAfterNode.remove();

Hi Amarjeet,

Thanks for sharing the detail. The behavior of insertDocument is correct. The empty cell have one empty paragraph. This method inserts the HTML document after the current paragraph. Please remove this empty paragraph after inserting the document.

You can also achieve your requirements by inserting the HTML using DocumentBuilder.insertHtml method as shown below. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// table
builder.startTable();
// (1)
builder.insertCell();
String question = "Is this a hardware component of a mass-market product that has been designed for an existing mass-market product, meeting all of the following:"
    + "Information security\" is not the primary function or set of functions of the component"
    + "the component does not change any cryptographic functionality of the existing items, or add new cryptographic functionality to the existing items"
    + "the feature set of the component is fixed and is not designed or modified to customer specification";
builder.insertHtml(question);
// (2)
builder.insertCell();
builder.write("nothing");
builder.endRow();
builder.endTable();
builder.getDocument().save(MyDir + "Out.docx");

hi, Tahir
Appreciate for your great help.

As we talk, this last blank paragraph is special.
To delete it, no problem. However, we will meet exception trouble in the following line.

builder.insertHtml(question);
cell.getLastParagraph().remove(); // delete the blank paragraph.
// (2)
builder.insertCell(); // will throw out nullpointer exception here.

Thanks again.

Hi Amarjeet,

Thanks for your inquiry. I have modified the code. Please remove the last empty paragraph of each cell before saving the document. Please check the following highlighted code below. Hope this helps you. Please let us know if you have any more queries.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// table
Table table = builder.startTable();
// (1)
Cell cell1 = builder.insertCell();
String question = "Is this a hardware component of a mass-market product that has been designed for an existing mass-market product, meeting all of the following:"
    +"Information security\" is not the primary function or set of functions of the component"
    +"the component does not change any cryptographic functionality of the existing items, or add new cryptographic functionality to the existing items"
    +"the feature set of the component is fixed and is not designed or modified to customer specification";
builder.insertHtml(question);
// (2)
builder.insertCell();
builder.write("nothing");
builder.endRow();
builder.endTable();
// cell1.getLastParagraph().remove();
for (Row row : table.getRows())
{
    for (Cell cell : row.getCells())
    {
        if (cell.getLastParagraph().toString(SaveFormat.TEXT).trim().equals(""))
        {
            cell.getLastParagraph().remove();
        }
    }
}
builder.getDocument().save(MyDir + "Out.docx");

Thanks Tahir,

The problem is solved. Your patch works well.

I thought it is bug for
builder.insertHtml(question) because the additional blank paragraph.

I still kept interested in how to delete the last bank paragraph; meanwhile, do not influence on the following cell creation method. ~_~

Have a nice day.

Hi Amarjeet,

Thanks for your inquiry.

*samarjeet:

I still kept interested in how to delete the last bank paragraph; meanwhile, do not influence on the following cell creation method. ~_~*

Please note that a minimal valid cell needs to have at least one Paragraph. Paragraph.IsEndOfCell Property return true if this paragraph is the last paragraph in a Cell; false otherwise. If table’s cell has only one empty paragraph, you can not delete this.

Could you please share your document along with screenshot of last blank paragraph here for testing? We will then provide you more information on this along with code.