New to Telerik Document Processing? Download free 30-day trial

Change Bookmark Content While Preserving Formatting using WordsProcessing

Product Version Product Author
2025.4.1104 RadWordsProcessing Yoan Karamanov

Description

This article shows how to change the content of an existing Bookmark in a DOCX document while preserving the original text formatting and character properties using the WordsProcessing library.

Solution

  • Import DOCX: Use DocxFormatProvider to read the input DOCX and obtain a RadFlowDocument.
  • Initialize editor: Create a RadFlowDocumentEditor for cursor movement and editing.
  • Find bookmark: Enumerate BookmarkRangeStart elements and select the bookmark by Name.
  • Capture formatting: Get the first Run within the bookmark and copy its CharacterFormatting properties.
  • Delete original bookmark content: Delete only the content between the start and end markers while keeping the bookmark structure intact.
  • Position cursor: Move the editor back to the start of the bookmark to insert new text in place.
  • Copy formatting: Apply the formatting of the original bookmark content to the editor properties.
  • Insert text: Add the replacement content.
  • Export DOCX: Write the updated document using DocxFormatProvider.

[C#] Replace bookmark content but keep formatting

RadFlowDocument document;
DocxFormatProvider docxFormatProvider = new DocxFormatProvider();

using (Stream input = File.OpenRead("input.docx"))
{
    document = docxFormatProvider.Import(input);
    RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);

    var documentBookmarks = document.EnumerateChildrenOfType<BookmarkRangeStart>().Select(b => b.Bookmark).ToList();

    // Obtain bookmark by name
    var specificBookmarkByName = documentBookmarks.FirstOrDefault(b => b.Name == "bookmark1");
    // Obtain the first Run element inside the bookmark to copy its formatting
    Run oldBookmarkText = (Run)specificBookmarkByName.BookmarkRangeStart.Paragraph.Inlines.FirstOrDefault(i => i is Run);

    // Keep the bookmark and just change the content
    editor.DeleteContent(specificBookmarkByName.BookmarkRangeStart, specificBookmarkByName.BookmarkRangeEnd, false);
    editor.MoveToInlineEnd(specificBookmarkByName.BookmarkRangeStart);

    // Apply the old formatting to the editor Method 1

    editor.CharacterFormatting.CopyPropertiesFrom(oldBookmarkText.Properties);

    // Apply the old formatting to the editor Method 2

    editor.CharacterFormatting.FontSize.LocalValue = oldBookmarkText.FontSize;
    editor.CharacterFormatting.FontFamily.LocalValue = oldBookmarkText.FontFamily;
    editor.CharacterFormatting.FontStyle.LocalValue = oldBookmarkText.FontStyle;
    editor.CharacterFormatting.FontWeight.LocalValue = oldBookmarkText.FontWeight;
    editor.CharacterFormatting.Strikethrough.LocalValue = oldBookmarkText.Strikethrough;
    editor.CharacterFormatting.FlowDirection.LocalValue = oldBookmarkText.FlowDirection;
    editor.CharacterFormatting.BaselineAlignment.LocalValue = oldBookmarkText.BaselineAlignment;
    editor.CharacterFormatting.ForegroundColor.LocalValue = oldBookmarkText.ForegroundColor;
    editor.CharacterFormatting.HighlightColor.LocalValue = oldBookmarkText.HighlightColor;
    editor.CharacterFormatting.UnderlineColor.LocalValue = oldBookmarkText.Underline.Color;
    editor.CharacterFormatting.UnderlinePattern.LocalValue = oldBookmarkText.Underline.Pattern;

    editor.InsertText("NEW CONTENT");

    using (Stream output = File.OpenWrite("output.docx"))
    {
        docxFormatProvider.Export(document, output);
    }
}

WordsProcessing Change Bookmark Content

See Also

In this article