Working with page navigation in UWP PDF Viewer (SfPdfViewer)

9 Jul 20212 minutes to read

The PdfViewer allows navigation through the pages of PDF documents. The navigation from one page to other can be done by using “GoToPage” method.

The following code snippet illustrates page navigation using the GotoPage method.

// Navigates to the page 5 in the PDF document.
pdfViewer.GotoPage(5);
Navigates to the page 5 in the PDF document.
pdfViewer.GotoPage(5)

There are also commands in PdfViewer, which could make the page navigation simpler:

Names Description
FirstPageCommand Navigates to the first page of the PDF document.
LastPageCommand Navigates to the last page of the PDF document.
PreviousPageCommand Navigates to the previous page of the PDF document.
NextPageCommand Navigates to the next page of the PDF document.
GoToPageCommand Navigates to the specified page of the PDF document.

FirstPageCommand

Navigation to the first page can be achieved by using the FirstPageCommand as shown below.

<Button Command="{Binding FirstPageCommand}" />

LastPageCommand

Navigation to the last page can be achieved by using the LastPageCommand as shown below.

<Button Command="{Binding LastPageCommand}" />

NextPageCommand

NextPageCommand navigates to the page next to the currently displayed page in the PDF Viewer.

<Button Command="{Binding NextPageCommand}" />

PreviousPageCommand

PreviousPageCommand navigates to the page previous to the currently displayed page in the PDF Viewer.

<Butston Command="{Binding PreviousPageCommand}" />

GoToPageCommand

This command navigates to the specified page.

<Button Command="{Binding GoToPageCommand}" CommandParameter="{Binding Path=Text, ElementName=PageNumberTextBox}" />

In the above snippet, the PageNumberTextBox refers to a text box instance that gets the page number input.

Tracking the change of pages in PDF document

The PageChanged event is triggered when there is a change in the page being displayed in the PDF Viewer.

The following code snippet updates the current page number in a text block, whenever the page is changed.

private void PdfViewer_PageChanged(object sender, PageChangedEventArgs e)
{
    pageNumberTextBlock.Text = e.NewPageNumber.ToString();
}
Private Sub PdfViewer_PageChanged(sender As Object, e As PageChangedEventArgs) Handles pdfViewer.PageChanged
    pageNumberTextBlock.Text = e.NewPageNumber.ToString()
End Sub