Open a Document From URL in .NET MAUI PDF Viewer (SfPdfViewer)
13 Jun 20233 minutes to read
A PDF document can be opened in the SfPdfViewer from a given URL by converting it to byte[] and assigning the obtained byte[] 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 URL. Refer to the following code example:
using System.Reflection;
using System.ComponentModel;
namespace PdfViewerExample
{
class PdfViewerViewModel : INotifyPropertyChanged
{
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()
{
SetPdfDocumentStream(“https://www.syncfusion.com/downloads/support/directtrac/general/pd/PDF_Succinctly1928776572”);
}
/// <summary>
/// Gets and sets the document stream from the given URL.
/// </summary>
/// <param name="URL">Document URL</param>
private async void SetPdfDocumentStream(string URL)
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync(URL);
PdfDocumentStream = await response.Content.ReadAsStreamAsync();
}
public void OnPropertyChanged(string name)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
The example project to open a PDF document from a given URL can be downloaded here.