BookmarkEnd of a bookmark is being removed on removing "another" bookmark!

Hi,
Since I updated my Aspose.Words.dll to the latest version (14.6.0.0) I’m havving some issues with the BookmarkEnd object (it used to work correctly when using version 14.2.1)

I have a report containing 2 tables:
- First table has its bookmark named “AnotherTable”, and its first row has a bookmark named “DeletedBookmark”
- Second table has its bookmark named “Table_DET”.
- A bookmark is added in the space between the two tables, named “Table_DER”

Now in my code, each time I delete a bookmark that ends with _DET, I delete similar bookmarks ending with _DER. When bookmark “Table_DER” is removed, the end of bookmark “DeletedBookmark” is being removed too! Why is that?!

Please find attached my input doc and a project containing the code I’m using to delete the bookmarks, I hope you can help me fix this, it’s so urgent, since it was working well before the new version of Aspose.Words.dll.

Thanks in advance,
Saleem

Any news about this yet??
Sorry for insisting but this is seriously an urgent issue for me!

Hi Saleem,

Please accept my apologies for late response.

Thanks for your inquiry. Could you please share some detail about DeleteBookmarkTable method what exact you want to achieve using Aspose.Words? Please share your expected output document here for our reference.

If you want to remove bookmark Table_DER and Table_DET, please use the following code example.

Using docStream As New FileStream("C:/testInput.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
docReport = New Document(docStream)
builder = New DocumentBuilder(docReport)
End Using
docReport.Range.Bookmarks("Table_DET").Remove()
docReport.Range.Bookmarks("Table_DER").Remove()
docReport.Save("C:\testOutput.docx", SaveFormat.Docx)

Hi Tahir,
Sorry I’m not at my office right now to send you the expected output doc,
But what I want to achieve here is to delete the table that is in the “Table_DET” bookmark range and the line above it which is in the “Table_DER” bookmark range. When I do that I find that bookmark end of “DeletedBookmark” is being deleted too which is causing me a problem. Please the the code I attached to the main post. I hope you can reply soon. Thanks a lot
Saleem.

Hi again,
Please find attached the output file I am getting after I run the project (testOutput.docx) and the desired output file (Desired testOutput.docx).

Hi Saleem,

Thanks for sharing the detail. Please check the attached DOM image for DeletedBookmark. The BookmarkEnd node is inside Table_DER. Unfortunately, I did not understand the detail of DeleteBookmarkLine method.

In your case, I suggest you please use the following code example to achieve your requirements. You do not need to use DeleteBookmarkLine method. Hope this helps you.

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Using docStream As New FileStream("C:/testInput.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
docReport = New Document(docStream)
builder = New DocumentBuilder(docReport)
End Using
AutoCleanup()
docReport.Range.Bookmarks("Table_DER").Remove()
docReport.Save("C:\testOutput.docx", SaveFormat.Docx)
MsgBox("Done")
End Sub
Private Sub AutoCleanup()
DeleteBookmarkTable("Table_DET")
End Sub
Private Sub DeleteBookmarkTable(ByVal bookmark As String)
Dim bm As Bookmark
bm = docReport.Range.Bookmarks(bookmark)
Dim tbl As Table = bm.BookmarkStart.GetAncestor(NodeType.Table)
tbl.Remove()
End Sub

Hi Tahir,

But I need to delete the line where the “Table_DER” bookmark is added.
For me, a doc is composed of multiple tables, so when I delete a table I need to delete the line above it to not have more than one line between two consecutive tables, that’s what DeleteBookmarkLine function is used for. Please if you have another method to remove this line let me know, I didn’t find a solution for this yet!

I tried to change how I added the bookmarks on table and line but still didn’t succeed, please check it in this post.

Waiting,
Saleem.

Hi Saleem,

Thanks for sharing the detail. Please check the attached DOM image for DeletedBookmark. The BookmarkEnd node of DeletedBookmark bookmark is inside Table_DER.

In this case, you need to add BookmarkEnd node for DeletedBookmark after deleting the paragraph which contain “Table_DER” bookmark and BookmarkEnd node for DeletedBookmark. Please check the highlighted code below. This will fix the issue which you are facing.

Please let us know if you have any more queries.

Private Sub DeleteBookmarkLine(ByVal bookmark As String)
Dim bm As Bookmark
bm = docReport.Range.Bookmarks(bookmark.Replace("_DET", "_DER").Replace("_DUR", "_DER"))
If bm IsNot Nothing Then
Dim bmEnd As BookmarkEnd
If bm.BookmarkStart.ParentNode.NodeType = NodeType.Paragraph Then
Dim para As Paragraph = bm.BookmarkStart.ParentNode
bmEnd = para.GetChild(NodeType.BookmarkEnd, 0, False)
If bm.BookmarkStart.ParentNode.NextSibling.NodeType = NodeType.Paragraph And bmEnd IsNot Nothing Then
para = bm.BookmarkStart.ParentNode.NextSibling
builder.MoveTo(para)
builder.EndBookmark(bmEnd.Name)
builder.EndBookmark("DeletedBookmark")
End If
End If
'bm.BookmarkStart.ParentNode.NextSibling
bm.BookmarkStart.ParentNode.Remove()
End If
End Sub

Hi Tahir,
I’m sorry but I didn’t know that this is the architecture of bookmarks’ starts and ends, I find it weird that a bookmark that starts on a line is opened before a bookmark on previous line is closed !!

All my work rely on bookmarks, so what I need here is a way to preserve bookmarkEnds whenever I delete a cell, delete a row, merge cells, delete table or delete a line.

Whatever bookmarks’ ends in a deleted object I will have to preserve them in a way or another, is it impossible?!

Hi Saleem,

Thanks for your inquiry. Bookmark is a “facade” object that encapsulates two nodes BookmarkStart and BookmarkEnd in a document tree and allows to work with a bookmark as a single object. Please read following documentation links for your kind reference.
https://docs.aspose.com/words/net/working-with-bookmarks/

By Aspose.Words design, bookmark’s nodes (BookmarkStart and BookmarksEnd)
are inline level nodes. This mean that BookmarkStart and BookmarkEnd
can be children of paragraphs only. However, bookmark start and bookmark
end can be placed in different paragraphs.

In your case, I suggest you please check the bookmark start and end nodes of all bookmarks inside a Row, Table or Paragraph before deleting Table, Row or Paragraph. It is possible that two bookmarks may exists in one Paragraph. If you want to delete X bookmark which is inside a Paragraph and that Paragraph contain BookmarkEnd of another bookmark Y, you need to move BookmarkEnd of Y bookmark after that Paragrpah using DocumentBuilder.EndBookmark.

Please let us know if you have any more queries.