How to Replace a Built-In Toolbar Item in .NET MAUI PDF Viewer?
This article demonstrates how to replace a built-in toolbar item in the .NET MAUI PDF Viewer with a custom application-level control, placed in the original item’s position. Toolbar items can be accessed and modified using predefined identifiers for both mobile and desktop platforms:
In this example, the “MoreOptions” toolbar item—which contains the dropdown options “Outline” and “Print”—is replaced by a custom print button in the top toolbar on both Android and iOS platforms.
Prerequisites
- A .NET MAUI project is set up.
- The Syncfusion.Maui.PdfViewer NuGet package is installed.
Steps
1. Install Required NuGet Package
Create a new MAUI App and install the Syncfusion.Maui.PdfViewer package using either.
- NuGet Package Manager
- NuGet CLI
2. Initialize and Configure the PDF Viewer
Start by adding the Syncfusion PDF Viewer control to your XAML file.
a. Add the Syncfusion namespace in MainPage.xaml
This namespace enables access to the PDF Viewer control.
XAML:
xmlns:syncfusion="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer"
b. Add the PDF Viewer to your layout
XAML:
<Grid>
<syncfusion:SfPdfViewer x:Name="pdfViewer" ></syncfusion:SfPdfViewer>
</Grid>
c. Load the PDF in MainPage.Xaml.cs
C#:
Stream? stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("ReplaceToolbarItem.Assets.PDF_Succinctly.pdf");
pdfViewer.LoadDocument(stream);
3. Replace a Toolbar Item in Android & iOS
Follow these steps to replace the built-in `MoreOptions toolbar item with a custom print button:
a. Retrieve the top toolbar
C#:
// Retrieve the top toolbar.
var toolbar = pdfViewer.Toolbars?.GetByName("TopToolbar");
b. Retrieve the specific toolbar item (MoreItem)
C#:
// Retrieve the toolbar item "MoreOptions" from the top toolbar.
var item = toolbar?.Items?.GetByName("MoreItem");
c. Get the index position of the retrieved item
C#:
// Obtain the index value of the retrieved toolbar item.
var index = (item?.Index != null) ? (int)item.Index : -1;
d. Remove the item from the toolbar
C#:
// Remove the "MoreOptions" toolbar item from the top toolbar.
if (item != null)
toolbar?.Items?.Remove(item);
e. Create a new custom button (e.g., a Print button)
C#:
// Create a new print button to replace the "MoreOptions" toolbar item.
Button printButton = new Button
{
Text = "\ue77f", // Printer icon (Unicode)
FontSize = 24,
FontFamily = "MauiMaterialAssets",
BackgroundColor = Colors.Transparent,
TextColor = Color.FromArgb("#49454F")
};
f. Attach a click event to handle printing
C#:
// Add a click event handler to the newly created button for initiating document printing
printButton.Clicked += (s, e) =>
{
pdfViewer.PrintDocument();
};
g. Insert the button into the toolbar at the original item’s position
C#:
// Replace the print button at the index of the "MoreOptions" toolbar item in the top toolbar.
toolbar?.Items?.Insert(index, new Syncfusion.Maui.PdfViewer.ToolbarItem(printButton, "printButton"));
4. Run the App
Build and run the application on Android and iOS. The PDF Viewer will display the loaded document with the custom print button replacing the default “MoreOptions” toolbar item.
Desktop Support
This approach can be extended to desktop platforms using
Screenshots
Default Toolbar with “MoreOptions” Item
After Replacing with Custom Print Button
Sample Code
Conclusion
We hope this article has helped you understand how to replace a built-in toolbar item with a custom application-level control in the .NET MAUI PDF Viewer.
Quick Summary of Toolbar APIs
Purpose | API Used |
---|---|
Access Toolbar | pdfViewer.Toolbars?.GetByName("Toolbar Name") |
Access Toolbar Item | .Items?.GetByName("Item Name") |
Remove Item | .Items?.Remove(item) |
Add Custom Item | .Items?.Insert(index, new ToolbarItem(...)) |
You can refer to our .NET MAUI PDF Viewer feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!