We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Proper way to remove bookmark and content?

I’m trying to figure out what is the best way to remove bookmarks and their content. bookmark.Text = “”; or RemoveBookmarkWithContent( bookmark ); ?

Easiest place to look for discrepancies, is in the AcmeStmt.doc under the ‘Compensation’ section, except for ‘Base Pay’, each row is bookmarked. It also contains the trailing empty paragraph so that when appropriate bookmarks/content are removed, the section should be rendered remaining line items with each having a blank paragraph following it.
You can run my sample code both ways to see different behaviors (and maybe my source document is corrupt, but I’m hoping not).

I have the following questions/concerns:

  1. I read this post: https://blog.aspose.com/2007/03/13/70192 but am curious why the code seems so ‘greedy’ in that it removes ‘extra’ stuff (as you’ll see in my demo).

For example, the code even states “// Bookmark nodes located immediately before start of our bookmark should also be included in the removal process.”. Why are these included? If you run my sample code using the ‘RemoveBookmarkWithContent’ method, you’ll see that some of the paragraphs between line items are removed. Some remain (correctly) and I can’t figure out what the pattern is. This illustrates some sort of problem, but you can then run my code using the bookmark.Text = “”; to compare the resulting documents and see all the differences. Note: the current code only attempts to clear/remove bookmarks in that first Compensation section, but if you uncomment the line that starts with // bookmarksToDelete = bookmarksToDelete.Concat you will then see full document processed and see other problem areas if you compare the output documents.

  1. I read this post: http://stackoverflow.com/questions/9776030/aspose-words-delete-nodes-in-a-bookmark where Shahzad Latif seems to say bookmark.Text = “”;

The reason I’m asking is that we are having other problems that I won’t go into when using bookmark.Text = “” and wondering if RemoveBookmarkWithContent is the ‘correct’ way and we should get that working and have our ‘document authors’ then adjust the way they do bookmarking to fit the appropriate pattern.

  1. I opened my document with your sample Document Explorer app and if you examine the ‘Compensation’ section, I’m confused if I have corrupt document or what. But if you look at the Word document, it looks like none of the bookmarks overlap. Not even on same lines. But if you look in your explorer, and almost every one of the bookmarks in that little section overlap by ‘one node’. Wondering if that is leading to any problems.

Thanks in advance,
Terry

Hi Terry,

Thanks for your inquiry. In case you are using an older version of Aspose.Words, I would suggest you please upgrade to the latest version (v15.4.0) from here:
https://releases.aspose.com/words/net

I suggest you please use Bookmark.Text = “” to remove all the contents of bookmark. The exception mentioned in this blog post is not thrown while using latest version of Aspose.Words for .NET 15.4.0.

Document doc = new Document(MyDir + "AcmeStmt.doc");
var bookmarksToDelete = new[] { "compPremiumPay", "compOvertime", "compAllowances", "compAchievement", "compLS", "compOther" };
foreach (var b in bookmarksToDelete)
{
    var mark = doc.Range.Bookmarks[b];
    // Some bookmarks are nested and removed when parent removed, so have to check if still there
    if (mark != null)
    {
        mark.Text = "";
        mark.Remove();
    }
}
doc.Save(MyDir + "Out.docx");

terry.aney:

  1. I opened my document with your sample Document Explorer app and if you examine the ‘Compensation’ section, I’m confused if I have corrupt document or what. But if you look at the Word document, it looks like none of the bookmarks overlap. Not even on same lines. But if you look in your explorer, and almost every one of the bookmarks in that little section overlap by ‘one node’. Wondering if that is leading to any problems.

Could you please share the overlapped bookmark names which you observed in Document Explorer? I will investigate the issue and provide you more information on this.

Thanks for the response.

I’ve attached a file showing ‘overlapping’ (at least how I interpret it) bookmarks in that ‘Compensation’ section.

Hi Terry,

Thanks for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does. Please convert your document to Docx using MS Word and check the document.xml. The BookmarkStart - “compOvertime” comes before BookmarkEnd - “compPremiumPay”. See the attached image and document.xml for your kind reference.

Please let us know if you have any more queries.

Weird that Word does that. You can see in the Word document that the bookmarks aren’t even on the same line, so I don’t know why Word is saying they are overlapping
I’ve posted another problem at Removing Bookmark: Cannot insert a node of this type at this location and I wonder if these ‘overlapping’ bookmarks are the culprit?

Hi Terry,

Thanks for your inquiry. In this case, I suggest you please use RemoveBookmarkWithContent method. Please let us know if you have any more queries.

Hi, I was reviewing this post again, and was trying to find attached code and images. Am I no longer able to access those?

@terry.aney
This is old thread and unfortunately there is no attachment in this thread. Could you please share some detail about your requirement? We will then provide you code example accordingly.