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

Replace existing text with Inline element

Product Version Product Author
2020.1.310 RadWordsProcessing Martin Velikov

Description

Introducing a way to replace text with other document elements.

Solution

To achieve this we will iterate the document elements of type Run and will compare their text with the desired string. If there is a match we will store the Run index and we will insert the desired element (in our example: Break on this specific index in the Inlines collection. Finally we will remove the Run.

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document); 
editor.InsertText("First line"); 
editor.InsertText("NewLine"); 
editor.InsertText("Second line"); 
 
foreach (Run run in document.EnumerateChildrenOfType<Run>().ToList()) 
{ 
    if (run.Text == "NewLine") 
    { 
        Paragraph paragraph = run.Paragraph; 
        int childIndex = paragraph.Inlines.IndexOf(run); 
 
        Break br = new Break(document); 
        br.BreakType = BreakType.LineBreak; 
        paragraph.Inlines.Insert(childIndex, br); 
        paragraph.Inlines.Remove(run); 
    } 
} 
In this article