# PDF Viewer Toolbar

The <a href="https://www.telerik.com/maui-ui/pdf-viewer" target="_blank">.NET MAUI PDF Viewer</a> Toolbar includes the commands that the PDF Viewer provides. You also have the option to include additional toolbar items in the `PdfViewerToolbar` through a custom command.

## Predefined Toolbar Items

By default, the PDF Viewer Toolbar provides the following toolbar items described in the table below:

| Toolbar Item                                 | Description                                                                                                                        |
| -------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------- |
| `PdfViewerZoomInToolbarItem`                 | Executes the `PdfViewerZoomInCommand`                                                                                              |
| `PdfViewerZoomOutToolbarItem`                | Executes the `PdfViewerZoomOutCommand`                                                                                             |
| `PdfViewerNavigateToNextPageToolbarItem`     | Executes the `PdfViewerNavigateToPreviousPageCommand`                                                                              |
| `PdfViewerNavigateToPreviousPageToolbarItem` | Executes the `PdfViewerNavigateToNextPageCommand`                                                                                  |
| `PdfViewerFitToWidthToolbarItem`             | Executes the `PdfViewerFitDocumentToWidthCommand`                                                                                  |
| `PdfViewerNavigateToPageToolbarItem`         | Executes the `PdfViewerNavigateToPreviousPageCommand`                                                                              |
| `PdfViewerToggleLayoutModeToolbarItem`       | toggles the `RadPdfViewer.LayoutMode` property between `SinglePage` and `ContinuousScroll`                                         |
| `PdfViewerSearchToolbarItem`                 | Activates the search functionality in the PDF viewer control. Opens a popup.                                                       |
| `PdfViewerSearchNavigationToolbarItem`       | Activates the search functionality in the PDF viewer control. Creates a default set of toolbar items applicable for search feature |

> note `PdfToolbarItems` inherit from `ButtonToolbarItem` except `PdfViewerNavigateToPageToolbarItem`.

All properties applicable for `ToolbarItem` are available for the PDF Viewer `PdfViewerNavigateToPageToolbarItem`.
All properties applicable for `ButtonToolbarItem` are available for all PDF Viewer toolbar items except `PdfViewerNavigateToPageToolbarItem`.

The following example demonstrates how to use the Toolbar and its predefined items:

**1.** Use the following snippet to define the `RadPdfViewer` and `RadPdfToolbar`:

```XAML
<Grid RowDefinitions="{OnIdiom Desktop='Auto, *', Phone='*, Auto'}">
    <telerik:RadPdfViewerToolbar Grid.Row="{OnIdiom Desktop=0, Phone=1}" 
                                 OverflowMode="Scroll"
                                 PdfViewer="{Binding Source={x:Reference pdfViewer}}">
        <telerik:PdfViewerFitToWidthToolbarItem />
        <telerik:PdfViewerZoomInToolbarItem />
        <telerik:PdfViewerZoomOutToolbarItem />
        <telerik:PdfViewerSearchToolbarItem IsVisible="{OnIdiom Phone=False, Desktop=True}"/>
        <telerik:PdfViewerSearchNavigationToolbarItem IsVisible="{OnIdiom Phone=True, Desktop=False}"/>
        <telerik:PdfViewerToggleLayoutModeToolbarItem />
        <telerik:PdfViewerNavigateToPreviousPageToolbarItem />
        <telerik:PdfViewerNavigateToNextPageToolbarItem />
        <telerik:PdfViewerNavigateToPageToolbarItem />
    </telerik:RadPdfViewerToolbar>
    <telerik:RadPdfViewer x:Name="pdfViewer"
                          Grid.Row="{OnIdiom Desktop=1, Phone=0}" />
</Grid>
```

**2.** Add the following namespace:

```XAML
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
```

**3.** Then add the following code to visualize the PDF document:

```C#
Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
{
    Assembly assembly = typeof(Toolbar).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-overview.pdf"));
    Stream stream = assembly.GetManifestResourceStream(fileName);
    return stream;
});
this.pdfViewer.Source = streamFunc;
```

> The snippet above shows one of the approaches for loading a PDF document inside `RadPdfViewer` just for the purpose of the example.

The following image shows the results from the completed example on WinUI:

