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

BookMark Content

4 Answers 312 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
RV
Top achievements
Rank 1
RV asked on 27 Mar 2018, 01:31 AM

How to get the text of BookMarks into a string variable. Following is my code.

#region Get Book Mark Data
        public void GetTextFromBookMarks(RadFlowDocument document)
        {
            IEnumerable<BookmarkRangeStart> bookmarkStart = document.EnumerateChildrenOfType<BookmarkRangeStart>();
            IEnumerable<BookmarkRangeEnd> bookmarkEnd = document.EnumerateChildrenOfType<BookmarkRangeEnd>();

            foreach (var item in bookmarkStart)
            {

            }

        }
        #endregion

4 Answers, 1 is accepted

Sort by
0
RV
Top achievements
Rank 1
answered on 27 Mar 2018, 09:15 PM

I got only first Paragraph but not all text from BookMark

#region Get Book Mark Data
public void GetTextFromBookMarks(RadFlowDocument document)
{
IEnumerable <BookmarkRangeStart> bookmarkStart = document.EnumerateChildrenOfType<BookmarkRangeStart>();
IEnumerable<BookmarkRangeEnd> bookmarkEnd = document.EnumerateChildrenOfType<BookmarkRangeEnd>();
foreach (var item in bookmarkStart)
{
string bookMarkName = item.Bookmark.Name;
IEnumerable <InlineBase> inLine = item.Paragraph.EnumerateChildrenOfType<InlineBase>();
foreach (var item1 in inLine)
{
string txt = item1.ToString();
}
}
}
#endregion

0
Tanya
Telerik team
answered on 29 Mar 2018, 07:38 AM
Hello,

To achieve that requirement, you will need to find the bookmark start marker and traverse the inlines of the current paragraph until you find a bookmark end. Since the paragraphs can contain elements other than text, you should only check the runs and skip the others. Here is an example in code:

List<string> bookmarksContent = new List<string>();
 
foreach (var bookmarkStart in this.document.EnumerateChildrenOfType<BookmarkRangeStart>())
{
    InlineBase currentInline = bookmarkStart;
    string currentBookmarkText = string.Empty;
 
    while (!(currentInline is BookmarkRangeEnd))
    {
        Run run = currentInline as Run;
        if (run != null)
        {
            currentBookmarkText += run.Text;
        }
 
        Paragraph paragraph = currentInline.Paragraph;
        int nextInlineIndex = paragraph.Inlines.IndexOf(currentInline) + 1;
 
        if (nextInlineIndex >= paragraph.Inlines.Count)
        {
            break;
        }
 
        currentInline = paragraph.Inlines.ElementAt(nextInlineIndex);
    }
 
    bookmarksContent.Add(currentBookmarkText);
}

Please, note that the sample handles the cases where the bookmark range start and end markers are in the same paragraph. If you need to handle more complex cases, you could extend the code following a similar logic. 

Hope this is helpful.

Regards,
Tanya
Progress Telerik

0
RV
Top achievements
Rank 1
answered on 29 Mar 2018, 08:07 PM

The following is the solution. Let me know if I can simplify any code.

 

#region Get Book Mark Data
public List<string> GetTextFromBookMarks(RadFlowDocument document)
{
List<string> bookmarksContent = new List<string>();
string bookMarkName = "";
string currentBookmarkText = string.Empty;
// Get the sections from the document
var section = document.EnumerateChildrenOfType<Section>().ToList();
foreach (var eachSection in section)
{
//Get the blocks from the sections
var blockBase = eachSection.EnumerateChildrenOfType<BlockBase>().ToList();
foreach (var eachBlockBase in blockBase)
{
// Get INLINE text
IEnumerable<InlineBase> inLine = eachBlockBase.EnumerateChildrenOfType<InlineBase>();
foreach (var eachInLine in inLine)
{
//If Inline is start of BookMark, get the BOOKMark Name
if (eachInLine is BookmarkRangeStart)
{
Paragraph paragraph = eachInLine.Paragraph;
var bookmarkStart1 = paragraph.EnumerateChildrenOfType<BookmarkRangeStart>().ToList();
foreach (var iBookMark in bookmarkStart1)
{
bookMarkName = iBookMark.Bookmark.Name;
currentBookmarkText = bookMarkName + ":";
break;
}
}
else if (eachInLine is BookmarkRangeEnd)
{
bookMarkName = "";
bookmarksContent.Add(currentBookmarkText);
currentBookmarkText = string.Empty;
}
else
{
Run run = eachInLine as Run;
if (run != null)
{
if (bookMarkName != "")
{
currentBookmarkText += run.Text;
}
}
}
}
}
}
return bookmarksContent;
}
#endregion

0
Tanya
Telerik team
answered on 03 Apr 2018, 08:27 AM
Hi,

All of the elements in the document should be iterated to find out which ones are surrounded by bookmark markers, so the code you shared should do the trick and there is nothing unnecessary that should be removed or simplified.

Regards,
Tanya
Progress Telerik

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