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. Image for the cookie policy date

Load PDF from URL in PDFViewer

Hello,

Does the PDF viewer allow to load documents from a URI or just from embedded documents?

thanks,
Pawan

10 Replies

BS Balasubramanian Sundararajan Syncfusion Team October 26, 2016 01:04 PM UTC

Hi Pawan, 

Thank you for using Syncfusion product. 

We can load the PDF document from the URL in our Xamarin PdfViewer. We have created a simple sample which loads the PDF document from the URL in the WebClient and returns the PDF document stream. Then the resultant PDF document stream can be loaded into the PdfViewerControl. The sample can be downloaded from the following link, 


Code Example: 

Below code example is for loading the URL in the WebClient for retrieving the file stream. 

 
public Stream DownloadPdfStream(string URL, string documentName) 
{ 
    
   var uri = new System.Uri(URL); 
 
    //Returns the PDF document stream from the given URL 
    return m_webClient.OpenRead(uri); 
 


Note: The above process will take place in the Custom Renderer. 

Below code example is for loading the PDF document stream in the Pdf viewer control Xamarin Forms( iOS / Android) 

 
protected override void OnAppearing() 
{ 
      base.OnAppearing(); 
 
      //Provide the PDF document URL in the below overload. 
      Stream documenStream = DependencyService.Get<IDownloader>().DownloadPdfStream(URL,"Sample"); 
 
      //Loads the PDF document as Stream to PDF viewer control 
      pdfViewerControl.LoadDocument(documenStream); 
      pageCountLabel.Text = pdfViewerControl.PageCount.ToString(); 
      
} 


Note: We recommend you to load the PDF document in OnAppearing method of the MainView. 

Please let us know if you need any further assistance. 

Thanks, 
Balasubramanian S 



AP Andy Pybus January 19, 2017 11:49 PM UTC

I'm hoping to implement a simple pdf viewer in a (portable) Xamarin.forms.droid app. 

I have updated all the nuget packages and changed the syncfusion references to the relevant dll's in my own version of syncfusion.

However, when I recompile your sample program I get the following error:

Error CS0234 The type or namespace name 'PdfViewerControl' does not exist in the namespace 'Syncfusion.SfPdfViewer.XForms' (are you missing an assembly reference?)

Can you help - (I'm using the latest versions of VS2015 and Syncfusion)?

Yours Sincerely,

Dr Andy Pybus





AS Aravinth Sampath Syncfusion Team January 20, 2017 12:33 PM UTC

Hi Pawan, 
 
The namespace error occurred because we have renamed the control from  PdfViewerControl to SfPdfViewer in our Essential studio volume 4, 2016 release based on standard naming convention of our controls.  
 
Please find the documentation and release notes link for the same below. 
 
Documentation link: 
 
Release Notes link: 
 
We request you to use the below Code snippet for using PdfViewerControl in your project to overcome the issue. 
 
<ContentPage.Content> 
 
<syncfusion:SfPdfViewer x:Name="pdfViewer"> 
 
</syncfusion:SfPdfViewer> 
 
</ContentPage.Content> 
 
 
Please let us know if you need further assistance. 
 
Regards, 
Aravinth 



MK Mireille Khalifeh February 14, 2017 09:48 AM UTC

Hello,
I have copied the example you have provided into my project, when I run the application I get this error Java.Lang.NoClassDefFoundError: android/util/Size, on the pdfViewerControl.LoadDocument(documenStream); line

public partial class PDF_Viewer : ContentPage {
        string URL = "http://unec.edu.az/application/uploads/2014/12/pdf-sample.pdf";
        public PDF_Viewer() {
            InitializeComponent();
            goToPreviousButton.Clicked += OnGoToPreviousPageClicked;
            goToNextButton.Clicked += OnGoToNextPageClicked;
            pageNumberEntry.Completed += CurrentPageEntry_Completed;
            pdfViewerControl.PageChanged += PdfViewerControl_PageChanged;
            pageNumberEntry.Text = "1";
        }

        protected override void OnAppearing() {
            base.OnAppearing();

            //Provide the PDF document URL in the below overload.
            Stream documenStream = DependencyService.Get<IDownloader>().DownloadPdfStream(URL, "Sample");

            //Loads the PDF document as Stream to PDF viewer control
            pdfViewerControl.LoadDocument(documenStream);

            pageCountLabel.Text = pdfViewerControl.PageCount.ToString();


        }

        /// <summary>
        /// Triggers whenever the PDF document page gets changed.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"> which contains the current page number</param>
        private void PdfViewerControl_PageChanged(object sender, PageChangedEventArgs args) {
            pageNumberEntry.Text = args.PageNumber.ToString();
        }

        private void CurrentPageEntry_Completed(object sender, EventArgs e) {
            int pageNumber = 1;
            if (int.TryParse(((sender as Entry).Text), NumberStyles.Integer, CultureInfo.InvariantCulture, out pageNumber)) {
                if ((sender as Entry) != null && pageNumber > 0 && pageNumber <= pdfViewerControl.PageCount)
                    pdfViewerControl.GoToPage(int.Parse((sender as Entry).Text));
                else {
                    DisplayAlert("Alert", "Please enter the valid page number.", "OK");
                    (sender as Entry).Text = pdfViewerControl.PageNumber.ToString();
                }
            }
            else {
                DisplayAlert("Alert", "Please enter the valid page number.", "OK");
                (sender as Entry).Text = pdfViewerControl.PageNumber.ToString();
            }

        }

        void OnGoToPreviousPageClicked(object sender, EventArgs e) {
            //Navigates to previous page of the PDF document.
            pdfViewerControl.GoToPreviousPage();
        }

        void OnGoToNextPageClicked(object sender, EventArgs e) {
            //Navigates to the next page of the PDF document.
            pdfViewerControl.GoToNextPage();
        }

    }
}

Thanks,
Mireille


BS Balasubramanian Sundararajan Syncfusion Team February 15, 2017 09:08 AM UTC

Hi Mireille, 
 
After analyzing the error details provided, we suspect that you are using use Android operation system with API level below than 21.  
 
We have provided support for PDF viewer from the API level of 21 (Operating System – Android 5.0 (Lollipop) and more). So it will throw such errors when your device does not meet the minimum requirement. 
 
The reported issue will get resolved if we use the device with operating system of Android 5.0 – Lollipop (API Level-21) or more.  
 
Kindly refer the following link for more details about the minimum requirement of PDF Viewer and android.util.Size, 
 
 
 
Please let me know if you need further assistance. 
 
Thanks, 
Balasubramanian S 



TN Thaine Norris September 12, 2017 04:29 PM UTC

Hello,


I was able to build the sample project referred to above, but when I try to run it in the iOS emulator (iOS 10.3) then it throws an exception with the following error.

2017-09-11 21:26:17.781 GettingStarted_PDFVieweriOS[54443:3987162] warning: Invalid type Syncfusion.SfPdfViewer.XForms.iOS.ScrollViewEx for instance field Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer:scrollView

Can you help?

Thanks,
Thaine



TN Thaine Norris September 12, 2017 11:03 PM UTC

I have implemented the URL stream loading mechanism as per the sample project you provided. However, it also fails under iOS with the same exception as the sample project. It's odd because I don't understand how changing the source of the data stream would affect the view renderer for SfPdf.

Please help!

Thanks,
Thaine



BS Balasubramanian Sundararajan Syncfusion Team September 13, 2017 01:57 PM UTC

Hi Thaine, 
 
We are unable to reproduce the reported exception “Invalid type Syncfusion.SfPdfViewer.XForms.iOS.ScrollViewEx for instance field Syncfusion.SfPdfViewer.XForms.iOS.SfPdfDocumentViewRenderer:scrollView” while loading the PDF document from the URL in our PDF viewer control. We have created a sample in which we have downloaded the PDF document from the URL and loaded in our PDF viewer control. The sample can be downloaded from the following link, 
 
 
Kindly try the above sample and if you still face the exception then we request you to provide the following details which will help us to investigate and assist you in a better way. 
 
PDF document (URL): 
Sample: 
Syncfusion Essential Studio version: 
Xamarin.Forms Version: 
Xamarin.iOS version: 
Device details: 
 
Please let me know if you have queries. 
 
Thanks, 
Balasubramanian S 



TN Thaine Norris September 13, 2017 05:09 PM UTC

Thank you for the new sample! It does work. I noticed that it references Syncfusion version 15.3.0.29, whereas I was using 15.3.0.26. Apparently the bug was fixed in the new version. Thank you for looking into it.

Thaine



BS Balasubramanian Sundararajan Syncfusion Team September 14, 2017 09:42 AM UTC

Hi Thaine, 
 
We are glad to hear that the issue has been resolved in our end. Please get back to us if you need any further assistance. 
 
Thanks, 
Balasubramanian S 


Loader.
Up arrow icon