WinRt 将 PDF 转换为图像提高图像质量

WinRt convert PDF to image improve image quality

我想在 WinRt 应用程序中显示 pdf 内容。因此,我将所有 pdf 页面转换为图像。但是质量很差。同样放大后,它变得更糟了。

我怎样才能提高质量!?可以使用编码器吗?或者我需要更高的 Widht/Hight 值?

            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 格式允许无限完美缩放,因为它是基于矢量的格式。当您将页面转换为图像时,您会受到转换分辨率的限制,并且缩放会显示像素。

一个解决方案是以更高的分辨率和比例渲染到显示器,它会给你一些缩放范围,但它仍然会达到一个极限。在其他平台上,这是通过仅渲染要在所需缩放级别查看的部分来完成的,但我认为使用 PdfDocument.

这是不可能的