Coder Social home page Coder Social logo

Comments (24)

jfversluis avatar jfversluis commented on September 15, 2024 1

Make sure the build action is set to AndroidAsset

screenshot 2019-01-10 15 28 07

Then, you will have to modify the code a bit. I have done this in the pdfjsPage.xaml.cs:

if (Device.RuntimePlatform == Device.Android)
{
    //var dependency = DependencyService.Get<ILocalFileProvider>();

    //if (dependency == null)
    //{
    //    DisplayAlert("Error loading PDF", "Computer says no", "OK");

    //    return;
    //}

   // var fileName = Guid.NewGuid().ToString();
    //// Download PDF locally for viewing
    //using (var httpClient = new HttpClient())
    //{
    //    var pdfStream = Task.Run(() => httpClient.GetStreamAsync(url)).Result;

    //    localPath =
    //        Task.Run(() => dependency.SaveFileToDisk(pdfStream, $"{fileName}.pdf")).Result;
    //}

    localPath = "file:///android_asset/pdffile.pdf";

    //if (string.IsNullOrWhiteSpace(localPath))
    //{
    //   DisplayAlert("Error loading PDF", "Computer says no", "OK");

    //    return;
    //}
}

Note the commented out piece and how I set the localPath variable to a local file that is in the assets folder directly.

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024 1

You will probably have to add it to the resources there, but you will need to extend the https://github.com/jfversluis/pdfjs/blob/master/iOS/PlatformSpecifics/LocalFileProvider.cs class to give you the right local file path.

Add the file to your iOS project and set the build action to EmbeddedResource

screenshot 2019-01-10 20 02 31

Then expand the IFileProvider like this

using System.IO;
using System.Threading.Tasks;

namespace pdfjs.Interfaces
{
    public interface ILocalFileProvider
    {
        Task<string> SaveFileToDisk(Stream stream, string fileName);

        string GetLocalFilePath();
    }
}

And implement it on iOS (LocalFileProvider.cs), like this:

public string GetLocalFilePath()
{
    return NSBundle.MainBundle.PathForResource("pdffile", "pdf");
}

Of course, you might want to implement some stuff to not hardcode the filename and extension here, but I trust you can do that yourself ;)

Now just assign this as the URL in pdfjsPage.xaml.cs:

PdfView.Source = dependency.GetLocalFilePath();

If you optimize some stuff then you will be able to use the same code for Android and iOS

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024 1

Leaving this open so people might benefit from it in the future :)

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Hi Bernard,

Thank you for reading my blog! I'd love to help out, but I'm not sure what you mean by "read local files only" can you please elaborate a bit more?

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

Ty for the attention Gerald!!
So, i'm making a school project and i want my app to load only local file PDF's.
I'm not being successful on finding what i need to change. I tried to add my file into assets folder and than calling it replacing the URL you've used. (sorry for the bad explanation, but um new on programing)

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Do you get any errors? Or what is happening?

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

Not getting any errors! I just don't know what i need to do to make the code recognize where is my local pdf file, and than rendering it, instead of rendering the url you provided on the MainPage (app.xaml.cs)

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Did you see the addition I just made on the blog post? Under Update Jan 2019?

You might need a custom renderer now

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

Yes!! I'm using the updated version you've posted here. My issue is just finding on your code, or where i need to add on your code, a function that renders a pdf from my APP storage (offline), and not a url link. I'm trying to make a simple pdf reader for me, and i want the app to access the pdf's from the app itself and not the internet, but i don't know how. I tried adding my file into Droid assets directory but i'm not doing it right

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Do you have the code somewhere I can have a look at it?

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

https://uploaddeimagens.com.br/imagens/meajuda-png I'm still testing using only your code

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

It worked perfectly on Android Gerald!!! Ty so much for your help, it was way easier than i thought!! If i wan't to do the same thing with IoS, i just need to create a device.runtime for ios ?

from pdfjs.

bernardojb avatar bernardojb commented on September 15, 2024

Hey Gerald, once again, thank you so much for your time!!
I have 2 questions, the first one is about NSBundle.

  1. Do i need to add "using Foundation;" to have access to this function?
  2. When assigning the URL in pdfjsPage.xaml.cs, do i have to create a new "...(Device.RuntimePlatform == Device.iOS)" or i can add it on the else:

// if (Device.RuntimePlatform == Device.Android)
// PdfView.Source = $"file:///android_asset/pdfjs/web/viewer.html?file=
// {WebUtility.UrlEncode(localPath)}";
else
PdfView.Source = dependency.GetLocalFilePath();"

Because when i do that (example above), i get an error on "dependency".

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

The "solution" I gave was based on the questions you were asking. It's not a full solution, but it shouldn't be too hard to put it all together yourself.

Since you will not be retrieving the file from the local app in both cases, you might be able to remove the check for a certain platform altogether. It could be as simple as implementing the GetLocalFilePath method also for Android and simply returning the static string "file:///android_asset/pdffile.pdf".

The code to set the source for the PDF would then simply be:

var dependency = DependencyService.Get<ILocalFileProvider>();
PdfView.Source = dependency.GetLocalFilePath();

Of course, if it should be a bit more dynamic, probably in the filename area, you might want to implement some more logic for it.

For your question about using Foundation;; I'm guessing NSBundle resides in that namespace, so yeah you should include it or simply type return Foundation.NSBundle.MainBundle.PathForResource("pdffile", "pdf");

from pdfjs.

carlosagudop avatar carlosagudop commented on September 15, 2024

Hi, and thank you for your work. I have a lot of questions about how to read local files, although i have read the entire issue.

The first question is about the "url" the parameter of the "pdfjsPage.xaml.cs", that url from a local file will be only the name of the file and the extension or i have to give the entire route? and in that case how i know the entire route?

My second question is about what i have to do inside that "if" "(Device.RuntimePlatform == Device.Android)" in "pdfjsPage.xaml.cs" if i give it the localpath, the url what is for? and this code

 if (Device.RuntimePlatform == Device.Android)
                PdfView.Source = $"file:///android_asset/pdfjs/web/viewer.html?file={WebUtility.UrlEncode(localPath)}";
            else
                PdfView.Source = url;

is even necesary?

Im sorry for my english i tried to explain myself the best as possible. I am and student and its my first aproximattion at c# and xamarin, so im very lost. 😅

Thank you so much!!

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Hey @carlosagudop thanks for your message.

  • The url comes from here and is an online URL, because the file is downloaded first for iOS: https://github.com/jfversluis/pdfjs/blob/master/pdfjs/App.xaml.cs#L11

  • SO as I mentioned on iOS I download the PDF file first to a local path and then show it. For Android you can take the online url directly and show that. That is why that difference is there. Maybe things have changed and this isn’t necessary anymore, but at the time it was.

I hope that helps you a bit further!

from pdfjs.

carlosagudop avatar carlosagudop commented on September 15, 2024

Ty for your answer!!

So, if i have the pdf already dowloaded in the assets folder in android, the url parameter is no longer neccesary, isn´t it? Im just trying to show in the webvier the local pdf,

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Correct. If you have the file locally already, just figure out the path where it is stored and supply that to the PDF viewer

from pdfjs.

carlosagudop avatar carlosagudop commented on September 15, 2024

And how i know the path of the pdf file? the file is inside the assets folder in android.

Thank you very much for your time!

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

If it’s just a static file sitting there then it will probably be something like "file:///android_asset/pdffile.pdf" else, you probably want to have a look at this concept: https://github.com/jfversluis/pdfjs/blob/master/Droid/PlatformSpecifics/LocalFileProvider.cs

from pdfjs.

carlosagudop avatar carlosagudop commented on September 15, 2024

Where do i use the LocalFileProvider? This is my constructor of the content page

public LocalWB(String url)
        {
            InitializeComponent();
            PdfView.Source = url;
       
        }

and this is the event of a button where i open the contentpage
var localroute = "file:///android_asset/sample.pdf"; Application.Current.MainPage.Navigation.PushModalAsync(new LocalWB(localroute));

But i only get a blankpage 😫

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Can you share the code somewhere?

from pdfjs.

MickLesk avatar MickLesk commented on September 15, 2024

Hey @jfversluis,

im using pdf.js with Xamarin Forms, the PDF work correctly, but i dont have "touch-events" for example, pinch to zoom, or double tap. Do you have any idea?

from pdfjs.

jfversluis avatar jfversluis commented on September 15, 2024

Could you please open a separate issue and post some relevant code? Also is it iOS or Android? Both?

from pdfjs.

Related Issues (6)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.