New to Telerik UI for WinUI? Download free 30-day trial

Text Position

The TextPosition class allows you to keep track of the current text caret position in the document.

TextPosition is part of the RadPdfProcessing library which is used for the document model of the PdfViewer.

The text position is useful for the UI only when you activate the TextSelection viewer mode. To do so, set the Mode property of the PdfViewer. For more information, refer to the article on the PdfViewer interaction modes.

Select the TextSelection mode

this.pdfViewer.Mode = FixedDocumentViewerMode.TextSelection; 
When the text selection mode is active, the caret position can be changed by clicking on the text in the PDF document.

Manual Text Positioning

To change the caret position manually, use the CaretPosition property of the RadFixedDocument class that can be accessed through the Document property of the PdfViewer. The caret position is represented by the TextPosition class which provides the following methods for updating the position:

  • MoveToNextPosition
  • MoveToPreviousPosition
  • MoveToPosition
  • MoveToNextWord
  • MoveToPreviousWord
  • MoveToCurrentWordStart
  • MoveToCurrentWordEnd
  • MoveToLineStart
  • MoveToLineEnd
  • MoveLineUp
  • MoveLineDown
  • MoveToStartOfDocument
  • MoveToEndOfDocument

Use the Move method

this.pdfViewer.Document.CaretPosition.MoveToEndOfDocument(); 
Calling one of the Move methods will also scroll the document to bring the corresponding line into the viewport.

To select a position based on an position index in the document, initialize a new TextPosition object with the constructor overload where you provide a RadFixedPage and an offset.

Move to a specific text position

RadFixedDocument document = this.pdfViewer.Document; 
RadFixedPage firstPage = document.Pages[0]; 
 
// This is the 100th character in the first page of the document. 
TextPosition position = new TextPosition(firstPage, 100);             
this.pdfViewer.Document.CaretPosition.MoveToPosition(position); 

Events

TextPosition provides the PositionChanging and PositionChanged events that can be used to listen for changes in the position.

Subscribe to position-changing events

void SubscribeToEvents() 
{ 
    this.pdfViewer.Document.CaretPosition.PositionChanged += Position_PositionChanged; 
    this.pdfViewer.Document.CaretPosition.PositionChanging += Position_PositionChanging; 
} 
 
private void Position_PositionChanging(object sender, EventArgs e) 
{             
} 
 
private void Position_PositionChanged(object sender, EventArgs e) 
{ 
} 

See Also

In this article
Not finding the help you need?