Getting Started with .NET MAUI PDF Viewer
16 Sep 20249 minutes to read
This section guides you through setting up and configuring PDF viewer in your .NET MAUI application. Follow the steps below to add the PDF viewer to your project and load a PDF document.
To get started quickly, you can also check out our video tutorial below.
Prerequisites
Before proceeding, ensure the following are in place:
- Install .NET 7 SDK or later.
- Set up a .NET MAUI environment with Visual Studio 2022 (v17.3 or later) or VS Code. For VS Code users, ensure that the .NET MAUI workload is installed and configured as described here.
Step 1: Create a New MAUI Project
Visual Studio
- Go to File > New > Project and choose the .NET MAUI App template.
- Name the project and choose a location, then click Next.
- Select the .NET framework version and click Create.
Visual Studio Code
- Open the command palette by pressing
Ctrl+Shift+P
` and type .NET:New Project and enter. - Choose the .NET MAUI App template.
- Select the project location, type the project name and press enter.
- Then choose Create project
Step 2: Install the Syncfusion MAUI PDF Viewer NuGet Package
- In Solution Explorer, right click the project and choose Manage NuGet Packages for Visual Studio and Add NuGet Package for Visual Studio Code.
- Search for Syncfusion.Maui.PdfViewer and install the latest version.`
- Ensure the necessary dependencies are installed correctly, and the project is restored.
Step 3: Register the Syncfusion Core Handler
Syncfusion.Maui.Core is a dependency for all MAUI Syncfusion controls. This package will be automatically installed as a dependency when Syncfusion.Maui.PdfViewer NuGet is installed. Register the Syncfusion core handler in the MauiProgram.cs
file.
using Syncfusion.Maui.Core.Hosting;
namespace PdfViewerExample
{
public class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
builder.ConfigureSyncfusionCore();
return builder.Build();
}
}
}
Step 4: Add PDF Viewer to the Project
Open the MainPage.xaml
file and follow the steps below.
- Import the control namespace
Syncfusion.Maui.PdfViewer
, and then add the SfPdfViewer control inside the<ContentPage.Content>
tag as follows. - Name the PDF viewer control as
pdfViewer
.
<?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:syncfusion="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer"
x:Class="PdfViewerExample.MainPage">
<ContentPage.Content>
<syncfusion:SfPdfViewer x:Name="pdfViewer">
</syncfusion:SfPdfViewer>
</ContentPage.Content>
</ContentPage>
Step 5: Load a PDF Document
- From the solution explorer of the project, add a new folder to the project named
Assets
and add the PDF document you need to load in the PDF viewer. Here, a PDF document namedPDF_Succinctly.pdf
is used.` - In Visual Studio, right click the added PDF document and set its
Build Action
asEmbedded Resource
. In VS Code, open the.csproj
file and add the following code snippet to embed the PDF document as a resource.
<ItemGroup>
<EmbeddedResource Include="Assets\PDF_Succinctly.pdf" />
</ItemGroup>
- In this example, we will load the PDF document through MVVM binding. Create a new C# file named
PdfViewerViewModel.cs
and add the following code snippet.
using System.Reflection;
using System.ComponentModel;
namespace PdfViewerExample
{
class PdfViewerViewModel : INotifyPropertyChanged
{
private Stream pdfDocumentStream;
/// <summary>
/// Occurs when a property value changes.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// Gets or sets the stream of the currently loaded PDF document.
/// </summary>
public Stream PdfDocumentStream
{
get
{
return pdfDocumentStream;
}
set
{
pdfDocumentStream = value;
OnPropertyChanged(nameof(PdfDocumentStream));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="PdfViewerViewModel"/> class.
/// </summary>
public PdfViewerViewModel()
{
// Load the embedded PDF document stream.
pdfDocumentStream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("PdfViewerExample.Assets.PDF_Succinctly.pdf");
}
/// <summary>
/// Raises the <see cref="PropertyChanged"/> event for the specified property name.
/// </summary>
/// <param name="name">The name of the property that changed.</param>
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
- Open the
MainPage.xaml
file again and add the default namespace of the created .NET MAUI project and name it aslocal
. Here the default namespace of the demo samplePdfViewerExample
is used. - Set an instance of the
PdfViewerViewModel
class as theBindingContext
of theContentPage
. - Bind the PDF viewer’s DocumentSource to the
PdfDocumentStream
property of thePdfViewerViewModel
class.
<?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:syncfusion="clr-namespace:Syncfusion.Maui.PdfViewer;assembly=Syncfusion.Maui.PdfViewer"
x:Class="PdfViewerExample.MainPage">
<ContentPage.BindingContext>
<local:PdfViewerViewModel x:Name="viewModel" />
</ContentPage.BindingContext>
<ContentPage.Content>
<syncfusion:SfPdfViewer x:Name="pdfViewer" DocumentSource="{Binding PdfDocumentStream}">
</syncfusion:SfPdfViewer>
</ContentPage.Content>
</ContentPage>
NOTE
- While changing or opening different documents on the same page, the previously loaded document will be unloaded automatically by the SfPdfViewer.
- And, if you are using multiple pages in your application, then make sure to unload the document from the SfPdfViewer while leaving the page that has it to release the memory and resources consumed by the PDF document that is loaded. The unloading of documents can be done by calling the UnloadDocument method.
Step 6: Running the Application
- Select the target framework, device or emulator.
- Press
F5
to run the application. - The PDF document will be loaded in the PDF viewer control as shown in the following screenshot.
The Getting Started example project for the.NET MAUI PDF Viewer can be downloaded here.
NOTE
You can refer to our .NET MAUI PDF Viewer feature tour page for its groundbreaking feature representations. You can also explore our .NET MAUI PDF Viewer Example that shows you how to render the PDF Viewer in .NET MAUI.