Syncfusion.DocIO.Base
Example 


The TextBodyPart to replace the bookmark contents.

ReplaceBookmarkContent(TextBodyPart) Method

Replaces the bookmark content with the specified TextBodyPart.
Syntax
'Declaration
 
Public Overloads Sub ReplaceBookmarkContent( _
   ByVal bodyPart As TextBodyPart _
) 
'Usage
 
Dim instance As BookmarksNavigator
Dim bodyPart As TextBodyPart
 
instance.ReplaceBookmarkContent(bodyPart)
public void ReplaceBookmarkContent( 
   TextBodyPart bodyPart
)

Parameters

bodyPart
The TextBodyPart to replace the bookmark contents.
Remarks

You cannot replace the multi section contents into a bookmark within table in Word documents.

Use for loop instead of foreach loop for iterating through document elements when replacing the bookmark contents to avoid �collection modified exception�. Since there is chance for modification in document elements while replacing the bookmark contents.

Example

The following code example demonstrates how to replace a specified bookmark content in the Word document.

private void Button1_Click(System.Object sender, System.EventArgs e)
{
    WordDocument document = new WordDocument("Bookmarks.docx", FormatType.Docx);
    //Create the bookmark navigator instance to access the bookmark
    BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
    //Move to the virtual cursor before the bookmark end location of the bookmark "Northwind"
    bookmarkNavigator.MoveToBookmark("Northwind");
    //Get the bookmark content
    TextBodyPart textBodyPart = bookmarkNavigator.GetBookmarkContent();
    document.AddSection();
    IWParagraph paragraph = document.LastSection.AddParagraph();
    paragraph.AppendText("Northwind Database is a set of tables containing data fitted into predefined categories.");
    //Add the new bookmark into Word document
    paragraph.AppendBookmarkStart("bm_empty");
    .AppendBookmarkEnd("bm_empty");
    //Move to the virtual cursor before the end location of the bookmark "bm_empty"
    bookmarkNavigator.MoveToBookmark("bm_empty");
    //Replace the bookmark content with text body part
    bookmarkNavigator.ReplaceBookmarkContent(textBodyPart);
    document.Save("Result.docx", FormatType.Docx);
    document.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
    Dim document As New WordDocument("Bookmarks.docx", FormatType.Docx)
    'Create the bookmark navigator instance to access the bookmark
    Dim bookmarkNavigator As New BookmarksNavigator(document)
    'Move to the virtual cursor before the bookmark end location of the bookmark "Northwind"
    bookmarkNavigator.MoveToBookmark("Northwind")
    'Get the bookmark content
    Dim textBodyPart As TextBodyPart = bookmarkNavigator.GetBookmarkContent()
    document.AddSection()
    Dim paragraph As IWParagraph = document.LastSection.AddParagraph()
    paragraph.AppendText("Northwind Database is a set of tables containing data fitted into predefined categories.")
    'Add the new bookmark into Word document
    paragraph.AppendBookmarkStart("bm_empty")
    paragraph.AppendBookmarkEnd("bm_empty")
    'Move to the virtual cursor before the end location of the bookmark "bm_empty"
    bookmarkNavigator.MoveToBookmark("bm_empty")
    'Replace the bookmark content with text body part
    bookmarkNavigator.ReplaceBookmarkContent(textBodyPart)
    document.Save("Result.docx", FormatType.Docx)
    document.Close()
End Sub
AssemblyVersion

Syncfusion.DocIO.Base: 16.4460.0.52

See Also