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: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to load PDF from file system?

This document illustrates loading a PDF document from the file system of a device into the PDF viewer control.

PCL/.NET Standard project

Add an interface with name IFileHelperService to the project using two method definitions to read the PDF from the file system. The first method is for iOS and Android, and the second one is for UWP that reads the PDF asynchronously.  

C#

 

Add a ViewModel class with name ‘PdfViewerViewModel’ to the project and the class must implement the INotifyPropertyChanged interface. Define a property of type MemoryStream with name PdfStream. When this property is set, raise the PropertyChanged event with the property name as argument.

 

In the constructor, read the PDF into a MemoryStream using DependencyService and set it to the PdfStream property.

C#

 

Set the BindingContext of the MainPage of the application to the PdfViewerViewModel class. Bind the InputFileStream property of PdfViewer to the PdfStream property.

XAML

 

Android project

 

Implement the interface IFileHelperService in the Android project. It is sufficient to implement the body of the synchronous method ‘GetFileStream’.  The asynchronous method is only for UWP and is not used in Android.

 

Here, a PDF with name GIT_Succinctly.pdf stored in the Android device’s external storage is read into a MemoryStream.

C#

 

iOS project

 

Implement the interface IFileHelperService in the iOS project. It is sufficient to implement the body of the synchronous method ‘GetFileStream’.  The asynchronous method is only for UWP and is not used in iOS.

 

Here, a PDF with name iOS_Succinctly.pdf stored in the application’s personal folder is read into a MemoryStream.

C#

 

UWP project

 

Implement the interface IFileHelperService in the UWP project. It is sufficient to implement the body of the asynchronous method ‘GetFileStreamAsync’.  The synchronous method ‘GetFileStream’ is only for Android and iOS and is used in UWP.

 

Here, a PDF with name iOS_Succinctly.pdf stored in the application’s personal folder is read into a MemoryStream.

C#

 

Sample link:

http://www.syncfusion.com/downloads/support/directtrac/general/ze/LoadFromFileDemo1205006140

 

Note:

 

The PDF files and the file paths used in the code snippet as well as in the sample are only meant to be examples. Before running the sample or using the code in your own application, please ensure that the PDF files exist in the specified paths. The file name and path can also be changed to suit your needs.

The input PDF files can be placed in the local disks of Windows (desktop/mobile) device and the storage directories of Android device manually.

But in iOS, manually placed or downloaded files cannot be accessed by an application. For an application, the file must be in the sandbox of the application according to the documentation to access a file. 

 

https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html

 

If the PDF to be shown does not already exist in the application sandbox use the following procedure to save the PDF to a directory in the sandbox. The following code snippets are not included in the sample.

Add another method named ‘Save’ to the IFileHelperService interface.

C#

 

This new method will also be defined in Android and UWP platforms. But, the body of the method does not need to be implemented in these platforms as they are never called. Now, implement the method in the FileHelperService class of iOS project.

C#

 

Call this save method from the constructor of the MainPage using DependencyService.

In below code snippet the variable ‘inputStream’ is a Stream object of a PDF either downloaded from URL or read from the assets.

C#

 

After saving the PDF to a directory, the previous code snippets can be removed from the project.

Article ID: Published Date: Last Revised Date: Platform: Control:
8858 06/19/2018 07/05/2018 Xamarin.Forms PdfViewer
Did you find this information helpful?
Add Comment
You must log in to leave a comment

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