How can I help you?
Open a Document from a Base64 String in .NET MAUI PDF Viewer
25 Mar 20264 minutes to read
A PDF document can be opened in the SfPdfViewer from a given Base64 string by converting it to a byte[] and assigning it to the DocumentSource property.
Just by making a few changes to the PdfViewerViewModel.cs shared in the getting started example, you can easily open a document from a given Base64 string by converting the Base64 string to byte array using the FromBase64String method of the Convert class in the System namespace and assigning the obtained byte array to the DocumentSource property in the .NET MAUI PDF Viewer. Refer to the following code example for reference:
using System.Reflection;
using System.ComponentModel;
namespace PdfViewerExample
{
class PdfViewerViewModel : INotifyPropertyChanged
{
private byte[]? m_pdfDocumentByteArray;
/// <summary>
/// An event to detect the change in the value of a property.
/// </summary>
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// The PDF document byte array that is loaded into the instance of the PDF viewer.
/// </summary>
public byte[]? PdfDocumentByteArray
{
get
{
return m_pdfDocumentByteArray;
}
set
{
m_pdfDocumentByteArray = value;
OnPropertyChanged("PdfDocumentByteArray");
}
}
/// <summary>
/// Constructor of the view model class
/// </summary>
public PdfViewerViewModel()
{
SetPdfDocumentByteArray();
}
/// <summary>
/// Gets and sets the document byte array from the given base64 string converted PDF file.
/// </summary>
private void SetPdfDocumentByteArray()
{
// Assign the returned byte array to the PdfDocumentByteArray property
PdfDocumentByteArray = GetByteArrayFromBase64String();
}
// Method to return a MemoryStream containing a base64 string converted PDF file
public byte[]? GetByteArrayFromBase64String()
{
// Decode the Base64 string into a byte array and the Convert class is used for Type conversion in C# (Here we are using to convert a base64 string value to a byte array).
byte[] decodedBytes = System.Convert.FromBase64String(“YourBase64String”);
// return the byte array.
return decodedBytes;
}
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}The example project to open a PDF document from a Base64 string can be downloaded here.