您可以下载 PDF 文件并将其保存在独立存储中,以便以后使用 PDF 查看器应用程序(如 Adobe Reader 或 PDF Reader)离线查看。
因此,让我们一步一步地看看如何做到这一点。
1-从服务器端提供的链接( URL )下载 PDF 文件:
WebClient client = new WebClient();
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri("http://url-to-your-pdf-file.pdf"));
2-将下载的PDF文件保存在本地存储中:
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- 从本地存储打开并显示 PDF 文件:
// 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);