0

我正在研究 Xamarin.Forms-UWP。我想将存储在数据库中的字节数组转换为 Windows 手机的 pdf。我知道如何转换 var base64Binarystr = "ABCDS" byte[] bytes = Convert.FromBase64String(base64Binarystr);

任何人都可以帮助如何显示pdf?只是一个指针 - 我有多个 pdf 文件,因此我无法将所有文件添加到应用程序或存储在磁盘上。

感谢对此的任何指示。谢谢!

4

1 回答 1

2

每个收到的文件都可以用相同的名称存储(我使用“my.pdf”),那么存储太多文件就没有风险了。如果您需要缓存文件,则可以提供不同的名称。尽管我尝试了 ms-appdata,但 pdf 查看器不想为我显示本地、临时或下载文件夹中的文件,所以我不得不将文件从本地文件夹移动到资产以显示查看器通过 ms-“想要”它的方式应用程序网络。下载文件夹也有 CreationCollisionOption.ReplaceExisting 的问题,如果文件已经存在而不是替换它,它会显示无效参数,但本地和临时文件夹行为正确。

        /////////////// store pdf file from internet, move it to Assets folder and display ////////////////////
        //bytes received from Internet. Simulated that by reading existing file from Assets folder
        var pdfBytes = File.ReadAllBytes(@"Assets\Content\samplepdf.pdf");
        try
        {
            StorageFolder storageFolder = ApplicationData.Current.LocalFolder; //or  ApplicationData.Current.TemporaryFolder
            StorageFile pdfFile = await storageFolder.CreateFileAsync("my.pdf", CreationCollisionOption.ReplaceExisting);
            //write data to created file
            await FileIO.WriteBytesAsync(pdfFile, pdfBytes);
            //get asets folder
            StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
            StorageFolder assetsFolder = await appInstalledFolder.GetFolderAsync("Assets");
            //move file from local folder to assets
            await pdfFile.MoveAsync(assetsFolder, "my.pdf", NameCollisionOption.ReplaceExisting);
        }
        catch (Exception ex)
        {
        }
        Control.Source = new Uri(string.Format("ms-appx-web:///Assets/pdfjs/web/viewer.html?file={0}", "ms-appx-web:///Assets/my.pdf")); //local pdf
于 2017-06-23T22:16:43.323 回答