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

Free Support Forum - aspose.com

Remove Dynamic Tables

Dynamic Tables are created as below.

                        com.aspose.words.DocumentBuilder builder;

builder.moveToBookmark(“HealthPractitionerCaseTable”);
builder.startTable();
builder.insertCell();
builder.getFont().setColor(Color.BLACK);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
builder.writeln(“tttttttt”);
builder.endTable();

Dynamic Tables have to be removed from the Document after creation.
Please advise.

@pexy_poulose,

You can achieve it like this:

com.aspose.words.DocumentBuilder builder;
builder.moveToBookmark(“HealthPractitionerCaseTable”);

Table tab =  builder.startTable();

builder.insertCell();
builder.getFont().setColor(Color.BLACK);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);
builder.writeln(“tttttttt”);
builder.endTable();

tab.remove();

Best regards,

Thanks. However, I am trying to remove the table already created by the above code in an existing document.
The bookmark cursor points to below the table as builder.endTable(); puts the cursor at the end of the table waiting for inserting new tables/rows.
How would I be able to retrieve the Table object in existing documents with the help of bookmark names?
The requirement is to obtain tables without knowing its index.

@pexy_poulose,

Please try using the following code:

Document doc = new Document("D:\\temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

builder.moveToBookmark("HealthPractitionerCaseTable", true, true);

builder.writeln("before table");

builder.startTable();
builder.insertCell();
builder.getFont().setColor(Color.BLACK);
builder.getCellFormat().getShading().setBackgroundPatternColor(Color.WHITE);

// insert a hidden bookmark in the first cell of table
builder.startBookmark("_hdnbm");
builder.endBookmark("_hdnbm");

builder.writeln("tttttttt");
builder.endTable();

builder.writeln("after table");

Table table = (Table)doc.getRange().getBookmarks().get("_hdnbm").getBookmarkStart().getAncestor(NodeType.TABLE);
if (table != null)
{
    table.remove();
}

doc.save("D:\\temp\\awjava-17.8.docx");

Hope, this helps.

Best regards,