Hi,
When i delete an table bookmark, althought the content is deleted, is leaving an empty box to the document. Is there a way for that to be removed?
Bookmark tempBookmark = document.getRange()
.getBookmarks()
.get(tempTarget.getUniqueName());
tempBookmark.setText("");
tempBookmark.remove();
I upload the final document so you can see the box that is left.
emptyTableDocument.docx (10.1 KB)
Thank you,
Eleftheria Stefanou
@Eleftheria_Stefanou
To remove a table bookmark without leaving an empty box in the document, you can use the following approach. The code you provided sets the text of the bookmark to an empty string and then removes the bookmark. However, this might not fully eliminate the space left by the table.
Instead of just removing the bookmark, you should also ensure that the content associated with the bookmark is properly deleted. Here’s a modified version of your code that handles this:
// Get the bookmark you want to remove
Bookmark tempBookmark = document.getRange().getBookmarks().get(tempTarget.getUniqueName());
// Move to the start of the bookmark and remove the content
Node startNode = tempBookmark.getBookmarkStart();
Node endNode = tempBookmark.getBookmarkEnd();
// Remove the content between the start and end nodes
while (startNode != endNode) {
Node nextNode = startNode.getNextSibling();
startNode.remove();
startNode = nextNode;
}
// Finally, remove the bookmark itself
tempBookmark.remove();
This code snippet first navigates to the start of the bookmark, removes all nodes until it reaches the end of the bookmark, and then removes the bookmark itself. This should effectively eliminate any empty space left in the document.
If you still encounter issues, consider checking if there are any additional formatting or layout elements that might be contributing to the empty space.
For more detailed guidance on working with bookmarks in Aspose.Words, you can refer to the official documentation on Working with Bookmarks and the BookmarkCollection.Clear method which provides insights into managing bookmarks effectively.
If you have further questions or need additional assistance, feel free to ask!
This does not work since the Node startNode is the same as endNode. So it doesn’t delete the content of the table at all.
@Eleftheria_Stefanou Could you please attach your input document here for testing? We will check the issue and provide you more information.
You can determine whether the bookmark is table column bookmark using Bookmark.isColumn()
property and adjust your code to remove the bookmark’s columns or whole table if required. Setting bookmark text to empty string will not remove the table.