Displaying PDF files in a UWP app

By | February 6, 2016
In this short blogpost I want to show you how I’ve used the Windows.Data.Pdf.PdfDocument class in order to render a PDF file inside my UWP app.
In order to open a PDF from a UWP app, you can ofcourse, always use Windows.System.Launcher.LaunchUriAsync like this:
This works great, but your file is being opened in an other app. And this might not be what you want. We just want the content of the PDF file being rendered inside our UWP app itself.
PDF content in UWP

PDF content in UWP

And that’s very simple to do!
First we need to load a PDF document in a Windows.Data.Pdf.PdfDocument object. There are two way to do this: From a StorageFile or from a Stream.

This PdfDocument object has a method GetPage(int pageIndex) which returns a PdfPage object. This object represents a single page of our PdfDocument and has this interesting method: RenderToStreamAsync. This method accepts a IRandomAccessStream and optionally PdfPageRenderOptions.
Once the page is rendered to the Stream, we can set it to a BitmapImage by using the BitmapImage‘s SetSource(Asyc) method.

This BitmapImage can now be easily bound to the Source property of an Image in XAML. Or, in this case, we can bind our collection of PdfPages to an ItemsControl and display an Image for each item.

 Complete code-behind:

And that’s all there is to it!


I made a PdfViewerControl you can use in order to easily display PDF files in your UWP apps. It’s available, together with the source code in GitHub.
See this blogpost: UWP Simple PDF viewer

18 thoughts on “Displaying PDF files in a UWP app

  1. Igor Kulman (@igorkulman)

    Nice as a simple demostration, but in a real world app you would have to buy a 3rd party control, because of search, re-rendering after zoom and overall performance. I know :/

    1. PieEatingNinjas Post author

      All code is in the post. But I’ll upload the code one of these days.

  2. ericguti

    When I try to open a local pdf, I get the following error: System.Runtime.InteropServices.COMException

    what is the solution?

    1. PieEatingNinjas Post author

      Hi! I’m uploading the sources at this very moment and will post a link asap. Then you can check if you get the same exception with my sources. If that still doesn’t work, let me know.

    1. PieEatingNinjas Post author

      Hi! Sorry for my late reply. I’ll upload the sources in a minute and post a link in the post.

  3. Mario

    it work with me , but only for your PDF , I tried many times to put another file , in assets and open it by the same url but I got exception file not found

    1. PieEatingNinjas Post author

      Hi Mario,
      Could you please send me your source code via mail? I will take a look at it, it’s impossible to determine your issue otherwise.

      1. Mario

        Thanks Pieter for solving my problem
        the mistake was that I wasn’t ‘packaging’ your pdf file with your app
        and the solution done by the following steps

        Right-click the Assets folder in Visual Studio and select Add => Existing Item and then select your pdf file.

        Next, select your pdf file in Visual Studio, go to the “Properties Window” and select Content as Build Action.

          1. PieEatingNinjas Post author

            Glad that in the end everything worked out for you, Mario!

  4. Pingback: UWP Simple PDF viewer | Pie Eating Ninjas' Blog

  5. Duke

    Nice tutorial & It helped me a lot to create my notice board application.
    One problem i am facing is that resolution of image is too low. Is there a way to get the proper resolution of image?

    Thanks in advance!


Leave a Reply