Oscar Fernandez
Oscar Fernandez

Reputation: 144

How to open pdf file in Windows Phone 8?

I have some pdf files inside (the project files) my app and I wanted how to open in Adobe Reader or the other, but I don't know how.

In iOS is more easy and in Android I know how, but I don't know how in WP8.

I'm new in Windows Phone 8 :/

Thanks for all!

Upvotes: 5

Views: 12782

Answers (3)

senimii
senimii

Reputation: 247

Save the downloaded file to the isolated storage...

async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
    byte[] buffer = new byte[e.Result.Length];
    await e.Result.ReadAsync(buffer, 0, buffer.Length);

    using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication())
    {
        using (IsolatedStorageFileStream stream = storageFile.OpenFile("your-file.pdf", FileMode.Create))
        {
            await stream.WriteAsync(buffer, 0, buffer.Length);
        }
    }
}

Open and display the pdf file from the isolated storage..

// Access the file.
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile pdffile = await local.GetFileAsync("your-file.pdf");

// Launch the pdf file.
Windows.System.Launcher.LaunchFileAsync(pdffile);

Upvotes: 2

iC7Zi
iC7Zi

Reputation: 1658

async void launchPDF()
{
 string fileURL = @"Assets\file.pdf";
 StorageFile pdfFile = await 
 Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(fileURL);
 if (pdfFile != null)
 {
   IAsyncOperation<bool> success = 
           Windows.System.Launcher.LaunchFileAsync(pdfFile);

   if (await success)
   {
     // File launched
   }
   else
   {
     // File launch failed
   }
 }
 else
 {

 }
}

Make sure pdf file build action is content

Upvotes: 0

anderZubi
anderZubi

Reputation: 6424

You have to use the LaunchFileAsync method of Launcher class. Example:

// Access the file.
StorageFile pdfFile = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("file.pdf");

// Launch the pdf file.
Windows.System.Launcher.LaunchFileAsync(pdfFile);

You will find more info here:

Auto-launching apps using file and URI associations for Windows Phone 8

Upvotes: 6

Related Questions