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

How to open file from external storage and load to pdfreader?

I tried this way but got nothing in return

 var directoryPath = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
            using (FileStream stream = new FileStream(directoryPath.AbsolutePath + "/" + "test.pdf",FileMode.Open,FileAccess.Read) )
            {
                pdfViewer.LoadDocument(stream);
            }

5 Replies

SS Sathish Sivakumar Syncfusion Team September 12, 2017 07:26 AM UTC

Hi Ishwor, 

Thank you for using Syncfusion products. 

We can read the PDF document from External Storage and display it into SfPdfViewer in Xamarin.Android. The following code snippet illustrates how to open the PDF document from external storage and read it as a Stream. 

private Stream ReadPdfStreamFromExternalStorage() 
{ 
//Get the path of external storage directory. Here we used download directory to read PDF document 
String path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath; 
//Read the specific PDF document from the download directory 
Java.IO.File filePath = new Java.IO.File(path+"/test.pdf"); 
// Check whether the file is exist in the download directory 
if (filePath.Exists()) 
{ 
// Convert the file path to stream to display it into SfPdfViewer 
return new FileStream(filePath.AbsolutePath, FileMode.Open, FileAccess.Read); 
} 
else 
{ 
// throw exception if the file is not found in the appropriate directory 
throw new FileNotFoundException("File not found"+ filePath.AbsolutePath.ToString()); 
} 
} 

We have also attached the sample in the below link for your reference. 


Note: Kindly ensure the PDF document that you want display in SfPdfViewer is available in the appropriate external storage directory. 

Please let us know if you need any other assistance. 

Regards, 
Sathish 



MI Mouhcine Ifouls March 16, 2020 10:35 PM UTC

How I can get a list of files type pdf 


JP Jhansi Priya Ramesh Syncfusion Team March 17, 2020 06:09 PM UTC

Hi Mouhcine,  
   
 
Could you please share details on your requirement to get the list of PDF’s so that, it will help us to assist you with a suitable solution . 
    
 
Regards,    
Jhansi Priya Ramesh  



MI Mouhcine Ifouls March 17, 2020 06:57 PM UTC

in this solution only recover a file and I would like to recover a list of pdf files

private Stream ReadPdfStreamFromExternalStorage() 
{ 
//Get the path of external storage directory. Here we used download directory to read PDF document 
String path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath; 
//Read the specific PDF document from the download directory 
Java.IO.File filePath = new Java.IO.File(path+"/test.pdf"); 
// Check whether the file is exist in the download directory 
if (filePath.Exists()) 
{ 
// Convert the file path to stream to display it into SfPdfViewer 
return new FileStream(filePath.AbsolutePath, FileMode.Open, FileAccess.Read); 
} 
else 
{ 
// throw exception if the file is not found in the appropriate directory 
throw new FileNotFoundException("File not found"+ filePath.AbsolutePath.ToString()); 
} 
} 


Urgent.


JP Jhansi Priya Ramesh Syncfusion Team March 18, 2020 12:37 PM UTC

Hi Mouhcine ,   
  
Based on your update, we assume that you require to get all the PDF file names located in the given folder. The following code snippet illustrates how to get the list of PDF files.   
  
private List<string> GetPDFFiles(string path)   
{   
   
string file;   
List<string> Pdffilelist = new List<string>();   
//Checks whether the given directory exists.   
if (Directory.Exists(path))   
{   
//Gets the list of PDF files located in the directory.   
var pdfFiles = Directory.EnumerateFiles(path, "*.pdf", SearchOption.AllDirectories);   
   
foreach (string currentFile in pdfFiles)   
{   
file = currentFile;   
//Adds the file name to the list.   
Pdffilelist.Add(file);   
}   
   
}   
//Returns the list of file names.   
return Pdffilelist;   
}   
   
  
   
We have also attached the sample for your reference and please find the sample from the below link.   
  
  
Kindly try the above suggestion and please let us know if any further assistance required.   

Regards, 
Jhansi Priya Ramesh 


Loader.
Live Chat Icon For mobile
Up arrow icon