question

GenerationContinuum-5900 avatar image
0 Votes"
GenerationContinuum-5900 asked ·

How do I allow a user to open a PDF stored in my UWP App?

I'm developing a UWP app and I want the user to be able to open a PDF stored in my app in Reader or a browser on the user's desktop.

I first used the following code-behind from an app bar button click:

             string filename1 = "ModelHouseAppStencils.pdf";
             System.Diagnostics.Process.Start(filename1);

I got the following exception:

System.ComponentModel.Win32Exception: 'The system cannot find the file specified'

Then I used the code:

StorageFile file1 = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/ModelHouseAppStencils.pdf"));

I then got the following exception:

Exception thrown: 'System.IO.FileNotFoundException' in System.Private.CoreLib.dll

What do I do to solve this? Thanks.

windows-uwpwindows-uwp-xaml
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

GenerationContinuum-5900 avatar image
0 Votes"
GenerationContinuum-5900 answered ·

The full path of my PDF file isn't exactly where you specified it should be. It's in this full path:

<yourproject><yourproject>\bin\<architecture>\Debug\AppX\Assets\

How do I get rid of the top folder in this path or redirect my project to look for the PDF file in the current full path?

GenerationContinuum5900

· 1 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Is now working. Installing the Application Model package really did it. Thanks!!

0 Votes 0 · ·
danielescipioni avatar image
0 Votes"
danielescipioni answered ·

At first check the properties of ModelHouseAppStencils.pdf: set "Copy to Output Directory" to "Copy if newer" or "Copy always" or set "Build Action" to "Content"

Then

StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(System.IO.Path.Combine("Assets", "ModelHouseAppStencils.pdf"));
await Windows.System.Launcher.LaunchFileAsync(file);
· 4 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks. I'll try that. I failed to mention that I'm using a view model in my app. I set the data context on the xaml main page to an instance of the view model class. That may be why I'm getting a file not found message because I was using code-behind to open the PDF. I think I'll put this code in the view model and see if it works then.

0 Votes 0 · ·
danielescipioni avatar image danielescipioni GenerationContinuum-5900 ·

Page code-behind or view model code should be the same, it seems to me that the pdf is not copied as content of you app.
Take a look at the properties of your pdf file in visual studio: select the file and press F4, you should see a property view similar to this:

7634-propertiview.png

Look at the "Build Action" and "Copy to Output Directory" properties and compare the values with the ones in my first comment:

set "Copy to Output Directory" to "Copy if newer" or "Copy always"

or

set "Build Action" to "Content"

To see if pdf file is correctly copied go in "<yourproject>\bin\<architecture>\Debug\AppX\Assets\" and check.









0 Votes 0 · ·
propertiview.png (8.3 KiB)

I checked my PDF file and both settings you mentioned had to be changed. I'll try to run it now. Thanks.

0 Votes 0 · ·
Show more comments