Class BookmarksNavigator
Helps to navigate between bookmarks in the Word document and manipulate its contents.
Inheritance
Namespace: Syncfusion.DocIO.DLS
Assembly: Syncfusion.DocIO.NET.dll
Syntax
public class BookmarksNavigator : Object
Constructors
BookmarksNavigator(IWordDocument)
Initializes a new instance of the BookmarksNavigator class with the specified WordDocument.
Declaration
public BookmarksNavigator(IWordDocument doc)
Parameters
Type | Name | Description |
---|---|---|
IWordDocument | doc | The WordDocument object. |
Examples
//Loads an existing Word document into DocIO instance
WordDocument document = new WordDocument("Bookmarks.docx", FormatType.Docx);
//Creates the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Moves the virtual cursor to the location before the end of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Gets the bookmark content as WordDocumentPart
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();
//Saves the WordDocumentPart as separate Word document
WordDocument newDocument = wordDocumentPart.GetAsWordDocument();
newDocument.Save("Result.docx", FormatType.Docx);
//Releases the resources hold by WordDocument instance
newDocument.Close();
document.Close();
'Load an existing Word document into DocIO instance
Dim document As New WordDocument("Bookmarks.docx", FormatType.Docx)
'Creates the bookmark navigator instance to access the bookmark
Dim bookmarkNavigator As New BookmarksNavigator(document)
'Moves the virtual cursor to the location before the end of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Gets the bookmark content as WordDocumentPart
Dim wordDocumentPart As WordDocumentPart = bookmarkNavigator.GetContent()
'Saves the WordDocumentPart as separate Word document
Dim newDocument As WordDocument = wordDocumentPart.GetAsWordDocument()
newDocument.Save("Result.docx", FormatType.Docx)
'Releases the resources hold by WordDocument instance
newDocument.Close()
document.Close()
Properties
CurrentBookmark
Gets the current bookmark.
Declaration
public Bookmark CurrentBookmark { get; }
Property Value
Type | Description |
---|---|
Bookmark | The current Bookmark object. |
CurrentBookmarkItem
Gets the current bookmark item which can be a BookmarkStart or BookmarkEnd. Read-only.
Declaration
public IParagraphItem CurrentBookmarkItem { get; }
Property Value
Type | Description |
---|---|
IParagraphItem | The IParagraphItem that specifies whether it is BookmarkStart or BookmarkEnd. |
Document
Gets or sets the Word document of this object.
Declaration
public IWordDocument Document { get; set; }
Property Value
Type | Description |
---|---|
IWordDocument | The IWordDocument object. |
Methods
DeleteBookmarkContent(Boolean)
Deletes the bookmark content.
Declaration
public void DeleteBookmarkContent(bool saveFormatting)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | saveFormatting | True if its save formatting, otherwise false. |
Examples
This example shows how to remove the contents of a bookmark from 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 the virtual cursor to the before the end location of the bookmark " Northwind "
bookmarkNavigator.MoveToBookmark("Northwind");
//Delete bookmark content without deleting the format in the target document.
bookmarkNavigator.DeleteBookmarkContent(false);
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 the virtual cursor to the before the end location of the bookmark " Northwind "
bookmarkNavigator.MoveToBookmark("Northwind")
'Delete bookmark content without deleting the format in the target document.
bookmarkNavigator.DeleteBookmarkContent(False)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
DeleteBookmarkContent(Boolean, Boolean)
Deletes the bookmark content and removes the empty paragraph after deletion.
Declaration
public void DeleteBookmarkContent(bool saveFormatting, bool removeEmptyParagraph)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | saveFormatting | True if to save formatting; otherwise, false. |
System.Boolean | removeEmptyParagraph | True if to remove paragraph with bookmark start and end when it is empty after deletion; otherwise, false. |
Remarks
This method will be removed in future version. As a work around to remove bookmarked paragraph, utilize current bookmark property of bookmark navigator to access the current bookmarked paragraph and then remove its index from its owner (Text Body) collection.
GetBookmarkContent()
Returns the bookmark content as a TextBodyPart, which represents the collection of body items if the bookmark start and bookmark end are preserved in a single section.
Declaration
public TextBodyPart GetBookmarkContent()
Returns
Type | Description |
---|---|
TextBodyPart | The TextBodyPart that represents the bookmark content. |
Examples
This example shows how to retrieve the specified bookmark content using GetBookmarkContent method in a 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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Get the bookmark content
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
//Add the retrieved content into another new section
document.AddSection();
foreach(TextBodyItem item in part.BodyItems)
document.LastSection.Body.ChildEntities.Add(item);
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Get the bookmark content
Dim part As TextBodyPart = bookmarkNavigator.GetBookmarkContent()
'Add the retrieved content into another new section
document.AddSection()
For Each item As TextBodyItem In part.BodyItems
document.LastSection.Body.ChildEntities.Add(item)
Next
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
GetContent()
Gets the bookmark content as WordDocumentPart, which represents the collection of sections if the bookmark start and bookmark end are preserved in different sections .
Declaration
public WordDocumentPart GetContent()
Returns
Type | Description |
---|---|
WordDocumentPart | The WordDocumentPart that represents the bookmark content. |
Examples
This example shows how to retrieve the specified bookmark content using GetContent method in a Word document.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//Load the template document with bookmark "Northwind" whose start and end preserved in different section
WordDocument document = new WordDocument("Template.docx", FormatType.Docx);
//Create the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
//Move the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Get the bookmark content as WordDocumentPart
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();
//Save the WordDocumentPart as separate Word document
WordDocument newDocument = wordDocumentPart.GetAsWordDocument();
newDocument.Save("Result.docx", FormatType.Docx);
//Release the resources hold by WordDocument instance
newDocument.Close();
document.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
'Load the template document with bookmark "Northwind" whose start and end preserved in different section
Dim document As New WordDocument("Template.docx", FormatType.Docx)
'Create the bookmark navigator instance to access the bookmark
Dim bookmarkNavigator As New BookmarksNavigator(document)
'Move the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Get the bookmark content as WordDocumentPart
Dim wordDocumentPart As WordDocumentPart = bookmarkNavigator.GetContent()
'Save the WordDocumentPart as separate Word document
Dim newDocument As WordDocument = wordDocumentPart.GetAsWordDocument()
newDocument.Save("Result.docx", FormatType.Docx)
'Release the resources hold by WordDocument instance
newDocument.Close()
document.Close()
End Sub
InsertParagraph(IWParagraph)
Inserts the specified IWParagraph to current bookmark.
Declaration
public void InsertParagraph(IWParagraph paragraph)
Parameters
Type | Name | Description |
---|---|---|
IWParagraph | paragraph | The IWParagraph to be inserted. |
Examples
The following code example demonstrates how to insert a paragraph using BookmarkNavigator.
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 after the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", false, true);
//Insert a new paragraph before the bookmark start
IWParagraph paragraph = new WParagraph(document);
paragraph.AppendText("Northwind Database is a set of tables containing data fitted into predefined categories.");
bookmarkNavigator.InsertParagraph(paragraph);
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 after the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", False, True)
'Insert a new paragraph before the bookmark start
Dim paragraph As IWParagraph = New WParagraph(document)
paragraph.AppendText("Northwind Database is a set of tables containing data fitted into predefined categories.")
bookmarkNavigator.InsertParagraph(paragraph)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
InsertParagraphItem(ParagraphItemType)
Inserts the specified IParagraphItem to current bookmark.
Declaration
public IParagraphItem InsertParagraphItem(ParagraphItemType itemType)
Parameters
Type | Name | Description |
---|---|---|
ParagraphItemType | itemType | The type of the paragraph item to be inserted. |
Returns
Type | Description |
---|---|
IParagraphItem | The IParagraphItem object that represents the inserted item. |
Examples
The following code example demonstrates how to insert a paragraph item using BookmarkNavigator.
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 after the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", false, true);
//Insert a new picture after the bookmark end
WPicture picture = bookmarkNavigator.InsertParagraphItem(ParagraphItemType.Picture) as WPicture;
picture.LoadImage(Image.FromFile("Northwind.png"));
picture.WidthScale = 50;
picture.HeightScale = 50;
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 after the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", False, True)
'Insert a new picture after the bookmark end
Dim picture As WPicture = TryCast(bookmarkNavigator.InsertParagraphItem(ParagraphItemType.Picture), WPicture)
picture.LoadImage(Image.FromFile("Northwind.png"))
picture.WidthScale = 50
picture.HeightScale = 50
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
InsertTable(IWTable)
Inserts the specified IWTable to current bookmark.
Declaration
public void InsertTable(IWTable table)
Parameters
Type | Name | Description |
---|---|---|
IWTable | table | The IWTable to be inserted. |
Examples
The following code example demonstrates how to insert a table using BookmarkNavigator.
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 end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", false, false);
//Insert a new paragraph before the bookmark end
IWParagraph paragraph = new WParagraph(document);
paragraph.AppendText("Northwind Database Contains the following tables:");
bookmarkNavigator.InsertParagraph(paragraph);
//Insert a new table before the bookmark end
WTable table = new WTable(document);
table.ResetCells(3, 2);
table[0, 0].AddParagraph().AppendText("Suppliers");
table[0, 1].AddParagraph().AppendText("2");
table[1, 0].AddParagraph().AppendText("Customers");
table[1, 1].AddParagraph().AppendText("1");
table[2, 0].AddParagraph().AppendText("Employees");
table[2, 1].AddParagraph().AppendText("3");
bookmarkNavigator.InsertTable(table);
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 end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", False, False)
'Insert a new paragraph before the bookmark end
Dim paragraph As IWParagraph = New WParagraph(document)
paragraph.AppendText("Northwind Database Contains the following tables:")
bookmarkNavigator.InsertParagraph(paragraph)
'Insert a new table before the bookmark end
Dim table As New WTable(document)
table.ResetCells(3, 2)
table(0, 0).AddParagraph().AppendText("Suppliers")
table(0, 1).AddParagraph().AppendText("2")
table(1, 0).AddParagraph().AppendText("Customers")
table(1, 1).AddParagraph().AppendText("1")
table(2, 0).AddParagraph().AppendText("Employees")
table(2, 1).AddParagraph().AppendText("3")
bookmarkNavigator.InsertTable(table)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
InsertText(String)
Inserts the specified text to current bookmark.
Declaration
public IWTextRange InsertText(string text)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string that represents the text to be inserted. |
Returns
Type | Description |
---|---|
IWTextRange | The IWTextRange object that represents the inserted text. |
Examples
This example shows how to insert a simple text using BookmarkNavigator.
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Insert a new text before the bookmark end of the bookmark
bookmarkNavigator.InsertText(" Northwind Database is a set of tables containing data fitted into predefined categories.");
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Insert a new text before the bookmark end of the bookmark
bookmarkNavigator.InsertText(" Northwind Database is a set of tables containing data fitted into predefined categories.")
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
InsertText(String, Boolean)
Inserts the specified text to current bookmark along with its formatting.
Declaration
public IWTextRange InsertText(string text, bool saveFormatting)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string that represents the text to be inserted. |
System.Boolean | saveFormatting | True if it is save formatting, otherwise false. |
Returns
Type | Description |
---|---|
IWTextRange | The IWTextRange object that represents the inserted text. |
Examples
This example shows how to insert a simple text along with its formatting using BookmarkNavigator.
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Insert a new text before the bookmark end of the bookmark
bookmarkNavigator.InsertText(" Northwind Database is a set of tables containing data fitted into predefined categories.", true);
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Insert a new text before the bookmark end of the bookmark
bookmarkNavigator.InsertText(" Northwind Database is a set of tables containing data fitted into predefined categories.", True)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
InsertTextBodyPart(TextBodyPart)
Inserts the specified TextBodyPart to current bookmark.
Declaration
public void InsertTextBodyPart(TextBodyPart bodyPart)
Parameters
Type | Name | Description |
---|---|---|
TextBodyPart | bodyPart | The TextBodyPart to be inserted. |
Examples
The following code example demonstrates how to insert a text body part using BookmarkNavigator.
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");
paragraph.AppendBookmarkEnd("bm_empty");
//Move to the virtual cursor after the start location of the bookmark "bm_empty"
bookmarkNavigator.MoveToBookmark("bm_empty", true, true);
//Insert the text body part after the bookmark start
bookmarkNavigator.InsertTextBodyPart(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 after the start location of the bookmark "bm_empty"
bookmarkNavigator.MoveToBookmark("bm_empty", True, True)
'Insert the text body part after the bookmark start
bookmarkNavigator.InsertTextBodyPart(textBodyPart)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
MoveToBookmark(String)
Moves to the specified bookmark in the document.
Declaration
public void MoveToBookmark(string bookmarkName)
Parameters
Type | Name | Description |
---|---|---|
System.String | bookmarkName | The string that specifies the bookmark name. |
Examples
The following code example demonstrates how to move to specific bookmark in the 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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Get the bookmark content
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
//Add the retrieved content into another new section
document.AddSection();
foreach(TextBodyItem item in part.BodyItems)
document.LastSection.Body.ChildEntities.Add(item);
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Get the bookmark content
Dim part As TextBodyPart = bookmarkNavigator.GetBookmarkContent()
'Add the retrieved content into another new section
document.AddSection()
For Each item As TextBodyItem In part.BodyItems
document.LastSection.Body.ChildEntities.Add(item)
Next
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The specified bookmark name is not present in the Word document. |
MoveToBookmark(String, Boolean, Boolean)
Moves to the start or end position of the specified bookmark in the document.
Declaration
public void MoveToBookmark(string bookmarkName, bool isStart, bool isAfter)
Parameters
Type | Name | Description |
---|---|---|
System.String | bookmarkName | The string that specifies the bookmark name. |
System.Boolean | isStart | Set to true to move the navigator at the beginning of the bookmark; Set to false to move the navigator at the end of the bookmark. |
System.Boolean | isAfter | Set to true to move the navigator after the specified bookmark start or end position; Set to false to move the navigator before the specified bookmark start or end position. |
Examples
The following code example demonstrates how to move to specific bookmark in the 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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", true, false);
//Get the bookmark content
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
//Add the retrieved content into another new section
document.AddSection();
foreach(TextBodyItem item in part.BodyItems)
document.LastSection.Body.ChildEntities.Add(item);
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 the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind", True, False)
'Get the bookmark content
Dim part As TextBodyPart = bookmarkNavigator.GetBookmarkContent()
'Add the retrieved content into another new section
document.AddSection()
For Each item As TextBodyItem In part.BodyItems
document.LastSection.Body.ChildEntities.Add(item)
Next
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
Exceptions
Type | Condition |
---|---|
System.ArgumentException | The specified bookmark name is not present in the Word document. |
ReplaceBookmarkContent(TextBodyPart)
Replaces the bookmark content with the specified TextBodyPart.
Declaration
public void ReplaceBookmarkContent(TextBodyPart bodyPart)
Parameters
Type | Name | Description |
---|---|---|
TextBodyPart | 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.
Examples
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");
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();
}
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
ReplaceBookmarkContent(String, Boolean)
Replaces the content of the bookmark with the specified text and formatting.
Declaration
public void ReplaceBookmarkContent(string text, bool saveFormatting)
Parameters
Type | Name | Description |
---|---|---|
System.String | text | The string that specifies the text. |
System.Boolean | saveFormatting | True if to save formatting, otherwise false. |
Examples
The following code example demonstrates how to replace a specified bookmark content with simple text along with its formatting.
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);
document.AddSection();
IWParagraph paragraph = document.LastSection.AddParagraph();
//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(" Northwind Database is a set of tables containing data fitted into predefined categories.", true);
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)
document.AddSection()
Dim paragraph As IWParagraph = document.LastSection.AddParagraph()
'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(" Northwind Database is a set of tables containing data fitted into predefined categories.", True)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub
ReplaceContent(WordDocumentPart)
Replaces the content of the bookmark with the specified WordDocumentPart.
Declaration
public void ReplaceContent(WordDocumentPart documentPart)
Parameters
Type | Name | Description |
---|---|---|
WordDocumentPart | documentPart | The WordDocumentPart 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.
Examples
This example shows how to replace a specified bookmark content using ReplaceContent method in Word document.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
//Load the template document with bookmark "Northwind" whose start and end preserved in different section
WordDocument templateDocument = new WordDocument("Template.docx", FormatType.Docx);
//Create the bookmark navigator instance to access the bookmark
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(templateDocument);
//Move the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind");
//Get the bookmark content as WordDocumentPart
WordDocumentPart wordDocumentPart = bookmarkNavigator.GetContent();
//Close the template document
templateDocument.Close();
//Load the Word document with bookmark NorthwindDB
WordDocument document = new WordDocument("Bookmarks.docx", FormatType.Docx);
//Create the bookmark navigator instance to access the bookmark
bookmarkNavigator = new BookmarksNavigator(document);
//Move the virtual cursor to the before the end location of the bookmark "NorthwindDB"
bookmarkNavigator.MoveToBookmark("NorthwindDB");
//Replace the bookmark content with word body part
bookmarkNavigator.ReplaceContent(wordDocumentPart);
document.Save("Result.docx", FormatType.Docx);
document.Close();
}
Private Sub button_Click(sender As Object, e As EventArgs)
'Load the template document with bookmark "Northwind" whose start and end preserved in different section
Dim templateDocument As New WordDocument("Template.docx", FormatType.Docx)
'Create the bookmark navigator instance to access the bookmark
Dim bookmarkNavigator As New BookmarksNavigator(templateDocument)
'Move the virtual cursor to the before the end location of the bookmark "Northwind"
bookmarkNavigator.MoveToBookmark("Northwind")
'Get the bookmark content as WordDocumentPart
Dim wordDocumentPart As WordDocumentPart = bookmarkNavigator.GetContent()
'Close the template document
templateDocument.Close()
'Load the Word document with bookmark NorthwindDB
Dim document As New WordDocument("Bookmarks.docx", FormatType.Docx)
'Create the bookmark navigator instance to access the bookmark
bookmarkNavigator = New BookmarksNavigator(document)
'Move the virtual cursor to the before the end location of the bookmark "NorthwindDB"
bookmarkNavigator.MoveToBookmark("NorthwindDB")
'Replace the bookmark content with word body part
bookmarkNavigator.ReplaceContent(wordDocumentPart)
document.Save("Result.docx", FormatType.Docx)
document.Close()
End Sub