Articles in this section
Category / Section

How to Load the Xamarin.Forms PdfViewer from FTP Server?

2 mins read

PDF documents can be loaded into the Xamarin PDF Viewer from the FTP server by downloading the PDF and then loading the stream of the downloaded file to the PDF Viewer.

To start with, add a new ViewModel class named PdfViewerViewModel to the .NET Standard project. The class must implement the INotifyPropertyChanged interface as follows,

C#:

public class PdfViewerViewModel : INotifyPropertyChanged

 

Then, define a new property named PdfDocumentStream in the PdfViewerViewModel 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"));
       }
   }
}

 

Next, define a method named DownloadAndSetStream that gets the stream from the given URL using the  FTP. Then, set the downloaded stream to the private field of the PdfDocumentStream.

C#

Private async void DownloadAndSetStream ()
  {
      //Username and Password.
      string userName = "pdfviewer";
      string passWord = "pdfviewer";
 
      //Accessing the PDF document in FTP server 
      FtpWebRequest request = (FtpWebRequest)WebRequest.Create(URL);
      request.Method = WebRequestMethods.Ftp.DownloadFile;
 
      // Provide credentials to access the FTP server 
      request.Credentials = new NetworkCredential(userName, passWord);
 
      FtpWebResponse response = await request.GetResponseAsync() as FtpWebResponse;
 
      // Downloaded PDF Stream
      Stream stream = response.GetResponseStream(); 
 
      //Convert FTP Data Stream to Memory Stream
      MemoryStream mStream = new MemoryStream();
      stream.CopyTo(mStream);
            
       // Create file stream from the byte array.
       Stream newStream = new MemoryStream(mStream.ToArray());
 
       pdfDocumentStream = newStream;
   }

 

Next, call DownloadAndSetStream method from the constructor of PdfViewerViewModel Class. Please refer to the following code snippet for reference.

C#

public PdfViewerViewModel()
   {
       DownloadAndSetStream ();
   }

 

Then, Bind the InputFileStream property of the PDFViewer to the PdfDocumentStream property in MainPage.Xaml as follows.

XAML

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

 

Sample Link:

https://www.syncfusion.com/downloads/support/directtrac/general/ze/LoadPDFFromURL-1384289831.zip

 

Note:

PdfViewer loads the PDF as soon as the InputFileStream property is set to a stream. In the above sample, the file may not be loaded immediately and this is due to the time taken for the stream to be fetched from the FTP server.

 

 

Conclusion

I hope you enjoyed learning how to load the Xamarin.Forms PdfViewer from FTP server.

You can refer to our Xamarin PDF Viewer feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Xamarin PDF Viewer example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments
Please  to leave a comment
Access denied
Access denied