Setting bookmark.Text="" makes its parentNode null

In the newest version of Aspose.Word, we are trying to remove bookmarks from a document and put something else there. We loop through document nodes, and check if the node has a bookmark. If it does we set the bookmark.Text = “”, but in this new version of Aspose.Word, doing that is making it so the node.ParentNode = null. Any ideas why that might be happening?

@dmerkle1

It seems that setting bookmark.Text = "" in the latest version of Aspose.Words is causing the ParentNode of the bookmark to become null. This behavior may be due to changes in how bookmarks are managed in the newer version of the library.

When you set the text of a bookmark to an empty string, it might be triggering an internal mechanism that removes the bookmark from the document structure, leading to the ParentNode being set to null. This could be a result of the bookmark’s content being cleared, which in turn affects its relationship with the document nodes.

To work around this issue, instead of setting the bookmark’s text to an empty string, you might consider removing the bookmark entirely if you no longer need it. You can do this using the Remove method on the bookmark, which will safely remove it from the document without affecting the parent node structure.

Here’s a brief example of how to remove a bookmark:

Document doc = new Document("your-document.docx");
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
if (bookmarks.get("YourBookmarkName") != null) {
    bookmarks.get("YourBookmarkName").remove();
}
doc.save("updated-document.docx");

This approach ensures that the bookmark is properly removed from the document without leaving any dangling references or null parent nodes. If you need to replace the bookmark with new content, you can insert new text or elements at the same location after removing the bookmark.

If you continue to experience issues or if this behavior is critical to your application, I recommend reaching out to Aspose support for further assistance, as they may provide insights specific to the version you are using.

@dmerkle1 Could you please attach your sample problematic input document and specify the bookmark that causes the problem or provide code that will allow us to reproduce the problem? We will check the issue and provide you more information.

Uh it honestly seems like its any bookmark. When i set the text to “”, the node that the bookmark was a part of gets like cleared out. So it no longer has any additional runs, no parent or previous, etc. But maybe its when the bookmark start and bookmark end are in different nodes?

@dmerkle1 Thank you for additional information.

Bookmark start and bookmark end are always different nodes. Setting Bookmark.Text to empty string removes nodes between them. So it is expected that some nodes are removed from document model when you do so.