Category / Section
How to display PDF document from URL using SfPdfViewer in Xamarin.Android?
1 min read
How to display PDF document from URL using SfPdfViewer in Xamarin.Android?
At present SfPdfViewer in Xamarin.Android do not have a direct support to display the PDF document from URL. However, as a workaround you can download the document or files from URL as Stream using WebClient class and load it in the PDF viewer.
The below code snippet illustrates how to download the PDF document from URL as Stream using WebClient:
C#:
/// <summary> /// Download PDF document as stream from the given URL /// </summary> /// <param name="URL">URL which holds 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 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 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