Page Navigation in .NET MAUI PDF Viewer (SfPdfViewer)
20 Mar 20244 minutes to read
After loading a document, you can navigate or scroll to different pages using the page navigation options, either programmatically or by dragging the scroll head in the UI. This section walks through the page navigation functionalities supported in the SfPdfViewer.
Page layout modes
The page layout modes of SfPdfViewer allow users to change the layout of the pages while viewing PDF documents. These modes provide flexibility in viewing and navigating PDF pages, responding to various reading styles and document structures. The SfPdfViewer supports the following Page layout modes:
- Continuous
- Single page
Continuous layout mode
The SfPdfViewer’s default page layout mode is Continuous
. In this mode, the SfPdfViewer displays pages in a continuous vertical scroll, is useful for documents containing a continuous flow of content. It enables users to scroll through the document with ease, providing a more fluid reading experience.
To enable the continuous page layout mode, you can call the following code in a button click.
// Enable or activate the Continuous Page Layout mode.
void EnableContinuousPageLayoutMode()
{
// Set the PageLayout mode to Continuous using the `SfPdfViewer` instance.
PdfViewer.PageLayoutMode = PageLayoutMode.Continuous;
}
Singe page layout mode
In single page layout mode, the SfPdfViewer shows one page at a time and allows users to swipe or scroll horizontally through the document. It provides a focused view, allowing readers to concentrate on a single page at a time without being distracted.
To enable the single page layout mode, you can call the following code in a button click.
// Enable or activate the Single Page Layout mode.
void EnableSinglePageLayoutMode()
{
// Set the PageLayout mode to Single using the `SfPdfViewer` instance.
PdfViewer.PageLayoutMode = PageLayoutMode.Single;
}
NOTE
You can download the Custom Toolbar sample project for the .NET MAUI PDF Viewer here , which demonstrates fuctionality related to the PageLayoutMode feature.
Obtain total page count and current page number
The PageCount property returns the total number of pages in a PDF document. And the PageNumber property returns the current page number displayed in the PDF Viewer.
// Returns total number of pages
int totalPageCount = PdfViewer.PageCount;
// Returns current page number
int currentPageNumber = PdfViewer.PageNumber;
Navigate to the desired page programmatically
The GoToPage method helps you navigate to the specified page number in a PDF document. If the destination page doesn’t exist, then the navigation will not happen, and the older page will be retained. Refer to the following code example:
// Navigate to the page number 4.
PdfViewer.GoToPage(4);
Navigate to the next and previous page programmatically
The GoToNextPage and GoToPreviousPage methods help you navigate to the next and previous pages of a PDF document. Refer to the following code example:
private void NextPageButton_Clicked(object sender, EventArgs e)
{
// Navigate to the next page
PdfViewer.GoToNextPage();
}
private void PreviousPageButton_Clicked(object sender, EventArgs e)
{
// Navigate to the previous page
PdfViewer. GoToPreviousPage();
}
Navigate to the first and last page programmatically
The GoToFirstPage and GoToLastPage methods help you navigate to the first and last pages of a PDF document. Refer to the following code example:
private void FirstPageButton_Clicked(object sender, EventArgs e)
{
// Navigate to the first page
PdfViewer.GoToFirstPage();
}
private void LastPageButton_Clicked(object sender, EventArgs e)
{
// Navigate to the last page
PdfViewer.GoToLastPage();
}
The example project with the page navigation functionalities can be downloaded here.