The problem of bookmark lost

           <a class="attachment" href="/uploads/default/4267">1.zip</a> (6.5 KB)

           When the following code is executed,
           The bookmark named Bookmark1 is missing


            string SavePath = Application.StartupPath + "\\1.doc";
            Document MainDoc = new Document(SavePath); 
            mainDocBuilder = new DocumentBuilder(MainDoc);
            mainDocBuilder.MoveToBookmark("Bookmark2", true, true);
            mainDocBuilder.CurrentParagraph.Remove(); 
            MainDoc.Save(SavePath);

@IORInet,

Thanks for your inquiry. You are moving the cursor to the bookmark and its parent node is paragraph. DocumentBuilder.CurrentParagraph property gets the paragraph that is currently selected in this DocumentBuilder. After moving the cursor to the bookmark, you are removing it parent node. This also removes the bookmark that is child node of current paragraph.

The bookmark named “Bookmark1” is in the first paragraph
The bookmark named “Bookmark2” is in the second paragraph
When I remove the second paragraph ,then save the document
The bookmark named “Bookmark1” in the first paragraph is lost

@IORInet,

Thanks for your inquiry. Could you please ZIP attach your input Word document here for testing? We will investigate the issue on our side and provide you more information.

document.zip (6.5 KB)

@IORInet,

Thanks for sharing the document. You are facing the expected behavior of Aspose.Words. The BookmarkEnd node of bookmark1 is inside second paragraph in your document. Please iterate through all node of second paragraph to check this.

Document MainDoc = new Document(MyDir + "1.doc");
DocumentBuilder mainDocBuilder = new DocumentBuilder(MainDoc);
mainDocBuilder.MoveToBookmark("Bookmark2", true, true);

foreach (Node node in mainDocBuilder.CurrentParagraph.ChildNodes)
{
    Console.WriteLine(node.NodeType);
}

Open 1.doc used by word 2003,you can see the bookmark of bookmark1 is only inside first paragraph.
Then delete the second paragraph used by word 2003,the bookmark of bookmark1 Still exist,don’t lost.

But Open 1.doc used by Aspose.words,the BookmarkEnd node of bookmark1 is inside second paragraph.
What do I need to do to remove the second paragraph, and the bookmark of bookmark1 won’t be lost?

@IORInet,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

To check this scenario in MS Word, please save your document to DOCX using MS Word. Rename the .docx to .zip and unzip it. Please check the document.xml for detail of bookmarks. See the attached image for detail. document.xml.png (17.2 KB) We have also attached the zip version of output document with this post. output_msword.zip (15.2 KB). Hope this helps you.