Hi,
Is there an easy way to delete the row in the table based on checkbox selection? I have attached the sample word document. If the checkbox is checked then I need to delete that corresponding row and if there are no rows in that section, then delete that section row altogether. I am setting the checkboxes programatically.
Also attached the sample java program. we are using aspose java 21.1.jdk17 sample doc.docx (16.0 KB) DeleteTableRows.zip (1.5 KB)sample doc-output.docx (15.4 KB)
Also attached sample output when Section 02 checkbox was not checked and some of the checkboxes are not checked in Section 03.
Document doc = new Document(srcFile);
Table table = (Table)doc.getChild(NodeType.TABLE, 0, true);
RowCollection rows = table.getRows();
for (int i = 0; i < rows.getCount(); i++)
{
NodeCollection<StructuredDocumentTag> nc = rows.get(i).getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);
for (StructuredDocumentTag sdt : nc) {
if (sdt.getSdtType() == SdtType.CHECKBOX)
if (sdt.getChecked()) {
rows.get(i+1).remove(); //in the sample document, the "response" cells are considered to be the new row, so we need to remove them too
rows.get(i).remove();
i--;
}
}
}
//removing the blank sections
for (int i = 0; i < rows.getCount()-1; i++)
{
boolean blankSection = false;
if ((rows.get(i).getFirstCell().getText().length() >= 7) && (rows.get(i).getFirstCell().getText().substring(0, 7).equals("Section")) &&
(rows.get(i+1).getFirstCell().getText().length() >= 7) && (rows.get(i+1).getFirstCell().getText().substring(0, 7).equals("Section"))) {
rows.get(i).remove();
i--;
}
}
//if the last row is "other" row, then delete it
Row lastRow = rows.get(rows.getCount()-1);
if (lastRow.getFirstCell().getText().substring(0, 5).equals("Other"))
lastRow.remove();
doc.save(destFile);
Is there a way to see if there is a table in a bookmarked text? I.e I have added a bookmark (Name: SectionsBookmark) in the attached document and want to know if there is a table in this bookmark.sample doc with bookmark.docx (17.4 KB)
@mp2,
You can use the following Java code of Aspose.Words for Java API to find out if there any tables in the bookmarked text.
Bookmark bookmark = doc.getRange().getBookmarks().get("SectionsBookmark");
Node curNode = bookmark.getBookmarkStart().getParentNode();
while (curNode != bookmark.getBookmarkEnd().getParentNode()) {
if (curNode.getNodeType() == NodeType.TABLE){
System.out.println("There is a table in the bookmarked text");
break;
}
curNode = curNode.getNextSibling();
}
//if we haven't found any tables in the bookmarked text, that means we got through
//the bookmarked text from the start to the end, and that means our current node
//is BookmarkEnd's parent node
if (curNode == bookmark.getBookmarkEnd().getParentNode() &&
curNode.getNodeType() != NodeType.TABLE)
System.out.println("There is no tables in the bookmarked text");
else if (curNode.getNodeType() == NodeType.TABLE) //but this node can also be a table
System.out.println("There is a table in the bookmarked text");
@mp2,
You need to update two conditions in your code. The BookmarkEnd node can be either a sibling of current node, or its child (like in your updated sample document). There is an updated code (updates in 4th and 11th lines)
Bookmark bookmark = doc.getRange().getBookmarks().get("SectionsBookmark");
Node curNode = bookmark.getBookmarkStart().getParentNode();
while (curNode != bookmark.getBookmarkEnd().getParentNode() && curNode != bookmark.getBookmarkEnd()) {
if (curNode.getNodeType() == NodeType.TABLE){
System.out.println("There is a table in the bookmarked text");
break;
}
curNode = curNode.getNextSibling();
}
if ((curNode == bookmark.getBookmarkEnd().getParentNode() || curNode == bookmark.getBookmarkEnd())&&
curNode.getNodeType() != NodeType.TABLE)
System.out.println("There is no tables in the bookmarked text");
else if (curNode.getNodeType() == NodeType.TABLE)
System.out.println("There is a table in the bookmarked text");
@mp2,
It is great that you were able to resolve this issue on your end. In case you need any other help, please, feel free to ask in Aspose.Words’ forum.