This is a migrated thread and some comments may be shown as answers.

Deleting bookmarks and the text they link to

1 Answer 190 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
ZergReporting
Top achievements
Rank 1
ZergReporting asked on 26 Mar 2018, 09:52 AM

 

In my docx document I have a bookmark named: "SomeBookMark". "SomeBookMark" was created in msword while "SomeText" was selected.

In the telerik flowdocuments this would mean that BookmarkRangeStart would be placed before "SomeText" and BookmarkRangeEnd would be placed at the end of "SomeText".

 

I'm able to delete the bookmark and I am able to insert text before or after "SomeText". But effectively I would like to remove "SomeText" and insert my own text. I can see the text in the "inlines" of the paragraph that contains the bookmarks and the text but the way the inlines are populated depends heavily on how the word document was edited. From the word document it would look like "SomeText" was 1 inline entry but it might actually be more than 1. 

 

Is there any generic way to use the bookmarks to select the range of text between the start and end and replace or delete it?

1 Answer, 1 is accepted

Sort by
0
Tanya
Telerik team
answered on 28 Mar 2018, 03:47 PM
Hello John,

You could achieve that by adding and removing elements to the parent's children collection. For replacing Runs, you can change the Inlines collection of the Paragraph element. Here is an example of how you could replace the bookmark and its content with a text:

RadFlowDocumentEditor editor = new RadFlowDocumentEditor(this.document);
 
foreach (var bookmarkEnd in editor.Document.EnumerateChildrenOfType<BookmarkRangeEnd>().ToList())
{
    Paragraph paragraph = bookmarkEnd.Paragraph;
    int bookmarkIndex = paragraph.Inlines.IndexOf(bookmarkEnd);
 
    for (int i = bookmarkIndex; i >= 0; i--)
    {
        InlineBase currentElement = paragraph.Inlines[i];
        paragraph.Inlines.RemoveAt(i);
 
        if (currentElement is BookmarkRangeStart)
        {
            Run run = new Run(this.document);
            run.Text = "New Content";
            paragraph.Inlines.Insert(i, run);
            break;
        }
    }
}

Please, note that this sample handles the case in which all the elements are in the same paragraph and you will need to add an additional logic if you would like to handle more complex cases. The implementation would be similar to the one shown above.

Hope this helps.

Regards,
Tanya
Progress Telerik

Tags
WordsProcessing
Asked by
ZergReporting
Top achievements
Rank 1
Answers by
Tanya
Telerik team
Share this question
or