In today’s digital landscape, the ability to manage and access files in a secure, efficient manner is more important than ever. Among the myriad solutions available, Firebase Cloud Storage is a popular one for storing and serving user-generated content. Its secure, scalable storage capabilities have made it a favorite among developers who need to manage files within their apps.
This blog will explain how to load a PDF document from Firebase Cloud Storage into a .NET MAUI app using the Syncfusion .NET MAUI PDF Viewer.
If you already have Firebase Cloud Storage, skip the steps in this section. However, if you’re setting up a new storage, here’s a guide to help you:
Then, select the location closest to you for optimal performance.
Note: It’s important to ensure that your storage has the proper read access for the files. This is crucial for accessing the files in your .NET MAUI app.
We’ve successfully configured the Firebase Cloud Storage. Let’s see how to access and manage its files in your .NET MAUI app.
Follow these steps to load a file in a .NET MAUI application.
Start by creating a new .NET MAUI application in Visual Studio.
Next, add the Syncfusion.Maui.PdfViewer and FirebaseStorage.net NuGet package references in your project from the NuGet Gallery.
Register the handler for the Syncfusion core package in the MauiProgram.cs file.
Refer to the following code example.
using Syncfusion.Maui.Core.Hosting; namespace PdfViewerExample; public static class MauiProgram { public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); builder .UseMauiApp<App>() .ConfigureFonts(fonts => { fonts.AddFont(“OpenSans-Regular.ttf”, “OpenSansRegular”); fonts.AddFont(“OpenSans-Semibold.ttf”, “OpenSansSemibold”); }); builder.ConfigureSyncfusionCore(); return builder. Build(); } }
Using the MVVM binding technique, create a new view model class named PdfViewerViewModel.cs. In this class, implement the LoadPDFFromFirebaseStorage() method. Using this method, we’ll implement the logic to access PDF files from Firebase Cloud Storage by providing the Storage bucket name (refer to the Setup Firebase Console section) and file name.
Refer to the following code example.
using Firebase.Storage; using System.ComponentModel; namespace PdfViewerExample { internal class PdfViewerViewModel : INotifyPropertyChanged { private static string storageBucket = "<STORAGE BUCKET NAME>"; private static string fileName = "<FILE NAME>"; private Stream? m_pdfDocumentStream; /// <summary> /// An event to detect the change in the value of a property. /// </summary> public event PropertyChangedEventHandler? PropertyChanged; /// <summary> /// The PDF document stream that is loaded into the instance of the PDF Viewer. /// </summary> public Stream PdfDocumentStream { get { return m_pdfDocumentStream; } set { m_pdfDocumentStream = value; OnPropertyChanged("PdfDocumentStream"); } } /// <summary> /// Constructor of the view model class. /// </summary> public PdfViewerViewModel() { LoadPDFFromFirebaseStorage(); } /// <summary> /// Load the PDF document from the Firebase storage. /// </summary> public async void LoadPDFFromFirebaseStorage() { HttpClient httpClient = new HttpClient(); FirebaseStorage storage = new FirebaseStorage(storageBucket); string documentUrl = await storage.Child(fileName).GetDownloadUrlAsync(); HttpResponseMessage response = await httpClient.GetAsync(documentUrl); PdfDocumentStream = await response.Content.ReadAsStreamAsync(); } public void OnPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } }
Note: Please replace the storage bucket name and file name before running the application.
Step 5: In the MainPage.xaml page, import the control namespace Syncfusion.Maui.PdfViewer, initialize the SfPdfViewer control, and bind the created PdfDocumentStream to the SfPdfViewer.DocumentSource property.
Refer to the following code example.
<?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" xmlns:local="clr-namespace:PdfViewerExample" 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>
Step 6: Finally, run the app to view your PDF file.
For more details, refer to load and view PDF files from Firebase in .NET MAUI app demo on GitHub.
Thanks for reading! I hope you now know how to load files from Firebase Cloud Storage and use them in your .NET MAUI app. Try this in your app and share your feedback in the comments below.
The .NET MAUI PDF Viewer control lets you view PDF documents seamlessly and efficiently. It has highly interactive and customizable features such as magnification and page navigation.
The new version of Essential Studio® is available from the License and Downloads page for current customers. If you are not a Syncfusion customer, try our 30-day free trial to check out our newest features.
You can share your feedback and questions through the comments section below or contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!