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

Get page and line number

4 Answers 142 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 12 Sep 2011, 11:23 AM
Hi, 

Is it possible to obtain the page and line number of a bookmark or a selection of text? 

I have seen code examples for get line numbers when the user clicks on the document, but i need to obtain this information from the bookmarks or .Document.Selection.

Thanks in advance. 



4 Answers, 1 is accepted

Sort by
0
Marcel
Top achievements
Rank 1
answered on 15 Sep 2011, 08:50 AM
Has no one ever tried this?
0
Accepted
Iva Toteva
Telerik team
answered on 15 Sep 2011, 10:25 AM
Hi Marcellino,

You can get the page number of the caret position quite easily like this:

int pageIndex = documentPosition.GetCurrentSectionBox().PageNumber;

You can use the methods of Document Positions to navigate to different document elements. The CaretPosition of the document is also a document position and you can obtain a reference to it like this:
this.editor.Document.CaretPosition

Another approach would be to iterate through the parents of the BookmarkRangeStart until you reach the SectionLayoutBox like this:
LayoutBox startBox = bookmarkRangeStart.FirstLayoutBox;
LayoutBox parent = startBox.Parent;
while (!(parent is SectionLayoutBox))
{
    parent = parent.Parent;
}
return ((SectionLayoutBox)parent).PageNumber;

When it comes to selection, you should have in mind that multi-range selection is supported and even one selection range can span several pages. However, getting the page number of the first selected element in the first selection range can be done like this:
SelectionRange range = this.editor.Document.Selection.Ranges.First;
return range.StartPosition.GetCurrentSectionBox().PageNumber;

Regards,
Iva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Marcel
Top achievements
Rank 1
answered on 01 Nov 2011, 09:55 AM
Hi,

thanks for the response, that really helped me out. I have a couple of other questions:

1) Is there a way to obtain the character position when the user clicks within the document? 

2) Is it possible to obtain the character position from a bookmark range? So I know what line the bookmark starts and ends on after using the code above, but i need to also know where in that line the bookmark starts. 

3) Also when the user clicks in the document, how can i obtain the page number?

Thanks 

Chez. 
0
Accepted
Iva Toteva
Telerik team
answered on 04 Nov 2011, 11:30 AM
Hello Chez,

RadRichTextBox does not use integer indexes for the positions in the document. Instead, it uses document positions, which enable you to go to specific document elements. This is particularly convenient when you want to do some formatting, as most methods and commands are executed on selection or the elements at the current caret position, which is also a document position.
You can get the current caret position like this:

DocumentPosition caretPosition = this.editor.Document.CaretPosition;

When you click inside the document, the caret position will change to the place in the document you have clicked.

As for obtaining the position of a Bookmark, this can be done through document positions as well. Some information on the methods that RadRichTextBox and RadDocument expose with relation to bookmarks can be found here. Obtaining a document position that is at the beginning or end of a bookmark, can be done like this:
DocumentPosition start = new DocumentPosition(radRichTextBox.Document);
DocumentPosition end = new DocumentPosition(radRichTextBox.Document);
start.MoveToInline(bookmarkRangeStart.FirstLayoutBox as InlineLayoutBox, 0); //DocumentPosition pointing to the beginning of the bookmark
end.MoveToInline(bookmarkRangeStart.End.FirstLayoutBox as InlineLayoutBox, 0); //DocumentPosition pointing to the end of the bookmark

Getting the line number of a document position can be done as shown in this forum post. You can perform some similar logic for counting the positions between the start of the line to the start of the bookmark in order to achieve the behavior you are after.

Obtaining the page number when the user clicks in the document can be done like this:

int pageIndex = this.radRichTextBox.Document.CaretPosition.GetCurrentSectionBox().PageNumber;


Best wishes,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
RichTextBox
Asked by
Marcel
Top achievements
Rank 1
Answers by
Marcel
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or