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

# How to delete HTML content of a bookmark

Hello!

We are using the DocumentBuilder.InsertHtml() to insert a HTML table into a bookmark in a Word document, and this works just fine. However, when we regenerate the Word document, we want to delete the existing table and insert a new version of the table into the same bookmark. But when we use the command bookmark.Text = string.empty, it does not clear the bookmark. So instead we get two tables instead of one.

See the attached word document for an example.

So the ultimate question is, is there in any way possible to delete this table? Or is there another way of inserting the HTML which makes is possible to delete it later?

Hi
Roar,

Thanks for your inquiry. First of all, you can insert a HTML table at some bookmark place in Word document by using the following code snippet:

Document
doc = new Document(@“c:\test\input.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark(“bm”);
builder.InsertHtml(
 cell 1 cell 2 cell 3 cell 4
);

doc.Save(@“c:\test\out.docx”);

Also, please note that when you use code like above, we place cursor between BookmarkStart and BookmarkEnd node, and then insert table between these fields. Main thing is that a paragraph and a table are on the same flat of the nodes hierarchy. So after inserting a table BookmarkStart and BookmarkEnd are placed in the paragraph below table. You can see this if you will open document using DocumentExplorer (Aspose demo app).

Secondly, as can be seen above you can not directly delete old table by using bookmark.Text = string.empty.

Best Regards,

Hello!

Thanks for quick reply. It guided me on the right path.

But I have another question. If I use

builder.MoveToBookmark(“bm”);
var bookmark = documentBuilder.Document.Range.Bookmarks[mappingCellItem.BookMarkName];

I know that the table is now the previousSibling
var table = bookmark.BookmarkStart.ParentNode.PreviousSibling;

But can I be 100% sure that this table belongs to the bookmark “bm”? Or is there another way of getting a reference to the table for the bookmark “bm”?

Hi there,

I think what Awais was trying to say is the bookmark was not properly inserted to encompass the table. Using the current structure you can’t really be sure that the previous node from the bookmark will always be a table.

Instead, could you please clarify how you are creating the bookmark? If you are inserting it dynamically then you need to insert the bookmark start at the paragraph before the table, and the bookmark end at the paragraph after the table.

Thanks,

Hi again.

We do not create the bookmarks dynamically, we use a word-template with predefined bookmarks.

I think our problem is that when using .InsertHtml(), the table is not inserted inside the bookmark and can therefore (as Awais says) not be deleted directly.

We believe the solution to this problem is to get some kind of reference to the table, so that we can make sure that we are deleting the right table.

Or is there another option?

Hi Roar,

Best Regards,

Hi there,

In your case I think you can safely find the previous table of the bookmark and delete it as shown in the previous posts.

Thanks,

Hi.

As mentioned earlier our problem has been that when using .InsertHtml, the table is not inserted inside the bookmark and we could not delete it by using bookmark.Text = string.Empty;. We just found that if we insert a linebreak before the bookmark in our word-template, we can actually delete the table by using bookmark.Text = string.Empty;.

Then everything seems to be working perfectly, but we don`t really understand why.

Hi
Roar,

Thanks for the additional information. I am afraid the trick of inserting a line break before the Bookmark is actually not working on my side. As suggested by Adam earlier, you can navigate to the previous table of bookmark and delete it safely. Moreover, when inserting a table via InsertHtml method, we don’t actually have much control so that an element (Node etc) could be placed inside table for future referencing .

Best Regards,

Hi

Thanks for your request. Have you tried using code like this?

[Test]

public void Test001()

{

Document doc = new Document(@"c:\temp\input.docx");

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark("bm", true, true);

builder.InsertHtml("

 cell 1 cell 2 cell 3 cell 4
");

doc.Save(@"c:\temp\out.docx");

}

[Test]

public void Test002()

{

Document doc = new Document(@"c:\temp\out.docx");

doc.Range.Bookmarks["bm"].Text = "";

doc.Save(@"c:\temp\out1.docx");

}

This code seems to work as expected on my side.

Best regards,