Highlighting Words and Making them Visible.

4 posts, 0 answers
  1. Aaron
    Aaron avatar
    38 posts
    Member since:
    Apr 2008

    Posted 10 Nov 2015 Link to this post

    Hi,

    We store a bunch of PDFs in SQL Server, and have implemented a full-text search using a PDF iFilter.  The user enters a keyword or phrase, then gets a list of matching PDFs in a ListBox.  

    When the user selects a PDF from the ListBox, we load the PDF document from the database and present it in a RadPdfViewer.

    However, I want to highlight the first instance of the word or phrase they searched for.  I've hooked into the PdfViewer's OnDocumentChanged event, and the handler looks like this:

            private void PdfViewerOnDocumentChanged(object sender, DocumentChangedEventArgs documentChangedEventArgs)
            {
                if (SearchText.IsNullOrEmpty())
                    return;
                
                DispatcherTimer timer = new DispatcherTimer();
                timer.Interval = TimeSpan.FromMilliseconds(200);
                timer.Tick += (o, args) =>
                {
                    var searchResult = this.PdfViewer.Find(SearchText);

                    if (!searchResult.Range.IsEmpty)
                    {
                        PdfViewer.Document.Selection.Clear();
                        PdfViewer.Select(searchResult.Range);
                        PdfViewer.Document.CaretPosition.MoveToPosition((searchResult.Range.EndPosition));                    
                    }
                    timer.Stop();
                };
                timer.Start();
            }

    This works well most of the time.  However, the area I have on screen for the PdfViewer is smaller than 1 page, and once in a while the word I'm trying to highlight is out of the visible area on the page.  

    Is there a method I can use to either always have the highlighted text be visible, or at least detect when the text is near the bottom of the page, and move everything up a bit when that is the case?

    Thanks.

    Aaron

  2. Deyan
    Admin
    Deyan avatar
    136 posts

    Posted 13 Nov 2015 Link to this post

    Hello Aaron,

    Thank you for contacting us.

    It seems that the issue you are facing is caused by using the DispatcherTimer instance. I have tested your code by removing this timer and the PdfViewer seems to be scrolling correctly to the selected text. Could you please try the following code to confirm that it is working as expected on your side as well:
    PdfViewer.DocumentChanged += (s, e) =>
        {
            SearchResult searchResult = PdfViewer.Find(SearchText);
     
            if (searchResult != SearchResult.NotFound)
            {
                PdfViewer.Document.CaretPosition.MoveToPosition(searchResult.Range.StartPosition);
                PdfViewer.Select(searchResult.Range);
            }
        };

    I hope this is helpful.

    Regards,
    Deyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. DevCraft banner
  4. Aaron
    Aaron avatar
    38 posts
    Member since:
    Apr 2008

    Posted 13 Nov 2015 in reply to Deyan Link to this post

    Thanks for the reply.

     I tried this code and it still didn't scroll.  It found and highlighted the text, but it was below the bottom of the screen (again, we can't fit an 8.5 x 11 inch page on one screen).

    We are using version 2014.2.729.1050 at the office.  I'm not sure if that's the issue.  

    Even so, I love getting rid of the dispatcher timer.  Thanks for that.

    Aaron

  5. Deyan
    Admin
    Deyan avatar
    136 posts

    Posted 18 Nov 2015 Link to this post

    Hello Aaron,

    I have managed to reproduce the issue with the version you have mentioned. However, with the version from the latest official release (Q3 2015 SP1), RadPdfViewer scrolls correctly to the search result in DocumentChanged event handler.

    Could you please try the latest version to confirm that everything is working as expected on your side as well?

    I hope this helps.

    Regards,
    Deyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top