# pdf-viewer-custom-toolbar

> PDF viewer uses custom controls for document search, text markup annotations, and undo/redo.

**Framework:** maui  **Component:** PdfViewer  **Variant:** custom-toolbar

## Get this item

**If you are an agent, fetch the JSON.** Source is inlined, so one request is enough and no tooling is required:

```
GET https://ai.syncfusion.com/r/maui/pdf-viewer-custom-toolbar.json
```

**Install Package(s)**

```bash
dotnet add package Syncfusion.Maui.PdfViewer
```

**Notes**

- Syncfusion release: 2026 Volume 3 (v35.1.37)
- The Syncfusion package is licensed. The composition in this file is source you own and edit. See https://ai.syncfusion.com/licensing.md

## Source files

### src/components/pdf-viewer-custom-toolbar/PdfViewer.xaml

```xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:pdf="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer"
             xmlns:model="clr-namespace:MAUI.Controls.PdfViewer.CustomToolbar"
             x:Class="MAUI.Controls.PdfViewer.CustomToolbar.PdfViewerView"
             x:DataType="model:PdfViewerViewModel"
             Title="PDF Viewer - Custom Toolbar">
    <ContentPage.Resources>
        <Style TargetType="Button">
            <Setter Property="BorderWidth"
                    Value="0"/>
            <Setter Property="FontSize"
                    Value="18"/>
            <Setter Property="Padding"
                    Value="4"/>
            <Setter Property="VerticalOptions"
                    Value="Center"/>
            <Setter Property="FontFamily"
                    Value="{OnPlatform iOS=PdfViewerIcon, MacCatalyst=PdfViewerIcon, Android=PdfViewerIcon.ttf#, WinUI=PdfViewerIcon.ttf#PdfViewerIcon}"/>
            <Setter Property="VisualStateManager.VisualStateGroups">
                <VisualStateGroupList>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal">
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="#99000000"/>
                                <Setter Property="BackgroundColor"
                                        Value="Transparent"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Disabled">
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="#44000000"/>
                                <Setter Property="BackgroundColor"
                                        Value="Transparent"/>
                            </VisualState.Setters>
                        </VisualState>
                        <VisualState x:Name="Pressed">
                            <VisualState.Setters>
                                <Setter Property="TextColor"
                                        Value="#99000000"/>
                                <Setter Property="BackgroundColor"
                                        Value="#0F000000"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateGroupList>
            </Setter>
        </Style>
    </ContentPage.Resources>
    <ContentPage.Content>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0"
                    HeightRequest="40"
                    BackgroundColor="#FFF6F6F6">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="{OnPlatform Default=Auto, Android=1.5*, iOS=1.5*}"/>
                    <ColumnDefinition Width="{OnPlatform Default=*, Android=3*, iOS=3*}"/>
                    <ColumnDefinition Width="{OnPlatform Default=*, Android=1*, iOS=1*}"/>
                    <ColumnDefinition Width="{OnPlatform Default=60, Android=1*, iOS=1*}"/>
                </Grid.ColumnDefinitions>
                <HorizontalStackLayout Spacing="10"
                        Grid.Column="0"
                        Margin="8,0,0,0"
                        HorizontalOptions="Start">
                    <Button Text="&#xe700;"
                            FontFamily="MauiMaterialAssets"
                            PropertyChanged="Button_PropertyChanged"
                            Command="{Binding OpenFileCommand}"
                            IsVisible="{OnPlatform MacCatalyst=false}"/>
                    <Button Text="&#xe77f;"
                            FontSize="20"
                            FontFamily="MauiMaterialAssets"
                            Command="{Binding Source={x:Reference PdfViewer},Path=PrintDocumentCommand}"/>
                </HorizontalStackLayout>
                <HorizontalStackLayout HorizontalOptions="{OnPlatform Default=End, Android=CenterAndExpand, iOS=CenterAndExpand}"
                                       Spacing="8"
                        Grid.Column="1">
                    <Border BackgroundColor="#FFFFFFFF"
                            Stroke="#66000000"
                            StrokeShape="{OnPlatform Default=RoundRectangle 4, MacCatalyst=RoundRectangle 5, iOS=RoundRectangle 5}"
                            HeightRequest="{OnPlatform Android=30,WinUI=34, MacCatalyst=26, iOS=26}"
                            Padding="{OnPlatform Android=5}"
                            StrokeThickness="1">
                        <Entry
                            x:Name="PageNumberEntry"
                            Text="{Binding PageNumber}"
                            MaxLength="4"
                            Keyboard="Numeric"
                            VerticalTextAlignment="Center"
                            WidthRequest="40"
                            HeightRequest="{OnPlatform Default=24, Android=40}"
                            TextChanged="PageNumberEntry_TextChanged"
                            Unfocused="PageNumberEntry_Unfocused"
                            Completed="PageNumberEntry_Completed"
                            VerticalOptions="Center"
                            HorizontalTextAlignment="Center"/>
                    </Border>
                    <Label
                        Text=" / "
                        VerticalTextAlignment="Center"
                        HorizontalTextAlignment="Center"
                        VerticalOptions="Center"/>
                    <Label
                        Text="{Binding PageCount}"
                        VerticalTextAlignment="Center"
                        HorizontalTextAlignment="Center"
                        VerticalOptions="Center"
                        Margin="0,0,4,0"/>
                    <Button
                        Text="&#xe702;"
                            FontSize="16"
                        FontFamily="MauiMaterialAssets"
                        PropertyChanged="Button_PropertyChanged"
                        Margin="4,0,0,0"
                            IsEnabled="{Binding CanGoToPreviousPage}"
                        Command="{Binding Source={x:Reference PdfViewer},Path=GoToPreviousPageCommand}"/>
                    <Button
                        Text="&#xe701;"
                            FontSize="16"
                        FontFamily="MauiMaterialAssets"
                        PropertyChanged="Button_PropertyChanged"
                        Margin="4,0,0,0"
                            IsEnabled="{Binding CanGoToNextPage}"
                        Command="{Binding Source={x:Reference PdfViewer},Path=GoToNextPageCommand}"/>
                    <Rectangle Fill="#FF979797"
                            HeightRequest="24"
                            WidthRequest="2"/>
                </HorizontalStackLayout>
                <HorizontalStackLayout
                    Grid.Column="2"
                        Margin="8,0,12,0"
                    HorizontalOptions="{OnPlatform MacCatalyst=Start, iOS=End, Android=End, WinUI=Start}">
                    <Button
                        Text="&#xe704;"
                            Margin="4,0,0,0"
                        FontFamily="MauiMaterialAssets"
                        Command="{Binding ZoomOutCommand}"
                            IsEnabled="{Binding CanZoomOut}"
                        PropertyChanged="Button_PropertyChanged"/>
                    <Button
                        Text="&#xe703;"
                            Margin="4,0,0,0"
                        FontFamily="MauiMaterialAssets"
                        Command="{Binding ZoomInCommand}"
                            IsEnabled="{Binding CanZoomIn}"
                        PropertyChanged="Button_PropertyChanged"/>
                </HorizontalStackLayout>
                <Grid Grid.Column="3"
                        Margin="0,0,12,0"
                        HorizontalOptions="End">
                    <Button Text="&#xe795;"
                            Margin="0,0,0,0"
                            HorizontalOptions="End"
                            FontFamily="MauiMaterialAssets"
                            PropertyChanged="Button_PropertyChanged"
                            Clicked="pageLayoutClicked"/>
                </Grid>
            </Grid>
            <Line Grid.Row="1"
                    StrokeThickness="1"
                    BackgroundColor="#33000000"/>
            <syncfusion:SfPdfViewer
                x:Name="PdfViewer"
                Grid.Row="2"
                ShowToolbars="False"
                PageCount="{Binding PageCount, Mode=OneWayToSource}"
                PageNumber="{Binding PageNumber, Mode=OneWayToSource}"
                MinZoomFactor="{Binding MinZoom, Mode=OneWayToSource}"
                MaxZoomFactor="{Binding MaxZoom, Mode=OneWayToSource}"
                DocumentSource="{Binding PdfDocumentStream}"
                ZoomFactor="{Binding CurrentZoom,Mode=TwoWay}"
                PasswordRequested="PdfViewer_PasswordRequested"
                DocumentLoadFailed="PdfViewer_DocumentLoadFailed">
            </syncfusion:SfPdfViewer>
            <Grid x:Name="PasswordDialog"
                  Grid.Row="2"
                  WidthRequest="360"
                  HeightRequest="280"
                  Padding="16"
                  Background="White"
                  HorizontalOptions="Center"
                  VerticalOptions="Center"
                  IsVisible="False">
                <Grid.RowDefinitions>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="2*"/>
                    <RowDefinition Height="2*"/>
                    <RowDefinition Height="1*"/>
                    <RowDefinition Height="1*"/>
                </Grid.RowDefinitions>
                <Label Grid.Row="0"
                       Text="Enter a Password"
                       FontAttributes="Bold"
                       FontSize="20"
                       VerticalOptions="Start"/>
                <Label Grid.Row="1"
                       Text="The file is protected. Please enter a password to open the PDF file."
                       VerticalOptions="Center"
                       VerticalTextAlignment="End"
                       LineBreakMode="WordWrap"/>
                <VerticalStackLayout Grid.Row="2"
                        Spacing="2">
                    <Border Stroke="{OnPlatform Android=Gray}"
                            Padding="{OnPlatform Android=4}">
                        <Entry x:Name="PasswordEntry"
                               Placeholder="Password"
                               IsPassword="True"
                               TextChanged="PasswordEntry_TextChanged"/>
                    </Border>
                    <Label x:Name="PasswordHelperText"
                           VerticalOptions="Center"
                           VerticalTextAlignment="Center"
                           LineBreakMode="WordWrap"
                           FontSize="12"
                           Padding="0,2,0,2"/>
                </VerticalStackLayout>
                <HorizontalStackLayout Grid.Row="3"
                                       Spacing="8"
                                       VerticalOptions="Start">
                    <CheckBox x:Name="ShowPasswordCheckBox"
                              CheckedChanged="ShowPasswordCheckBox_CheckedChanged"/>
                    <Label Text="Show Password"
                           HorizontalTextAlignment="Center"
                           VerticalTextAlignment="Center"/>
                </HorizontalStackLayout>
                <Grid Grid.Row="4">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="20"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <Button Grid.Column="1"
                            Text="CANCEL"
                            FontSize="14"
                            FontFamily="Roboto"
                            Padding="16,8"
                            Background="#512BD4"
                            TextColor="White"
                            Clicked="PasswordDialog_CancelClicked"
                            VerticalOptions="End"/>
                    <Button Grid.Column="3"
                            Text="OK"
                            FontSize="14"
                            FontFamily="Roboto"
                            Padding="16,8"
                            Background="#512BD4"
                            TextColor="White"
                            Clicked="PasswordDialog_OkClicked"
                            VerticalOptions="End"/>
                </Grid>
            </Grid>
            <Grid x:Name="MessageDialog"
                  Grid.Row="2"
                  WidthRequest="320"
                  HeightRequest="200"
                  HorizontalOptions="Center"
                  VerticalOptions="Center"
                  IsVisible="False">
                <Border Background="White"
                        Stroke="#33000000"
                        StrokeShape="RoundRectangle 4"
                        StrokeThickness="1">
                    <Grid Padding="20">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="1*"/>
                            <RowDefinition Height="2*"/>
                            <RowDefinition Height="1*"/>
                        </Grid.RowDefinitions>
                        <Label x:Name="MessageTitle"
                               Grid.Row="0"
                               FontAttributes="Bold"
                               FontSize="20"
                               VerticalOptions="Start"
                               VerticalTextAlignment="Start"/>
                        <Label x:Name="MessageText"
                               Grid.Row="1"
                               TextColor="#DE000000"
                               VerticalTextAlignment="Center"
                               LineBreakMode="WordWrap"/>
                        <Button x:Name="MessageOkButton"
                                Grid.Row="2"
                                FontSize="14"
                                FontFamily="Roboto"
                                Padding="16,8"
                                Background="#512BD4"
                                TextColor="White"
                                Text="CLOSE"
                                Clicked="MessageBox_OKClicked"
                                HorizontalOptions="End"
                                VerticalOptions="End"/>
                    </Grid>
                </Border>
            </Grid>
            <Grid x:Name="PageLayout"
                  Grid.Row="2"
                  WidthRequest="{OnPlatform MacCatalyst=205, iOS=150, Android=150, WinUI=205}"
                  HeightRequest="84"
                  HorizontalOptions="End"
                  VerticalOptions="Start"
                  IsVisible="False">
                <Border Background="#F7F2FB"
                        StrokeShape="RoundRectangle 4"
                        Padding="0">
                    <Grid RowDefinitions="*,*">
                        <Grid x:Name="ContinuousLayoutOption"
                              Grid.Row="0"
                              AutomationId="ContinuousLayoutOption">
                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Tapped="ContinuousPageLayout_Tapped"/>
                            </Grid.GestureRecognizers>
                            <HorizontalStackLayout Spacing="8"
                                                   Padding="8,0"
                                                   VerticalOptions="Center">
                                <Label Text="&#xE796;"
                                       FontSize="18"
                                       FontFamily="MauiMaterialAssets"
                                       TextColor="#212121"
                                       VerticalOptions="Center"/>
                                <Label Text="Continuous page"
                                       FontSize="{OnPlatform MacCatalyst=16, iOS=14, Android=14, WinUI=16}"
                                       FontFamily="Roboto"
                                       TextColor="#212121"
                                       VerticalOptions="Center"/>
                            </HorizontalStackLayout>
                        </Grid>
                        <Grid x:Name="SingleLayoutOption"
                              Grid.Row="1"
                              AutomationId="SingleLayoutOption">
                            <Grid.GestureRecognizers>
                                <TapGestureRecognizer Tapped="SinglePageLayout_Tapped"/>
                            </Grid.GestureRecognizers>
                            <HorizontalStackLayout Spacing="8"
                                                   Padding="8,0"
                                                   VerticalOptions="Center">
                                <Label Text="&#xE797;"
                                       FontSize="18"
                                       FontFamily="MauiMaterialAssets"
                                       TextColor="#212121"
                                       VerticalOptions="Center"/>
                                <Label Text="Page by page"
                                       FontSize="{OnPlatform MacCatalyst=16, iOS=14, Android=14, WinUI=16}"
                                       FontFamily="Roboto"
                                       TextColor="#212121"
                                       VerticalOptions="Center"/>
                            </HorizontalStackLayout>
                        </Grid>
                    </Grid>
                </Border>
            </Grid>
        </Grid>
    </ContentPage.Content>
</ContentPage>

```

