Download, save( locally ) and display PDF from a link

10,397

You can download the PDF file and save it in Isolated Storage, to be able to view later offline using a PDF viewer app such Adobe Reader or PDF Reader.

So lets see how to do it step-by-step.

1- Download PDF file from a link( URL ) provided by server side:

WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("http://url-to-your-pdf-file.pdf"));

2- Save the downloaded PDF file in local 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);
        }
    }
}

3- Open and display PDF file from local 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);
Share:
10,397
pg90
Author by

pg90

Updated on June 04, 2022

Comments

  • pg90
    pg90 about 2 years

    I am developing Windows phone 8 application. In my application, i have to display PDF file in offline( without net connection ) mode, within application. For that i have to do the following,

    1. Download PDF file from a link( URL ) provided by server side.
    2. Save the downloaded PDF file in local storage.
    3. Open and display PDF file from local storage.

    On searching, i found suggestions to use ComponentOne Studio's toolset called 'Studio for Windows Phone'. Unfortunately it is not free. Is there any way to implement in free of cost?

    Any reference, samples or ideas will be greatly appreciated.

  • pg90
    pg90 almost 11 years
    Thanks anderZubi, That works correctly. But the third step, launches the launcher to open PDF. But my requirement is to display PDF within the application ie., in XAML. Is there any hope..?
  • anderZubi
    anderZubi almost 11 years
    Sorry, I don't know about any free pdf libraries, but have a look at this and this. They may be helpful.
  • souvickcse
    souvickcse over 10 years
    Thanx for this awesome answer :)
  • Nitesh Kothari
    Nitesh Kothari over 9 years
    @anderZubi hello brother, its a supper code, but I am getting this error. "FILE-NAME can't be opened".
  • anderZubi
    anderZubi over 9 years
    @NiteshKothari do you get that error with any pdf file? Have you tried with other files?
  • Nitesh Kothari
    Nitesh Kothari over 9 years
    @anderZubi I also tried with, excel files and .docx files, but getting this result, "Doesn't recognise the file format" and "file format has been damaged". Thank you!!
  • Nitesh Kothari
    Nitesh Kothari over 9 years
    @anderZubi my basic concept is, there are documents on the server and there is a URI, and I have to open those documents.
  • Nitesh Kothari
    Nitesh Kothari over 9 years
    @anderZubi hello, are you there??please reply if you are there