"This stream does not support seek operations" error when opening webClient.OpenRead(uri) stream

I have implemented the PDF Viewer in my Xamarin Forms project and it works perfectly in Android, however the identical code fails with the error "This stream does not support seek operations" on IOS.  I have tested this on IOS 12 and IOS 13 with the same error on both.

I have tried binding to the InputFileStream property and also tried via LoadDocument with the same error.

Here is the applicable ViewModel code that the PDF Viewer is bound to:

        public string PDFFileURL { get; set; }
        public Stream PDFStream => DownloadPdfStream(PDFFileURL);
        private Stream DownloadPdfStream(string URL)
        {
            try
            {
                if (URL.IsNullOrEmpty()) return Stream.Null;
                //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);
            } catch (Exception exception)
            {
                Crashes.TrackError(exception);
                if (Debugger.IsAttached) Debugger.Break();
                return Stream.Null;
            }
        }


And here is the XAML:
        <syncfusion:SfPdfViewer x:Name="pdf_Viewer" InputFileStream="{Binding PDFStream}" />


I am at a loss as to what to try next, in researching the issue I ran across an article from 2016 which appears to use the same web client read code. 
I cannot download that code to see if the sample exhibits the same issue.

I am on VS2019 16.6.0, XamForms 4.5.0.725 and the code I am using works on android but fails only on IOS. 

Thanks
M



4 Replies

AV Ashokkumar Viswanathan Syncfusion Team June 1, 2020 07:36 AM UTC

Hi Mike, 
 
Greetings from Syncfusion Support, 
 
While analyzing the issue, “Stream does not support seek operations in iOS Platform, Application crashes as stream obtained using WebClient is not seekable. This issue can be resolved by the loading the memory stream created using stream obtained using WebClient as below, 
 
Please find the below code snippet, 
private Stream DownloadPdfStream(string URL) 
        { 
            try 
            { 
                //Initialize WebClient 
                WebClient webClient = new WebClient(); 
                // Initialize Uri 
                var uri = new System.Uri(URL); 
                //Returns the document stream from the given URL 
                Stream InputStream = webClient.OpenRead(uri); 
                byte[] result; 
                using (var streamReader = new MemoryStream()) 
                { 
                    InputStream.CopyTo(streamReader); 
                    result = streamReader.ToArray(); 
                } 
                return new MemoryStream(result); 
            } 
            catch (Exception exception) 
            { 
                return null; 
            } 
        } 
 
Please find the sample in the below link, 
 
Please try the above solution and let us know whether the provided solution resolved your issue, 
 
Regards, 
Ashok Kumar Viswanathan. 



MR Mike Rowley June 1, 2020 05:00 PM UTC

Thank you for the suggestion, however this code appears to break redirection.  The URL I am testing (https://kellymoore.com/app-contractor-sale) doesn't work with your code on IOS or Android, however Android works with my original code.




AV Ashokkumar Viswanathan Syncfusion Team June 2, 2020 06:27 AM UTC

Hi Mike, 

We were unable to reproduce the issue, “PDF document is not loaded properly” in Android and iOS platform while loading the PDF document from the URL provided in your last update. The solution provided in our last update is working as expected. 

Please find the sample in the below link, which uses the provided URL 

Please find the screenshot below, 
 
Kindly share the following details to analyze more on this issue and assist you with the better solution   
1.       Simple sample/ modify the above sample to reproduce the issue.  
2.       SfPdfViewer version  
3.       Xamarin.Forms version  
4.       Replication procedure to reproduce the issue.  

Regards, 
Ashok Kumar Viswanathan. 



MR Mike Rowley June 2, 2020 03:43 PM UTC

Thank you, I missed the sample project the first time around.  Turns out a loading dialog was deadlocking the load in my project.

Thanks again for your assistance!
M


Loader.
Up arrow icon