### src/components/pdf-viewer-custom-toolbar/PdfViewer.xaml.cs

```cs
using Microsoft.Maui.Platform;
using Syncfusion.Maui.PdfViewer;
namespace MAUI.Controls.PdfViewer.CustomToolbar
{
    public partial class PdfViewerView : ContentPage
    {
        private int targetPageNumber = 0;
        private bool pageNumberChanged = false;
        //When loading password protected files, it is used to wait the current thread's execution, until the user enters the password.
        private ManualResetEvent manualResetEvent = new ManualResetEvent(false);

        public PdfViewerView(PdfViewerViewModel viewModel)
        {
            InitializeComponent();
            BindingContext = viewModel;
        }

        /// <summary>
        /// Handles automatic page number selection in the entry when the entry is tapped.
        /// </summary>
        private void PageNumberEntry_HandlerChanged(object sender, EventArgs e)
        {
            if (PageNumberEntry.Handler != null)
            {
                var handler = PageNumberEntry.Handler as Microsoft.Maui.Handlers.EntryHandler;
#if ANDROID
            handler.PlatformView.SetSelectAllOnFocus(true);
#elif IOS || MACCATALYST
        handler.PlatformView.EditingDidBegin += (s, e) =>
        {
            handler.PlatformView.PerformSelector(new ObjCRuntime.Selector("selectAll"), null, 0.0f);
        };
#elif WINDOWS
        handler.PlatformView.GotFocus += (s, e) =>
        {
            handler.PlatformView.SelectAll();
        };
#endif
            }
        }

        /// <summary>
        /// Handles the page number validation when the page number is typed in the entry.
        /// </summary>
        void PageNumberEntry_TextChanged(System.Object sender, Microsoft.Maui.Controls.TextChangedEventArgs e)
        {
            if (PageNumberEntry != null && PageNumberEntry.IsFocused && !string.IsNullOrEmpty(PageNumberEntry.Text))
            {
                bool isPageNumber = int.TryParse(PageNumberEntry.Text, out targetPageNumber);
                if (isPageNumber)
                {
                    pageNumberChanged = true;
                    PageNumberEntry.Text = targetPageNumber.ToString();
                }
                else
                {
                    PageNumberEntry.Text = e.OldTextValue;
                }
            }
        }

        /// <summary>
        /// Handles when the page number entry focused property changes to unfocused.
        /// </summary>
        void PageNumberEntry_Unfocused(System.Object sender, Microsoft.Maui.Controls.FocusEventArgs e)
        {
            PageNumberEntry.Text = PdfViewer?.PageNumber.ToString();
        }

        /// <summary>
        /// Handles when the page number is entered in the entry.
        /// </summary>
        private void PageNumberEntry_Completed(object sender, EventArgs e)
        {
            // When the enter key is pressed, the soft keyboard hides.
#if ANDROID
        if (Platform.CurrentActivity?.CurrentFocus != null)
            Platform.CurrentActivity.HideKeyboard(Platform.CurrentActivity.CurrentFocus);
#endif
            if (pageNumberChanged && targetPageNumber != PdfViewer.PageNumber)
            {
                if (targetPageNumber > 0 && targetPageNumber <= PdfViewer.PageCount)
                {
                    PdfViewer.GoToPage(targetPageNumber);
                    pageNumberChanged = false;

                }
                else
                {
                    // Shows the invalid page number to the user when trying to go to the entered page number when the loaded PDF document does not have a page number.
                    MainThread.BeginInvokeOnMainThread(() =>
                    {
                        MessageBox.Show("Error", "Invalid Page Number", "OK");
                    });
                    PageNumberEntry.Text = PdfViewer.PageNumber.ToString();
                }
                PageNumberEntry.Unfocus();
            }
        }

        /// <summary>
        /// Handles the button enable and disable states when the button property changes.
        /// </summary>
        private void Button_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (sender is Button button)
            {
                if (e.PropertyName == "IsEnabled")
                {
                    if (!button.IsEnabled)
                    {
                        button.Unfocus();
                    }
                    else
                    {
                        Dispatcher.DispatchDelayed(TimeSpan.FromMilliseconds(1), () =>
                        {
                            VisualStateManager.GoToState(button, "Normal");
                        });
                    }
                }
            }
        }

        /// <summary>
        /// Handles password requested event.
        /// </summary>
        private void PdfViewer_PasswordRequested(object sender, Syncfusion.Maui.PdfViewer.PasswordRequestedEventArgs e)
        {
            //Show the password dialog.
            PasswordDialog.Dispatcher.Dispatch(() => PasswordDialog.IsVisible = true);

            //Block the current thread until user enters the password.
            manualResetEvent.WaitOne();
            manualResetEvent.Reset();

            //Pass the user password to PDF Viewer to validate and load the PDF.
            e.Password = PasswordDialog.Password;
            e.Handled = true;
        }

        /// <summary>
        /// Handles when document is failed to load.
        /// </summary>
        private void PdfViewer_DocumentLoadFailed(object sender, Syncfusion.Maui.PdfViewer.DocumentLoadFailedEventArgs e)
        {
            //Show the incorrect password message to the user, when document failed to load if the password is invalid.
            if (e.Message == "Can't open an encrypted document. The password is invalid.")
            {
                //Unloads the current document.
                PdfViewer.DocumentSource = null;
                MainThread.BeginInvokeOnMainThread(() => MessageBox.Show("Incorrect Password", "The password you entered is incorrect. Please try again."));
            }
            else if (e.Message == "Invalid cross reference table.")
            {
                //Show the error message to the user, when corrupted document is loaded.
                MainThread.BeginInvokeOnMainThread(() => MessageBox.Show("Error", "Failed to load the PDF document."));
                if (BindingContext is PdfViewerViewModel context)
                {
                    context.PdfDocumentStream = null;
                }
            }
        }

        private string Password { get; set; }

        private void PasswordDialog_CancelClicked(object sender, EventArgs e)
        {
            Password = null;
            PasswordEntry.Text = string.Empty;
            PasswordDialog.IsVisible = false;
            manualResetEvent.Set();
        }

        private void PasswordDialog_OkClicked(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(PasswordEntry.Text))
            {
                PasswordHelperText.TextColor = Colors.Red;
                PasswordHelperText.Text = "* Password cannot be empty";
                return;
            }

            Password = PasswordEntry.Text;
            PasswordEntry.Text = string.Empty;
            PasswordDialog.IsVisible = false;
            manualResetEvent.Set();
        }

        private void PasswordEntry_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(e.NewTextValue))
                PasswordHelperText.Text = string.Empty;
        }

        private void ShowPasswordCheckBox_CheckedChanged(object sender, CheckedChangedEventArgs e)
        {
            PasswordEntry.IsPassword = !e.Value;
        }

        private void ShowMessage(string title, string message, string closeText = "CLOSE")
        {
            MessageTitle.Text = title;
            MessageText.Text = message;
            MessageOkButton.Text = closeText;
            MessageDialog.IsVisible = true;
        }

        /// <summary>
        /// Handles when the message box's ok button is clicked.
        /// </summary>
        private void MessageBox_OKClicked(object sender, EventArgs e)
        {
            MessageDialog.IsVisible = false;
            if (BindingContext is PdfViewerViewModel context)
            {
                // Retry loading the document.
                if (context.PdfDocumentStream != null)
                    PdfViewer.DocumentSource = context.PdfDocumentStream;
            }
        }

        /// <summary>
        /// Toggles the visibility of the page layout options when the page layout button is clicked.
        /// </summary>
        private void pageLayoutClicked(object sender, EventArgs e)
        {
            PageLayout.IsVisible = !PageLayout.IsVisible;
        }

        /// <summary>
        /// Handles the change of page layout mode based on user selection.
        /// </summary>
        private void ContinuousPageLayout_Tapped(object sender, TappedEventArgs e)
        {
            SetPageLayoutMode(PageLayoutMode.Continuous);
        }

        private void SinglePageLayout_Tapped(object sender, TappedEventArgs e)
        {
            SetPageLayoutMode(PageLayoutMode.Single);
        }

        private void SetPageLayoutMode(PageLayoutMode mode)
        {
            if (BindingContext is PdfViewerViewModel context)
            {
                context.PageLayoutMode = mode;
                PdfViewer.PageLayoutMode = mode;
                PageLayout.IsVisible = false;
            }
        }

    }
}

```
