Embed a PDF into a WPF application

Benjamin Beaulieu picture Benjamin Beaulieu · Jan 20, 2014 · Viewed 10.2k times · Source

I'm trying to embed/display a PDF in a WPF application. So far, I've tried those solutions, without success:

  1. Display the PDF in a WindowsFormsHost hosting an AxAcroPdf control, similarly to what's shown here. The problem is that my application sets AllowsTransparency = True to create a style similar to Modern UI, but that doesn't blend well with a WindowsFormsHost (the control becomes invisible).

  2. Display the PDF in a WebBrowser control. The problem is the same.

  3. Set AllowsTransparency = False, but this causes a sluggish feeling in the application. Since I use WPF purposedly to enhance the look and feel of our business applications to the benefit of our end-users, this can't be a solution.

  4. Use a second window with AllowsTransparency = False to display the WindowsFormsHost, and hack it to make it look like a child control of the main window, as it's described here. However, I don't like the code-behind approach since I'm using MVVM.

  5. Find a native PDF control for WPF. However, I only found a couple of commercial ones and that's not an option right now.

What I need is to be able to:

  • Display a PDF or its representation (i.e. an image or a conversion to another format) in a WPF application.
  • Keep my style visually intact and fluid (AllowsTransparency must stay True).
  • Use an approach respecting the principles of MVVM (preferably no code-behind).
  • Include it in my application for free (for commercial usage).

I'm totally open to hand-made solutions, open-source libraries and even completely different approaches.

Answer

Raúl Otaño picture Raúl Otaño · Jan 21, 2014

I have two solution for this:

  1. Open your .pdf file and then print as an .xps (also you must be able for doint this in code), then this file you can embded this file in your app, and show it as a xps document. See this: Documents in WPF - MSDN - Microsoft (XPS)

  2. To use a free library, I'm not very sure if this allows show pdf, but it generate them, you can take a look at EO-Pdf.

Hope this tips helps to solve the problem.