- Navigate to the desired offset programmatically
- Scroll changes detection
- Detecting the end of the document
Contact Support
Scrolling in .NET MAUI PDF Viewer (SfPdfViewer)
The NET MAUI PDF Viewer has scrolling capabilities that allow users to navigate through content seamlessly. This section will walk you through various aspects of scrolling, including programmatically setting scroll positions and detecting scroll changes.
WARNING
Please note that since the PDF Viewer has built-in scrolling capability, it is advised to avoid placing the PDF Viewer inside other controls that also offer scrolling, such as ScrollView. Nesting within such controls may cause unexpected issues.
Navigate to the desired offset programmatically
The ScrollToOffset method moves the scroll position of the SfPdfViewer to the specified horizontal and vertical offsets. If the specified offset value is wrong, the scroll will not happen, and the older position will be retained. The offset values are represented in device-independent units.
// Navigate to the scroll offset position (100,1000).
PdfViewer.ScrollToOffset(100, 1000);
Scroll changes detection
The PropertyChanged
event can be used to identify the scroll changes in the PDF Viewer effectively. By monitoring the HorizontalOffset and VerticalOffset property changes, you can respond to both the horizontal and vertical scroll movements respectively.
The following is the code example for subscribing to the PropertyChanged
event.
<syncfusion:SfPdfViewer
x:Name = "PdfViewer" PropertyChanged = "PdfViewer_PropertyChanged"/>
void SubscribeToPropertyChangedEvent()
{
SfPdfViewer PdfViewer = new SfPdfViewer();
// Subscribe to property changed event.
PdfViewer.PropertyChanged += PdfViewer_PropertyChanged;
}
Handle scroll changes by implementing the event handler method. The following code example illustrates how to respond to both horizontal and vertical scroll changes:
private void PdfViewer_PropertyChanged(object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SfPdfViewer.HorizontalOffset))
{
// Horizontal scroll change detected
// Your code here
}
else if (e.PropertyName == nameof(SfPdfViewer.VerticalOffset))
{
// Vertical scroll change detected
// Your code here
}
}
Detecting the end of the document
You can determine if the control has reached the vertical end of the document by evaluating the following properties:
-
VerticalOffset
: It provides the current vertically scrolled offset. -
ClientRectangle
: It provides the viewport area bounds. -
ExtentHeight
: It provides the overall vertical height of the PDF document.
Below is the code example to detect whether the control has reached the vertical end of the document.
private void PdfViewer_PropertyChanged(object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SfPdfViewer.VerticalOffset))
{
// Determine if the control has reached the vertical end of the document.
bool isVerticalEndReached = (PdfViewer.VerticalOffset +
PdfViewer.ClientRectangle.Height >= PdfViewer.ExtentHeight);
}
}
Similarly, for horizontal end detection, refer to the following code example.
private void PdfViewer_PropertyChanged(object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SfPdfViewer.HorizontalOffset))
{
// Determine if the control has reached the horizontal end of the document.
bool isHorizontalEndReached = (PdfViewer.HorizontalOffset +
PdfViewer.ClientRectangle.Width >= PdfViewer.ExtentWidth);
}
}