image - 转换后的 PDF 图像在 UWP 中看起来很模糊

标签 image pdf uwp pdf-generation image-quality

我想使用 c#、xaml 将 pdf 文件转换为 UWP 中的图像 UI 控件。

我已经阅读了另一种使用 Flip Viewer 的方法,但我需要转换后的 PDF 文件的每个图像文件。

所以我修改了一些现有示例以打开 pdf 文件。

我的问题是转换后的图像文件的质量非常差。

我什至看不到字母。

但这个质量问题在其他pdf样本上也是一样的。

看看我的代码。


private PdfDocument pdfDocument;

private async void LoadDocument()
    {
        pdfDocument = null;

        //Output means my image UI control (pdf image will be added)
        Output.Source = null;

        //PageNumberBox shows current page
        PageNumberBox.Text = "1";

        var picker = new FileOpenPicker();
        picker.FileTypeFilter.Add(".pdf");
        StorageFile file = await picker.PickSingleFileAsync();

        if (file != null)
        {               
            try
            {
                pdfDocument = await PdfDocument.LoadFromFileAsync(file);
            }
            catch (Exception ex)
            {                   
            }

            if (pdfDocument != null)
            {  // I use this text to move page.
                PageCountText.Text = pdfDocument.PageCount.ToString();                    
            }                
        }            

        uint pageNumber;
        if (!uint.TryParse(PageNumberBox.Text, out pageNumber) || (pageNumber < 1) || (pageNumber > pdfDocument.PageCount))
        {
            return;
        }

        Output.Source = null;

        // Convert from 1-based page number to 0-based page index.            
        uint pageIndex = pageNumber-1 ;

        using (PdfPage page = pdfDocument.GetPage(pageIndex))
        {
            var stream = new InMemoryRandomAccessStream();

            await page.RenderToStreamAsync(stream);

            BitmapImage src = new BitmapImage();
            Output.Source = src;
            await src.SetSourceAsync(stream);
        }
    }

这是我的 xaml 代码。

<Grid>
    <ScrollViewer >            
                <TextBlock Name="ViewPageLabel"VerticalAlignment="center">Now page</TextBlock>                   
                <TextBox x:Name="PageNumberBox" InputScope="Number" Width="30" Text="1" TextAlignment="Right" Margin="5,0,5,0"
                        AutomationProperties.LabeledBy="{Binding ElementName=ViewPageLabel}"/>
                <TextBlock VerticalAlignment="Center">of <Run x:Name="PageCountText"/>.</TextBlock>
    </ScrollViewer>
</Grid>

有什么建议吗?

请帮帮我。

感谢阅读本文。

最佳答案

最近遇到这个问题,这里没有看到任何确定的答案,所以就这样:

JosephA 走在正确的轨道上,但 PdfPageRenderOptions 没有任何关于图像保真度/质量或类似我所希望的东西。但是,它确实允许您指定图像尺寸。您所要做的就是扩大维度。

我怀疑发生的事情是 PDF 有一个它“喜欢”使用的比例,它小于您要绘制的图像。如果不指定尺寸,它会绘制“小”图像,然后按比例放大,从而导致模糊。

相反,如果您明确告诉它以更高分辨率绘制图像,它会使用 PDF 魔法使这些线条更清晰,并且生成的光栅图像不必缩放/模糊。

PdfPage page = _document.GetPage(pageIndex);
await page.RenderToStreamAsync(stream, new PdfPageRenderOptions {
    DestinationWidth = (uint)page.Dimensions.MediaBox.Width * s,
    DestinationHeight = (uint)page.Dimensions.MediaBox.Height * s
});

这样的事情会有所帮助,其中“s”是实现所需尺寸的比例因子。 PdfPage.Dimensions 具有许多不同的属性,而不仅仅是“MediaBox”,您可能还想根据您的用例探索这些属性。

关于image - 转换后的 PDF 图像在 UWP 中看起来很模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51925882/

相关文章:

pdf - 我可以使用谷歌 API 将 PDF 转换为 PNG 吗?

uwp - 无法安装 UWP 旁加载版本

php - PHP 上传和 MySQL 插入后的白色空图像

html - 博主数据 :post. firstImageUrl

java - 使用java从模板生成pdf文档的最佳方法

xaml - 为什么圆圈会被切断?

c# - 本地 UWP 客户端到本地 asp.net core api web 服务器 - 证书颁发机构无效或不正确

html - 如何将文本放置在不同位置的不同图像上?

php - WordPress 自定义图片上传字段

java - 使用 iText 一次性解析所有 PDF 页面