Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Introducing the New .NET MAUI PDF Viewer
Syncfusion, Inc. profile imageJollen Moyani
Jollen Moyani forSyncfusion, Inc.

Posted on • Originally published atsyncfusion.com on

     

Introducing the New .NET MAUI PDF Viewer

Syncfusion is excited to announce the availability of another valuable control,PDF Viewer, for the.NET MAUI platform in the2022 Volume 4 release.

The new .NET MAUI PDF Viewer control allows you to easily add PDF viewing capabilities within your applications. It is supported in Windows, macOS, iOS, and Android platforms.

In this blog, we’ll explore the features of the .NET MAUI PDF Viewer and the steps to get started with it.

Key features

The key features of the new .NET MAUI PDF Viewer are:

  • Virtual scrolling : Easily scroll through the pages in a PDF document with a fluent experience. The pages are rendered on demand in order to enhance the loading and scrolling performance.
  • Magnification : The content of a PDF document can be efficiently zoomed in and out by pinching or changing the zoom factor programmatically.
  • Page navigation : Navigate to the desired pages instantly using the programmatic page navigation or by dragging the scroll box in the UI.
  • Opening password-protected PDFs : Load and view password-protected PDFs in a hassle-free way. Also, you can design a custom password request view and integrate the functionality easily with built-in options.

Getting started with .NET MAUI PDF Viewer

Let’s see how to integrate the Syncfusion .NET MAUI PDF Viewer control in your application and use its basic features.

Creating a .NET MAUI application with the PDF Viewer

Step 1: First, create anew .NET MAUI application inVisual Studio.

Step 2: Then, add theSyncfusion.Maui.PdfViewer NuGet package reference to your project from theNuGet Gallery.

Step 3 : Then, register the handler for the Syncfusion core package in theMauiProgram.cs file. Refer to the following code.

usingSyncfusion.Maui.Core.Hosting;namespacePdfViewerExample{publicstaticclassMauiProgram{publicstaticMauiAppCreateMauiApp(){varbuilder=MauiApp.CreateBuilder();builder.UseMauiApp<App>().ConfigureFonts(fonts=>{fonts.AddFont("OpenSans-Regular.ttf","OpenSansRegular");fonts.AddFont("OpenSans-Semibold.ttf","OpenSansSemibold");});builder.ConfigureSyncfusionCore();returnbuilder.Build();}}}
Enter fullscreen modeExit fullscreen mode

Step 4: Add a new folder to the project namedAssets and add the PDF document you want to view (such asPDF_Succinctly.pdf ) using the PDF Viewer.

Note: You can also load PDF files fromlocal storage or aURL.

Step 5: We are going to use the MVVM binding technique. So, create a new view model class namedPdfViewerViewModel.cs and add the following code to it.

