2014-10-30 241 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