Category / Section
How to display PDF document from URL using SfPdfViewer in Xamarin.Android?
1 min read
How to display a PDF document from a URL using SfPdfViewer in Xamarin.Android?
At present, SfPdfViewer in Xamarin.Android does not have direct support to display a PDF document from a URL. However, as a workaround, you can download the document or files from the URL as a Stream using the WebClient class and load it into the PDF viewer.
The code snippet below illustrates how to download the PDF document from a URL as a Stream using WebClient:
C#:
/// <summary>
/// Download a PDF document as a stream from the given URL
/// </summary>
/// <param name="URL">URL which holds the PDF document</param>
/// <returns></returns>
private Stream DownloadPdfStream(string URL)
{
// Initialize WebClient
WebClient webClient = new WebClient();
// Initialize Uri
var uri = new System.Uri(URL);
// Returns the document stream from the given URL
return webClient.OpenRead(uri);
}
Then, we can load the downloaded PDF document Stream to display it in the PDF viewer in Xamarin.Android. Please find the code snippet below for your reference:
C#:
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set the activity content from a layout resource
SetContentView(Resource.Layout.Main);
// Finds a SfPdfViewer that was identified by resource id
var pdfViewerControl = FindViewById<SfPdfViewer>(Resource.Id.pdfviewerControl);
// Download the PDF document from the given URL as a stream
Stream pdfStream = DownloadPdfStream("http://www.syncfusion.com/downloads/support/directtrac/general/pd/Xamarin-Forms-Succinctly1843641035");
// Load the downloaded PDF document stream to display it in SfPdfViewer
pdfViewerControl.LoadDocument(pdfStream);
}
Sample Link:
https://www.syncfusion.com/downloads/support/directtrac/general/ze/PdfViewerSample-1610083496