usingSystem.ComponentModel;usingSystem.Reflection;namespacePdfViewerExample{internalclassPdfViewerViewModel:INotifyPropertyChanged{privateStream?m_pdfDocumentStream;/// <summary>/// An event to detect the change in the value of a property./// </summary>publiceventPropertyChangedEventHandler?PropertyChanged;/// <summary>/// The PDF document stream that is loaded into the instance of the PDF viewer./// </summary>publicStreamPdfDocumentStream{get{returnm_pdfDocumentStream;}set{m_pdfDocumentStream=value;OnPropertyChanged("PdfDocumentStream");}}/// <summary>/// Constructor of the view model class./// </summary>publicPdfViewerViewModel(){//Accessing the PDF document that is added as an embedded resource as a stream.m_pdfDocumentStream=typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("PdfViewerExample.Assets.PDF_Succinctly.pdf");}publicvoidOnPropertyChanged(stringname){PropertyChanged?.Invoke(this,newPropertyChangedEventArgs(name));}}}
Enter fullscreen modeExit fullscreen mode

Step 6: In theMainPage.xaml page, import the control namespaceSyncfusion.Maui.PdfViewer , initialize theSfPdfViewer control, and bind the createdPdfDocumentStream to theSfPdfViewer.DocumentSourceproperty. Refer to the following code example.

Note: TheDocumentSource property accepts both stream and byte[] instances.

<?xml version="1.0" encoding="utf-8" ?><ContentPagexmlns="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"xmlns:local="clr-namespace:PdfViewerExample"x:Class="PdfViewerExample.MainPage"><ContentPage.BindingContext><local:PdfViewerViewModelx:Name="viewModel"/></ContentPage.BindingContext><ContentPage.Content><syncfusion:SfPdfViewerx:Name="PdfViewer"DocumentSource="{Binding PdfDocumentStream}"></syncfusion:SfPdfViewer></ContentPage.Content></ContentPage>
Enter fullscreen modeExit fullscreen mode

Note: If you are using multiple pages in your app, then make sure to unload the document from theSfPdfViewer class while leaving the page that has it to release the memory and resources consumed by the loaded PDF document. You can unload documents by calling theUnloadDocument method. Also, while changing or opening different documents on the same page, the previously loaded document will be unloaded automatically by theSfPdfViewer.

Step 7: Finally, run the app. Refer to the following output image.

Integrating PDF Viewer in a .NET MAUI application

Integrating PDF Viewer in a .NET MAUI application

Note: For more details, refer to the.NET MAUI PDF Viewer GitHub demo.

Creating a custom toolbar

Currently, we do not support a built-in toolbar in the .NET MAUI PDF Viewer. However, we have provided APIs for performing magnification and page navigation programmatically.

Therefore, you can design a simple custom toolbar and invoke the operations using the following APIs.

Load document API

API

Type

Description

DocumentSource

Property

Represents the source object to load PDF files from the stream or byte array. This property helps load a PDF document during control initialization and while switching to the document dynamically.

Page navigation APIs

APIs

Type

Description

GoToFirstPageCommand

Property

Gets the value that represents the GoToFirstPage command.

GoToLastPageCommand

Property

Gets the value that represents the GoToLastPage command.

GoToNextPageCommand

 

Property

Gets the value that represents the GoToNextPage command.

GoToPreviousPageCommand

 

Property

Gets the value that represents the GoToPreviousPage command.

GoToPageCommand

Property

Gets the value that represents the GoToPage command.

GoToFirstPage()

Method

Navigates to the first page of a PDF.

GoToLastPage()

Method

Navigates to the last page of a PDF.

GoToNextPage()

Method

Navigates to the next page in a PDF.

GoToPreviousPage()

Method

Navigates to the previous page in a PDF.

GoToPage(int)

Method

Navigates to the specified page of the PDF document. The parameter represents the page number (1-based index).

PageNumber

Property

Returns the current page number.

PageNumberProperty

Bindable property

The backing store for the PageNumber bindable property.

PageCount

Property

Returns the total number of pages in a PDF document.

PageCountProperty

Bindable property

The backing store for the PageCount bindable property.

ZoomAPIs

APIs

Type

Description

ZoomFactor

Property

Returns and sets the zoom factor. The default value is 1, which represents 100% zoom. This value can be from 1 to 4.

Note: For more details, refer to the.NET MAUI PDF Viewer with a custom toolbar demo on GitHub.

.NET MAUI PDF Viewer with a Custom Toolbar

.NET MAUI PDF Viewer with a Custom Toolbar

Opening a password-protected PDF

To open a password-protected or encrypted PDF document, you can use theLoadDocument() method by providing the password along with the document stream.

Refer to the following code example.

stringpassword="PASSWORD";pdfViewer.LoadDocument(pdfDocumentStream,password);
Enter fullscreen modeExit fullscreen mode

Using the following APIs, you can determine whether a PDF document is password protected.

APIs

Type

Description

PasswordRequested

Event

This event will be called when a password is required to open a PDF document.

DocumentLoadFailed

Event

This event will be called when the document fails to load. For example, this event occurs with the message, “Can't open an encrypted document. The password is invalid,” when we provide a wrong or invalid password.

Note: For more details, refer to theopening a password-protected PDF using the .NET MAUI PDF Viewer demo on GitHub.

Opening a Password-Protected PDF Document Using .NET MAUI PDF Viewer

Opening a Password-Protected PDF Document Using .NET MAUI PDF Viewer

Reference

Refer to the.NET MAUI PDF Viewer documentation to learn about its other features.

Coming soon

We have planned and are working to support the following major features in the .NET MAUI PDF Viewer control in upcoming releases:

  • Built-in toolbar.
  • Text search.
  • Text selection and copy.
  • Bookmark, TOC, and hyperlink navigation.
  • Localization.
  • Accessibility.
  • RTL text.
  • The ability to add, edit, save, and remove annotations.
  • Form filling with edit and save functions.
  • Single-page layout mode.
  • Thumbnails.
  • Themes.

Conclusion

Thanks for reading! I hope you enjoyed learning about the new Syncfusion.NET MAUI PDF Viewer introduced in2022 Volume 4. You can download and check out our MAUI demo app fromGoogle Playand theMicrosoft Store.

Also, check out ourRelease Notes and theWhat’s New pages to see the other updates in this release.

For current customers, the new version is available for download from theLicense and Downloads page. If you are not yet a Syncfusion customer, you can try our 30-dayfree trial to check out our newest features.

You can share your feedback and questions through the comments section below or contact us through oursupport forums,support portal, orfeedback portal. We are always happy to assist you!

Related blogs

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Syncfusion provides third-party UI components for React, Vue, Angular, JavaScript, Blazor, .NET MAUI, ASP.NET MVC, Core, WinForms, WPF, UWP and Xamarin.

More fromSyncfusion, Inc.

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp