从链接下载、保存(本地)和显示 PDF [英] Download, save( locally ) and display PDF from a link

查看:21
本文介绍了从链接下载、保存(本地)和显示 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Windows 手机 8 应用程序.在我的应用程序中,我必须在应用程序中以离线(无网络连接)模式显示 PDF 文件.为此,我必须执行以下操作,

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. 从服务器端提供的链接( URL )下载 PDF 文件.
  2. 将下载的 PDF 文件保存在本地存储中.
  3. 打开并显示本地存储中的 PDF 文件.

在搜索过程中,我找到了使用 ComponentOne Studio 的工具集Studio for Windows Phone"的建议.不幸的是,它不是免费的.有什么办法可以免费实施吗?

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.

推荐答案

您可以下载 PDF 文件并将其保存在独立存储中,以便以后可以使用 PDF 查看器应用程序(例如 Adob​​e Reader 或 PDF Reader)离线查看.

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- 从服务器端提供的链接(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);

这篇关于从链接下载、保存(本地)和显示 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