Harald
Harald

Reputation: 11

UWP-Apps: How can I improve the quality of PDF-images rendered with Windows.Data.Pdf?

What I'm doing now: I am working on an UWP-App that contains a very simple PDF-Viewer for scrolling and zooming PDFs. For handling the PDFs, I'm using Windows.Data.Pdf, which renders PDF-pages to images. At the moment, I'm rendering an image for each page of an PDF-file, which I then insert into a (vertically orientated) StackPanel. The StackPanel - again - is embedded in a ScrollViewer, which easily allows to implement scrolling and zooming. This method basically works; however, the quality of the PDF-images isn't yet as good as when the same PDF-file is displayed with Adobe Acrobat Reader or Microsoft Edge: I only get good renderings of PDFs if I render them in the original size; otherwise they are blurred.

My perception of the problem: Although the Windows.Data.Pdf-Namespace provides methods for zooming and scaling, it seems to me that the PDF-page are nevertheless always rendered to images with a specific/constant resolution, which thereafter is zoomed to the required zoom-factor. Therefore, I spare using zooming via Windows.Data.Pdf and instead just zoom the rendered Image.

Is there a way to render high quality/high resolution images with Windows.Data.Pdf?

My current XAML for the UI:

<Page
...>
    <Grid ...>
        <ScrollViewer 
            x:Name="ScrollViewerOutput"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Background="black"
            HorizontalScrollMode="Enabled"
            HorizontalScrollBarVisibility="Visible"
            ZoomMode="Enabled">                        
                <StackPanel
                    x:Name="StackPanelViewer"
                    Orientation="Vertical"/>
        </ScrollViewer>
    </Grid>
</Page>

My current VB-Code:

Imports Windows.Data.Pdf
...

    Public Class ...

    Dim pdfDocument As PdfDocument
    Dim BAnz As Integer = 0
    Dim Bild(999) As Image
    Dim stream(999) As InMemoryRandomAccessStream
    Dim src(999) As BitmapImage
    ...

        Async Function ... (ByVal SFI As StorageFile) As Task

            pdfDocument = Await PdfDocument.LoadFromFileAsync(SFI)

            BAnz = 0
            For i As Integer = 0 To pdfDocument.PageCount - 1

                stream(BAnz) = New InMemoryRandomAccessStream()
                src(BAnz) = New BitmapImage()
                Bild(BAnz) = New Image()

                Using page As PdfPage = pdfDocument.GetPage(i)

                    Dim rect As Rect = page.Dimensions.TrimBox
                    Dim options As New PdfPageRenderOptions
                    options.SourceRect = New Rect(rect.X, rect.Y, rect.Width, rect.Height)
                    Await page.RenderToStreamAsync(stream(BAnz), options)

                    Bild(BAnz).MaxWidth = rect.Width
                    Bild(BAnz).MaxHeight = rect.Height
                End Using


                Bild(BAnz).Source = src(BAnz)
                Await src(BAnz).SetSourceAsync(stream(BAnz))

                Bild(BAnz).HorizontalAlignment = HorizontalAlignment.Stretch

                Bild(BAnz).VerticalAlignment = VerticalAlignment.Stretch

                Bild(BAnz).Margin = New Thickness(10)

                StackPanelViewer.Children.Add(Bild(BAnz))

                BAnz = BAnz + 1
            Next

        End Function

End Class ...

Upvotes: 1

Views: 1313

Answers (1)

Breeze Liu - MSFT
Breeze Liu - MSFT

Reputation: 3808

The Windows.Data.Pdf Namespace is for converting a page in a PDF document to an image file. PDF APIs support high fidelity rendering, but only for C++ applications using Direct2D. You can check out the PDF Showcase sample on how to do it. Though it is a C# Project call C++ library, it is similar to your VB project.

Upvotes: 0

Related Questions