【问题标题】:The converted PDF image looks so blurry in UWP转换后的 PDF 图像在 UWP 中看起来很模糊
【发布时间】:2019-01-26 07:06:46
【问题描述】:

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

我已经阅读了另一种使用翻转查看器的方法,但我需要转换后的 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>

有什么建议吗?

请帮帮我。

感谢您阅读本文。

【问题讨论】:

  • 在这个例子中我没有得到模糊的文字 - 我写的comentsys.wordpress.com/2018/05/17/uwp-pdfview-app - 也许那里有什么东西,对仅链接评论表示歉意,希望不会被删除
  • @RoguePlanetoid 感谢回复,我看到评论了!但我认为质量问题是不可避免的。好吧,如果我减小图像的宽度,那么图像的质量看起来会好一些。目前,我认为我只是减小图像的宽度来解决这个问题。

标签: image pdf uwp pdf-generation image-quality


【解决方案1】:

听起来您遇到了需要动态调整 PDF 页面呈现为图像的分辨率的常见问题。

我不熟悉该 PDF 库,但您似乎需要使用 RenderToStreamAsync() 重载,该重载采用 PdfPageRenderOptions 参数来完成此操作。

【讨论】:

  • 嗯,我想我已经用过了..await page.RenderToStreamAsync(stream);这不是你说的吗?
  • 抱歉,我的链接不准确,RenderToStreamAsync() 有两个重载,一个采用单个参数,另一个采用两个参数。我建议尝试使用后者。
【解决方案2】:

最近碰到这个问题,在这里没有看到任何明确的答案,所以在这里:

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”,您可能还想根据您的用例探索这些属性。

【讨论】:

    猜你喜欢
    • 2016-07-15
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多