)
We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load PDF document in SfPdfViewer Xamarin.Forms from given URL?

Platform: Xamarin.Forms |
Control: PdfViewer |
Published Date: May 7, 2018 |
Last Revised Date: July 3, 2020 |
.NET Framework: 4.5.1

PDF documents can be loaded into the PdfViewer from a given URL by downloading the PDF and then loading the stream of the downloaded file to the PdfViewer. Refer to the following code snippets.

 

Add a new ViewModel class named PdfViewerViewModel to the PCL or .NET Standard project. The class must implement the INotifyPropertyChanged interface.

C#

public class PdfViewerViewModel : INotifyPropertyChanged

 

Define a new property named PdfDocumentStream in the class. In the setter accessor of the property raise the PropertyChanged event once the value is set to the private field of the property.

C#

public Stream PdfDocumentStream
{
   get { return pdfDocumentStream; }
   set
   {
       //Check the value whether it is the same as the currently loaded stream
       if (value != pdfDocumentStream)
       {
          pdfDocumentStream = value;
          PropertyChanged(this, new PropertyChangedEventArgs("PdfDocumentStream"));
       }
   }
}

 

Define an asynchronous method that gets the stream from the given URL.

C#

private async Task<Stream> DownloadPdfStream(string URL)
{
     HttpClient httpClient = new HttpClient();
     HttpResponseMessage response = await httpClient.GetAsync(URL);
   //Check whether redirection is needed
     if((int)response.StatusCode == 302)
     {
      //The URL to redirect is in the header location of the response message
          HttpResponseMessage redirectedResponse = await httpClient.GetAsync(response.Headers.Location.AbsoluteUri);
          return await redirectedResponse.Content.ReadAsStreamAsync();
     }
     return await response.Content.ReadAsStreamAsync();}

 

The stream returned by the previous method must be set to the PdfDocumentStream property in the constructor. As the constructor cannot be executed asynchronously, define another asynchronous method and call the DownloadPdfStream method from inside it.

C#

private async void SetStreamAsync()
{
   PdfDocumentStream = await DownloadPdfStream(URL);
}

Call this method from the constructor synchronously.

C#

public PdfViewerViewModel()
{
   SetStreamAsync();
}

 

Bind the InputFileStream property of PdfViewer to the PdfDocumentStream property.

XAML

<viewer:SfPdfViewer x:Name="pdfViewerControl" InputFileStream="{Binding PdfDocumentStream}" />

 

 

Sample link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/LoadPDFFromURL783209975.zip

Note

PdfViewer loads the PDF as soon as the InputFileStream property is set to a stream. In the previous sample PDFs may not load immediately. This is due to the time taken for the stream to be fetched from the URL asynchronously. This must not be taken as a PdfViewer defect.

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Danny Ackerman
Jun 06, 2018

I tried doing this, but the PDF Viewer spinner just goes on forever, never loading the document.

Reply
Navaneetha Kannan Sudalai Muthu [Syncfusion]
Jun 07, 2018

Hi Danny,

 

The sample attached in this KB article works as expected if run as such. It will take approximately 2 seconds to fetch the PDF from the given URL during which PdfViewer’s loading indicator will rotate as you have mentioned.

 

If you have changed the URL provided in the sample and the PDF at the URL is very large it may take longer to fetch the PDF from the URL. Until the PDF stream is obtained the loading indicator will rotate in PdfViewer. If this is the case kindly wait until the PDF is fetched and loaded. As we have mentioned in the article it is not the responsibility of PdfViewer to read the PDF from URL. The PdfViewer loads the PDF as soon as it is read from the URL. So the delay occurs in reading the PDF and this is not a defect of PdfViewer.

 

If you still face the issue after trying the above suggestion, please contact Syncfusion support (support@syncfusion.com) to get further assistance.

Best,

Navaneetha Kannan 

Navaneetha Kannan Sudalai Muthu [Syncfusion]
Jun 07, 2018

Hi Danny,

 

