How to Edit/Replace the Content of Word Bookmark with HTML Code

Bookmark can locate a range. Assuming the content of the range is some html code, how to change the content of the range. Spire.Doc supports bookmarks. And you can use Spire.Doc to fulfill the job.

In this article, a solution will be introduced. Spire.Doc provides you a method:

[C#]
public void ReplaceBookmarkContent(TextBodyPart bodyPart)

Replace the content of bookmark with TextBodyPart bodyPart.

This method cannot handle html code directly. Here is what to do. First load the new html code to document. Then select the newly added data as TextBodyPart. At last, call the method to replace the content of the bookmark.

Step 1: Add bookmarks containing html code.

[C#]
Paragraph p2 = section.AddParagraph();
p2.AppendBookmarkStart("bookmark2");
p2.AppendHTML("<p><font color='blue'>This para is also Generated by Decoding HTML Code</font></p>");
p2.AppendBookmarkEnd("bookmark2");

Step 2: Add new html code to document.

[C#]
Section tempSection = document.AddSection();
String html
= "<p>This Bookmark has been <font color=\"#ff0000\">Edited</font></p>";
tempSection.AddParagraph().AppendHTML(html);

Step 3: Get the new added html as TextBodyPart.

[C#]
ParagraphBase replacementFirstItem = tempSection.Paragraphs[0].Items.FirstItem as ParagraphBase;
ParagraphBase replacementLastItem  = tempSection.Paragraphs[tempSection.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
TextBodySelection selection = new TextBodySelection(replacementFirstItem, replacementLastItem);
TextBodyPart part = new TextBodyPart(selection);

Step 4: Locate the bookmark "bookmark2" and call the method ReplaceBookmarkContent to replace the content. Then remove the temp section.

[C#]
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//locate the bookmark
bookmarkNavigator.MoveToBookmark("bookmark2");
//replace the content of bookmark
bookmarkNavigator.ReplaceBookmarkContent(part);
//remove temp section
document.Sections.Remove(tempSection);

Preview the original screenshot:

Edit and replace bookmark with HTML

Preview the effect screenshot:

Edit and replace bookmark with HTML

Here comes to the full code:

[C#]
static void Main(string[] args)
{
 Document document = new Document();
 Section section = document.AddSection();

 //create bookmarks
 Paragraph p1 = section.AddParagraph();
 p1.AppendBookmarkStart("bookmark1");
 p1.AppendHTML("<p><font color='blue'>This para is also Generated by Decoding HTML Code</font></p>");
 p1.AppendBookmarkEnd("bookmark1");

 Paragraph p2 = section.AddParagraph();
 p2.AppendBookmarkStart("bookmark2");
 p2.AppendHTML("<p><font color='blue'>This para is also Generated by Decoding HTML Code</font></p>");
 p2.AppendBookmarkEnd("bookmark2");
 document.SaveToFile("BeforeReplace.doc");

 //create a temp section to contain multiple paragraph.
 Section tempSection = document.AddSection();
 String html
 = "<p>This Bookmark has been <font color=\"#ff0000\">Edited</font></p>";
 tempSection.AddParagraph().AppendHTML(html);  
          
 ParagraphBase replacementFirstItem = tempSection.Paragraphs[0].Items.FirstItem as ParagraphBase;
 ParagraphBase replacementLastItem = tempSection.Paragraphs[tempSection.Paragraphs.Count - 1].Items.LastItem as ParagraphBase;
 TextBodySelection selection = new TextBodySelection(replacementFirstItem, replacementLastItem);
 TextBodyPart part = new TextBodyPart(selection);

 BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
 //locate the bookmark
 bookmarkNavigator.MoveToBookmark("bookmark2");
 //replace the content of bookmark
 bookmarkNavigator.ReplaceBookmarkContent(part);
 //remove temp section
 document.Sections.Remove(tempSection);

 document.SaveToFile(@"AfterReplace.doc");
}