2014-10-30 236 views
0

我正在为Windows 8.1的Windows应用商店应用程序工作,但找不到任何解决我需要的操作。我正在尝试将PDF页面的图像保存到本地应用程序数据存储中。我有以下:将PDF保存为文件

private async void GetFirstPage() 
    { 
     // Retrieve file from FutureAccessList 
     StorageFile file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(token); 

     // Truncate last 4 chars; ex: '.pdf' 
     FileName = file.Name.Substring(0, file.Name.Length - 4); 

     // Get access to pdf functionality from file 
     PdfDocument doc = await PdfDocument.LoadFromFileAsync(file); 

     // Get a copy of the first page 
     PdfPage page = doc.GetPage(0); 

     // Below could be used to tweak render options for optimizations later 
     //PdfPageRenderOptions options = new PdfPageRenderOptions(); 

     // Render the first page to the BitmapImage 
     InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); 
     await page.RenderToStreamAsync(stream); 

     // Common code 
     Cover.SetSource(stream); 

     // Convert the active BitmapImage Cover to a storage file to be stored locally 
     // ??? 

    } 

可变,盖,是在XAML绑定到显示图像给用户的的BitmapImage。我想将此图像保存到我的本地应用程序数据中,以便每次打开应用程序时都不必通过PDF库重新渲染它!问题是我不能从流中保存任何Windows应用程序应用程序,除非它是我所知的文本(对我来说没有文件流或文件输出流),并且Cover的URI源是空的,因为它来自流,使得很难将我的BitmapImage,Cover,保存到StorageFile中,以便正确保存Windows Store。

目前一切都适用于我,每当我的应用程序打开时,我都感到沮丧,从头开始重新将pdf页面呈现给我的bitmapimage。预先感谢您的任何意见!

回答

0

您需要直接从PdfPage保存图像,而不是从BitmapImage保存图像,因为无法从BitmapImage获取数据。 PdfPage.RenderToStreamAsync已经将流编码为位图,因此除了将其发送到文件之外,您不需要执行任何操作。这与保存到InMemoryRandomAccessStream中的基本相同,除了基于文件流:

//We need a StorageFile to save into. 
StorageFile saveFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("safedPdf.png",CreationCollisionOption.GenerateUniqueName); 

using (var saveStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) 
{ 
    await page.RenderToStreamAsync(saveStream); 
} 
+0

这就像一个魅力,它现在真的有道理!我一定是密集的。<非常感谢你:) – Proto 2014-11-02 19:16:00