![.NET MAUI PdfViewer Toolbar](https://www.telerik.com/maui-ui/documentation/assets/84b404b8f73e298356a8625516f5a431/pdf-toolbar.png "PDF Viewer Toolbar")

And this is the look on Android:

![.NET MAUI PdfViewer Toolbar](https://www.telerik.com/maui-ui/documentation/assets/c2ed293743c246b4dd25d5eac1417414/pdf-toolbar-mobile.png "PDF Viewer Toolbar")

> For a runnable example with the PDF Viewer toolbar, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **PdfViewer > Toolbar**.

## Custom Toolbar Items

You can add custom toolbar items to the PDF Toolbar and bind them to a custom command. You can achieve this by using the `ButtonToolbarItem`.

Here is an example showing how to add a custom Toolbar item with a sample command bound to it. The command is used just to display a message with the PDF document's file size.

**1.** Add the PdfViewer and the PdfToolbar controls to your page:

```XAML
<Grid RowDefinitions="{OnIdiom Desktop='Auto, *', Phone='*, Auto'}">
    <telerik:RadPdfViewerToolbar Grid.Row="{OnIdiom Desktop=0, Phone=1}"
                                 PdfViewer="{Binding Source={x:Reference pdfViewer}}">
        <telerik:ButtonToolbarItem Command="{Binding DisplayFileSizeCommand}">
            <telerik:ButtonToolbarItem.ImageSource>
                <FontImageSource Glyph="{x:Static telerik:TelerikFont.IconFile}"
                                 FontFamily="{x:Static telerik:TelerikFont.Name}"
                                 Size="16"/>
            </telerik:ButtonToolbarItem.ImageSource>
        </telerik:ButtonToolbarItem>
        <telerik:PdfViewerZoomInToolbarItem />
        <telerik:PdfViewerZoomOutToolbarItem />
    </telerik:RadPdfViewerToolbar>
    <telerik:RadPdfViewer x:Name="pdfViewer"
                          Grid.Row="{OnIdiom Desktop=1, Phone=0}"
                          Document="{Binding Document, Mode=OneWayToSource}" />
</Grid>
```

**2.** Load a sample PDF document in code-behind:

```C#
this.BindingContext = new ViewModel();

Func<CancellationToken, Task<Stream>> streamFunc = ct => Task.Run(() =>
{
    Assembly assembly = typeof(CustomToolbar).Assembly;
    string fileName = assembly.GetManifestResourceNames().FirstOrDefault(n => n.Contains("pdf-processing.pdf"));
    Stream stream = assembly.GetManifestResourceStream(fileName);
    return stream;
});
this.pdfViewer.Source = streamFunc;
```

**3.** Add the ViewModel class. In the ViewModel, get a reference to the `RadFixedDocument` instance through the `Document` property of the PDF Viewer and execute the `DisplayFileSizeCommand` bound to the `Command` property of the custom Toolbar item:

```C#
public class ViewModel
{
    public ViewModel()
    {
        this.DisplayFileSizeCommand = new Command(this.DisplayFileSizeCommandExecute);
    }

    public RadFixedDocument Document { get; set; }
    public ICommand DisplayFileSizeCommand { get; set; }

    protected void DisplayFileSizeCommandExecute(object para)
    {
        RadFixedDocument document = this.Document;
        if (document != null)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                PdfFormatProvider formatProvider = new PdfFormatProvider();
                formatProvider.Export(document, stream, TimeSpan.FromMinutes(1));
                double megabytes = ToKiloBytes(stream.Length);
                Application.Current.Windows[0].Page.DisplayAlert("", "File Size: " + megabytes.ToString("N0") + " KB", "OK");
            }
        }
    }

    private static double ToKiloBytes(long bytesCount)
    {
        return (double)bytesCount / 1024;
    }
}
```

The image below shows the results on the different platforms:

![.NET MAUI PdfToolbar Custom ToolbarItem](https://www.telerik.com/maui-ui/documentation/assets/6970cb3aa94187ded1986faac09d3336/pdf-custom-toolbar.png)

> For a runnable example demonstrating the PDF Viewer's custom Toolbar items, see the [SDKBrowser Demo Application](https://www.telerik.com/maui-ui/documentation/demos-and-sample-apps/sdkbrowser-app) and go to **PdfViewer > Toolbar**.

## See Also

* [Review PDF Viewer Commands](https://www.telerik.com/maui-ui/documentation/controls/pdfviewer/commands)
