Frage

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.

War es hilfreich?

Lösung 2

If you're opened to Open-Source solutions, I would recommend GhostScript. You can convert the PDF (with decent quality, for the most part) to individual image files of most any format you might want to work with.

The other option is to convert the PDF to HTML using pdf2htmlEX, but it will currently only compile for Linux.

I use both of the above solutions in several applications on both Linux and Windows. The advantage to the HTML way is that the text can be copied and pasted. The advantage of the GhostScript way is that the images might be more portable (smaller).

As with any open-source solution, you need to be aware of the terms of the license under which each product is released, and how that may impact your final result.

Andere Tipps

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.

There is a good solution that I used before is to use CefSharp. It's the Chrome browser engine that supports previewing PDF documents. Visit quick start page.

It is recommended to visit Troubleshooting page to set appropriate settings if you have problems.

If you want to show PDF files on your local drives you should also use these settings for the browser and then use file:/// protocol:

 CefSharp.BrowserSettings browserSettings = new CefSharp.BrowserSettings();
 browserSettings.FileAccessFromFileUrls = CefSharp.CefState.Enabled;
 browserSettings.UniversalAccessFromFileUrls = CefSharp.CefState.Enabled;
 browserSettings.TextAreaResize = CefSharp.CefState.Enabled;
 my_bowser.BrowserSettings = browserSettings;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top