【发布时间】:2015-06-02 06:38:38
【问题描述】:
在 WinRt 应用程序中,我想显示 pdf 内容。因此,我将所有 pdf 页面转换为图像。但是质量很差。同样放大后,它变得更加糟糕。
我怎样才能提高质量!?可以使用编码器吗?还是我需要更高的宽度/高度值?
StorageFile pdfFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
//Load Pdf File
pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile);
if (pdfDocument != null && pdfDocument.PageCount > 0) {
//Get Pdf page
for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++) {
var pdfPage = pdfDocument.GetPage((uint)pageIndex);
if (pdfPage != null) {
// next, generate a bitmap of the page
StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
StorageFile pngFile = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".png", CreationCollisionOption.ReplaceExisting);
if (pngFile != null) {
IRandomAccessStream randomStream = await pngFile.OpenAsync(FileAccessMode.ReadWrite);
PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
pdfPageRenderOptions.IsIgnoringHighContrast = false;
PdfPageDimensions rotation = pdfPage.Dimensions;
Size pdfPageSize = pdfPage.Size;
if (pdfPageSize.Height > 1000) {
pdfPageRenderOptions.DestinationHeight = (uint)(pdfPageSize.Height * 0.85);
}
else if (pdfPageSize.Width > 1000) {
pdfPageRenderOptions.DestinationWidth = (uint)(pdfPageSize.Width * 1.25);
}
await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
await randomStream.FlushAsync();
randomStream.Dispose();
pdfPage.Dispose();
PdfPagePath = pngFile.Path;
PdfPagesPathCollection.Add(pngFile.Path);
}
}
}
}
【问题讨论】:
-
为什么要将它们转换为图像? Pdf 是一种可缩放的矢量格式。
-
我需要使用它,在 winrt 应用程序中显示它们并允许缩放。如果我保持 pdf 格式,直接在应用程序中处理和显示会更加困难。
-
通过将其转换为高分辨率位图,您的应用程序可能会消耗大量内存,这可能会导致应用程序终止。
-
@GermanSniper 我正在尝试一些非常相似的东西。我想用
ScrollViewer包装Image,当ViewChanged事件发生时,我会用新的DestinationWidth调用RenderToStreamAsync,但是,性能真的很差……@WiredPrairie 是对的,高分辨率位图正在消耗大量内存... -
您现在找到更好的解决方案了吗?
标签: c# wpf pdf windows-runtime winrt-xaml