The issue may also occur if the device (iPhone or Android) to which the application is deployed does not have an active internet connection. It is not enough that the machine in which the sample is built and deployed is connected to the network, but the device in which the application runs has to be connected to the internet. Kindly ensure that the device is connected to either internet (if the URL is online) or the local network (if the URL is hosted locally).

 

Best,

Navaneetha Kannan

Niels Andersen
Oct 27, 2020

After upgrading to 18.3.x this method fails (work before this version). The PDF is downloaded ok, the Stream object seems fine but when the property-change fires (on change to the Stream), we get a NullReferenceException

{System.NullReferenceException: Object reference not set to an instance of an object. at Syncfusion.SfPdfViewer.XForms.Droid.ScrollViewEx.LoadDocument (System.IO.Stream inputStream) [0x001ab] in :0 at Syncfusion.SfPdfViewer.XForms.Droid.SfPdfDocumentViewRenderer.OnElementPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00083] in :0 at (wrapper delegate-invoke) .invoke_void_object_PropertyChangedEventArgs(object,System.ComponentModel.PropertyChangedEventArgs) at Xamarin.Forms.BindableObject.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:229 at Xamarin.Forms.Element.OnPropertyChanged (System.String propertyName) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\Element.cs:358 at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00114] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:461 at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x00042] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:334 at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:311 at Syncfusion.SfPdfViewer.XForms.DocumentViewHelper.set_InputFileStream (System.IO.Stream value) [0x00000] in <58958fe884e342ac898d77120a7e28c8>:0 at Syncfusion.SfPdfViewer.XForms.SfPdfViewer.OnPdfViewerPropertyChanged (System.String propertyName) [0x00019] in <58958fe884e342ac898d77120a7e28c8>:0 at Syncfusion.SfPdfViewer.XForms.SfPdfViewer.OnInputFileStreamChanged (Xamarin.Forms.BindableObject bindable, System.Object oldValue, System.Object newValue) [0x0048c] in <58958fe884e342ac898d77120a7e28c8>:0 at Xamarin.Forms.BindableObject.SetValueActual (Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject+BindablePropertyContext context, System.Object value, System.Boolean currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, System.Boolean silent) [0x00120] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:463 at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x00173] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:397 at Xamarin.Forms.BindingExpression.ApplyCore (System.Object sourceObject, Xamarin.Forms.BindableObject target, Xamarin.Forms.BindableProperty property, System.Boolean fromTarget) [0x00214] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:155 at Xamarin.Forms.BindingExpression.Apply (System.Boolean fromTarget) [0x0003e] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:53 at Xamarin.Forms.BindingExpression+BindingExpressionPart.b__49_0 () [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:742 at Xamarin.Forms.BindingExpression+BindingExpressionPart.PropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs args) [0x000cb] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:753 at Xamarin.Forms.BindingExpression+WeakPropertyChangedProxy.OnPropertyChanged (System.Object sender, System.ComponentModel.PropertyChangedEventArgs e) [0x00012] in D:\a\1\s\Xamarin.Forms.Core\BindingExpression.cs:646 at LoadPDFFromURL.PdfViewerViewModel.set_PdfDocumentStream (System.IO.Stream value) [0x00019] in C:\Users\nan\Downloads\LoadPDFFromURL-984168691\LoadPDFFromURL\LoadPDFFromURL\LoadPDFFromURL\PdfViewerViewModel.cs:27 at LoadPDFFromURL.PdfViewerViewModel.SetStreamAsync () [0x00034] in C:\Users\nan\Downloads\LoadPDFFromURL-984168691\LoadPDFFromURL\LoadPDFFromURL\LoadPDFFromURL\PdfViewerViewModel.cs:41 }

Reply
Ashokkumar Viswanathan [Syncfusion]
Oct 28, 2020

Hi Niels,

The fix for the issue, “Application crashes with null reference exception while loading” will be included in our 2020 Volume 3 Service pack release, which is expected to be available in the month of November 2020.

Regards, Ashok Kumar Viswanathan.

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon