Forum Xamarin.Forms

why cant show pdf in pdf.js in UWP?

omarantonio16omarantonio16 Member ✭✭
edited August 2018 in Xamarin.Forms

I've been using pdf.js and a weibiew render to show a pdf or word file in UWP; in Release and Debug mode if it works, but when I generate the .appxbundle and install the application, I get a damaged Access error when trying to move the file from the local storage to the assets folder of the project.

The code to save the pdf:

// create file in public folder
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync (filename, CreationCollisionOption.ReplaceExisting);

// write sring to created file
await FileIO.WriteBytesAsync (sampleFile, bytes);

// get asets folder
StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assetsFolder = await appInstalledFolder.GetFolderAsync ("Assets");

// move file from public folder to assets
await sampleFile.MoveAsync (assetsFolder, filename, NameCollisionOption.ReplaceExisting);
str = sampleFile.Name;

The code for the webview route:

Control.Navigate (new Uri (string.Format ("ms-appx-web: ///Assets/pdfjs/web/viewer.html? File = {0}", string.Format ("ms-appx-web: / // Assets / {0} ", WebUtility.UrlEncode (customWebView.Uri)))));

As I mentioned before, the pdf is seen in release mode and in debugu mode, but when I install the package application it marks me the error of 'access denied'; In the capabilities of my application I have everything that has to do with storage installed in my app. I would like to know how to solve this error.

Best Answers


  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    edited August 2018


    When debugging, the app runs from the bin folder in the project directory and there is an Assets folder there which is readable and writeable. When building for distribution and sideloading, the Assets folder is in the install directory, Program Files\WindowsApps\<PackageGUID>, which is read only See:

    The last line in the "Application install directory" section:

    The app's install directory is a read-only location.

    So you need to save the file somewhere else.

  • omarantonio16omarantonio16 Member ✭✭

    If, in fact first put it in a folder as if it can read and write, and then I move it to the folder of Assets that is the only way in which you can see the PDFs.

    I have tried to show the PDF without moving it to the Assets folder, but it is not charged at PDF.js.

    Do you know the correct way to display the PDF using pdf.js by storing it in a folder that is not in the package, or know any way to display it without using the application by default for the file (with PDF.js I can show pdf and Word documents in some way? (el componente renderiza los archivos word)?

    Please, I need your help, I have already implemented the viewfinder on IOS and Android, uwp is the only platform where not I've been able to implement it, I already posted on a forum for Microsoft and there was no response. Already walked all the posts and no response I worked.

    Thank you in advance.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai
    Accepted Answer


    Sorry, I am not familiar with pdf.js, but I wonder why you need the PDF in the assets folder? I would think you could load it from any location, even the public folder, just by changing the URI, but again I am not familiar with pdf.js so I cannot be certain.

    There are other ways to display a pdf in UWP. Check out this blog post:

  • omarantonio16omarantonio16 Member ✭✭

    Hi @JGoldberger i need the PDF in Assets because its the only place works, im trying to show from another place but not works

  • omarantonio16omarantonio16 Member ✭✭
    edited August 2018


  • omarantonio16omarantonio16 Member ✭✭

    Thank you for having guided me on the right path, since the solution that you had passed me from native implementation, I could implement it using a customrender for stacklayout.
    it works in the following way: in the portable project I have the calse extended to a stacklayout, and in uwp I have the renderer, using all the implementation that you passed me I use that class adding a constructor to pass the path of my PDF, and in my renderer I created a StackPanel and that StackPanel I added as a child the class that contains the controls of the example, and with SetNativeControl I assigned it the stacklayout, impressive, thanks again, I had almost a month with this. : D

Sign In or Register to comment.