Pdf Viewer Scrolling to a Certain location

1 Answer 142 Views
PDFViewer
Nathan
Top achievements
Rank 1
Nathan asked on 01 Nov 2023, 06:43 PM

In the WPF version of the Pdf Viewer, there was VerticalScrollOffset and ScrollToVerticalOffset(). I'm not seeing those in the .Net MAUI version of the pdf viewer.  I've been trying to use the ScrollIntoView, but I'm finding it difficult to figure out the correct location.  

I'm wanting to capture the current scroll location, and then once I reload the pdf, scroll back to that location. 

 

1 Answer, 1 is accepted

Sort by
0
Didi
Telerik team
answered on 06 Nov 2023, 07:39 AM | edited on 09 Nov 2023, 02:11 PM

Hello Nathan,

You can use the ScrollIntoView method and Viewport property of the PDF Viewer. Here is the approach:

private static Rect GetViewport(RadPdfViewer pdfViewer)
{
    var presenterPI = pdfViewer.GetType().GetProperty("Presenter", BindingFlags.NonPublic | BindingFlags.Instance);
    object presenter = presenterPI.GetValue(pdfViewer);
    var viewportPI = presenter.GetType().GetProperty("Viewport", BindingFlags.NonPublic | BindingFlags.Instance);
    object radRect = viewportPI.GetValue(presenter);
    var xFI = radRect.GetType().GetField("X");
    var yFI = radRect.GetType().GetField("Y");
    var rightPI = radRect.GetType().GetProperty("Right", BindingFlags.NonPublic | BindingFlags.Instance);
    var bottomPI = radRect.GetType().GetProperty("Bottom", BindingFlags.NonPublic | BindingFlags.Instance);

    double x = (double)xFI.GetValue(radRect);
    double y = (double)yFI.GetValue(radRect);
    double width = (double)rightPI.GetValue(radRect) - x;
    double height = (double)bottomPI.GetValue(radRect) - y;

    return new Rect(x, y, width, height);
}

Rect viewport = GetViewport(this.pdfViewer);
Rect rect = new Rect(viewport.X, viewport.Y + 200, viewport.Width, viewport.Height);
this.pdfViewer.ScrollIntoView(rect);

Regards,
Didi
Progress Telerik

A brand new .NET MAUI course was just added to the Virtual Classroom. The training course is developed to help you get started with the Telerik UI for .NET MAUI components and features. It aims to put you in the shoes of an engineer who adds new features to an existing application. You can check it out at https://learn.telerik.com
Nathan
Top achievements
Rank 1
commented on 06 Nov 2023, 03:29 PM | edited

I'm getting an error when I try to access the Presenter on the pdfViewer.  When in debug mode, I can see that it exists, but it won't compile when I try to use it.

 

Didi
Telerik team
commented on 09 Nov 2023, 02:14 PM

I have updated the code for the GetViewport method. You have to get the Presenter property through reflection. 

In general, the Viewport functionality will be available from the next release expected in the middle of November 2023, so you won't need to use the workaround suggested here.

Nathan
Top achievements
Rank 1
commented on 09 Nov 2023, 02:59 PM

Ok, getting the presenter through reflection worked.  I'll look forward to the next release as well.

thanks

Tags
PDFViewer
Asked by
Nathan
Top achievements
Rank 1
Answers by
Didi
Telerik team
Share this question
or