locked
Open pdf files in reader

    Question

  • I have a educational app, it contains a fair few pdfs that are contained with a <a> links currently

    <a href="/assets/mypdf.pdf">Open PDF</a>

    I have read so many articles on how to view pdf's but none of them seem to work.

    The only minor success I had opened the pdf file as soon as you landed on the page, using this script:

    // Path to the file in the app package to launch
    var imageFile = "assets\\mypdf.pdf";
    
    // Get the image file from the package's image directory
    Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(
      function (file) {
        // Set the show picker option
        var options = new Windows.System.LauncherOptions();
        options.displayApplicationPicker = true;
    
        // Launch the retrieved file using the selected app
        Windows.System.Launcher.launchFileAsync(file, options).then(
          function (success) {
            if (success) {
                // File launched
            } else {
                // File launch failed
            }
          });
      });

    Currently all I get when I click the pdf link is a javascript error "Not supported", and the script above only allows for 1 file, not multiple. I just can't find any clear tutorials on how this simple task is accomplished.

    I thought this would be really simple, but really struggling on this one! So any help would be massively appreciated.

    Thanks!

    Friday, November 22, 2013 11:42 AM

Answers

  • Hi matrix_digital,

    We can only use launchFileAsync() to start the app associated with the specified file, that's the reason why your <a> is not function.

    Try the following code and you could see that the 1.pdf opened in the Reader App and then switch to 2.pdf, this is because in windows store app only one app instance is allowed, and let's back to your question, to open multiple pdfs in Reader app is not possible, if you have other desktop reader installed, might be a possible.  

            var fileToLaunch1 = "assets\\1.pdf";
            var fileToLaunch2 = "assets\\2.pdf";
    
            function launchFile() {
    
                for (i = 0; i < 2; i++) {
                    var fileToLaunch;
                    if (i == 0) fileToLaunch = fileToLaunch1;
                    if (i == 1) fileToLaunch = fileToLaunch2;
                    Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(fileToLaunch).done(
                                 function (file) {
                                     Windows.System.Launcher.launchFileAsync(file).done(
                                         function (success) {
                                             if (success) {
                                                 WinJS.log && WinJS.log("File " + file.name
                    + " launched.", "sample", "status");
                                             } else {
                                                 WinJS.log && WinJS.log("File launch failed.", "sample", "error");
                                             }
                                         });
                                 });
                }
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, November 25, 2013 8:53 AM
    Moderator

All replies

  • Ok I have gotten a little further:

    (function () {
         "use strict";
    
         WinJS.UI.Pages.define("/pages/page/page.html", {
    
             ready: function (element, options) {
                 document.getElementById("file").addEventListener("click",
    launchFile, false);
    
             }
         });
    
         var fileToLaunch = "assets\\mypdf.pdf";
    
         function launchFile() {
    
    Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(fileToLaunch).done(
                 function (file) {
                     Windows.System.Launcher.launchFileAsync(file).done(
                         function (success) {
                             if (success) {
                                 WinJS.log && WinJS.log("File " + file.name
    + " launched.", "sample", "status");
                             } else {
                                 WinJS.log && WinJS.log("File launch
    failed.", "sample", "error");
                             }
                         });
                 });
         }

    This code works perfect now except I have multiple pdfs and cant figure out how to allow all pdf files to open this way and not just the specified one in the filetolaunch variable

    Friday, November 22, 2013 11:56 AM
  • Hi matrix_digital,

    We can only use launchFileAsync() to start the app associated with the specified file, that's the reason why your <a> is not function.

    Try the following code and you could see that the 1.pdf opened in the Reader App and then switch to 2.pdf, this is because in windows store app only one app instance is allowed, and let's back to your question, to open multiple pdfs in Reader app is not possible, if you have other desktop reader installed, might be a possible.  

            var fileToLaunch1 = "assets\\1.pdf";
            var fileToLaunch2 = "assets\\2.pdf";
    
            function launchFile() {
    
                for (i = 0; i < 2; i++) {
                    var fileToLaunch;
                    if (i == 0) fileToLaunch = fileToLaunch1;
                    if (i == 1) fileToLaunch = fileToLaunch2;
                    Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(fileToLaunch).done(
                                 function (file) {
                                     Windows.System.Launcher.launchFileAsync(file).done(
                                         function (success) {
                                             if (success) {
                                                 WinJS.log && WinJS.log("File " + file.name
                    + " launched.", "sample", "status");
                                             } else {
                                                 WinJS.log && WinJS.log("File launch failed.", "sample", "error");
                                             }
                                         });
                                 });
                }
            }

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, November 25, 2013 8:53 AM
    Moderator