mohammad anouti
mohammad anouti

Reputation: 171

xamarin forms get pdf path

I've successfully read a pdf from the project and displayed in a WebView. What I want to do is to get the pdf file path from my project and check if it exists to display it, and if not, to give it another URI to open with. Anyone knows how I can get the path of the pdf and it works for both Android and iOS.

Here is my C# code:

 string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode("Conctionprofile.pdf"));
        public pdfviewer ()
        {
            InitializeComponent ();
            PDFReading();
        }
        private void PDFReading()
        {

                pdfwebview.Uri = internalstorageurl;

        }

XAML:

    <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:AppXamarin.CustomRenders"
             x:Class="AppXamarin.Pages.pdfviewer"
             Padding="0,20,0,0">
    <ContentPage.Content>
        <local:CustomWebView x:Name="pdfwebview" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
    </ContentPage.Content>
</ContentPage>

I'm checking if the file exist in my project with this code:

 [assembly: Dependency(typeof(GetPdfFilePath_Android))]
namespace AppXamarin.Droid.CustomRender
{
    public class GetPdfFilePath_Android : Java.Lang.Object, IGetPdfFilePath
    {
        public string filePath(string fileName)
        {      
                string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(fileName));
            if (File.Exists(internalstorageurl))
                return internalstorageurl;
            else
                return "not found";
        }
    }
}

The if statement is always false

Upvotes: 0

Views: 757

Answers (1)

nevermore
nevermore

Reputation: 15806

You can use dependencyService to get pdf file path from both iOS and Android app.

The interface:

public interface IGetPdfFilePath
{
    string filePath(string fileName);
}

Get the file path in Xamarin.forms:

string pdfPath = DependencyService.Get<IGetPdfFilePath>().filePath("myPdf.pdf");
                Console.WriteLine(pdfPath);

In iOS, the path is depending on where you store the file:

[assembly: Dependency (typeof (GetPdfFilePath_iOS))]

namespace UsingDependencyService.iOS
{
    public class GetPdfFilePath_iOS : IGetPdfFilePath
    {
        public GetPdfFilePath_iOS ()
        {
        }

        public string filePath(string fileName)
        {
            // 1. if your store your pdf in DocumentDirectory
            //string directories = NSSearchPath.GetDirectories(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomain.All)[0];
            //string pathBundle = directories + "/" + fileName;
            ////bool fileExists = NSFileManager.DefaultManager.FileExists(pathBundle);
            //return pathBundle;

            //2.pdf file in your project
            string pathBundle = Path.Combine(NSBundle.MainBundle.ResourcePath, fileName);

            return pathBundle;
        }
    }
}

In Android:

[assembly: Dependency(typeof(GetPdfFilePath_Android))]
namespace UsingDependencyService.Android
{
    public class GetPdfFilePath_Android : Java.Lang.Object, IGetPdfFilePath
    {

        public string filePath(string fileName)
        {

            string internalstorageurl = string.Format("file:///android_asset/Content/{0}", WebUtility.UrlEncode(fileName));

            return internalstorageurl;

        }
    }
}

Update:

Add this line: [assembly: Dependency(typeof(GetPdfFilePath_Android))] to your Android class. GetPdfFilePath_Android here is your class name.

Another way is to create a custom renderer of your webview, you can have a look at this project.

Update to check file:, I add this method in the Android to check if the file exist inside Assets/Content.

public bool fileExist (string fileName) {

            AssetManager assets = MainActivity.Instance.Assets;        
            string[] names = assets.List("Content");

            for (int i = 0; i < names.Length; i++)
            {
                if (names[i] == fileName)
                {
                    return true;
                }
            }

            return false;
        }

Note: You have to change the build action of pdf file to AndroidAsset if you can't find the file inside Assets.

Upvotes: 1

Related Questions