Hi Anders,
Thank you for contacting Syncfusion support.
The GetBookmarkContent API of DocIO gets the contents of the bookmark which will be the TextBodyParts such as paragraphs and tables and the paragraph inturn contains text ranges. If your requirement is to modify the bookmark contents then you can use ReplaceBookmarkContent API and if your requirement is to insert new content to the existing bookmark you can use InsertText API of BookmarkNavigator class. Please find the modified code snippets for your reference.
WordDocument document = new WordDocument(); document.OpenReadOnly(FileName, FormatType.Docx); BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document); bookmarkNavigator.MoveToBookmark("Bookmark"); //GetBookmarkContent will get the cloned copy of the bookmark contents and the changes made to the retrieved contents //will not be reflected in the bookmark contents. var textBodyPart = bookmarkNavigator.GetBookmarkContent(); foreach (var childEntity in textBodyPart.BodyItems) { if (childEntity is WParagraph) { var paragraph = childEntity as WParagraph; IWTextRange firsttext = paragraph.AppendText("New text");
firsttext.CharacterFormat.FontSize = 14;
firsttext.CharacterFormat.Bold = true;
firsttext.CharacterFormat.TextColor = Color.Green; } } //Replace the modified bookmark content bookmarkNavigator.ReplaceBookmarkContent(textBodyPart); document.Save(FileName, FormatType.Docx); |
Hi Anders,
The bookmark contents are retrieved as TextBodyPart which includes paragraphs, tables and the formatting of the entire bookmark contents cannot be retrieved as single formatting. You can use the InsertText API of BookmarkNavigator class to preserve the same formatting for the newly inserted text. Please find the following API documentation links for preserving the existing formatting of the newly inserted text to the bookmark contents.
InsertText:
http://help.syncfusion.com/cr/cref_files/file-formats/docio/Syncfusion.DocIO.Base~Syncfusion.DocIO.DLS.BookmarksNavigator~InsertText(String,Boolean).html
ReplaceBookmarkContent:
http://help.syncfusion.com/cr/cref_files/file-formats/docio/Syncfusion.DocIO.Base~Syncfusion.DocIO.DLS.BookmarksNavigator~ReplaceBookmarkContent(String,Boolean).html
The InsertText API will return a new text range instance to which you can define new font formattings. If your requirement to modify the formatting of the newly inserted text other than the existing format or default format,.then you can define separate formattings to the newly inserted text. Please find the following code snippets for your reference.
IWTextRange text = bookmarkNavigator.InsertText("New text"); text.CharacterFormat.TextColor = Color.Green; |