将 PDF 嵌入 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21240314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:33:19  来源:igfitidea点击:

Embed a PDF into a WPF application

c#wpfvb.netmvvmaxacropdf

提问by Benjamin Beaulieu

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

我正在尝试在 WPF 应用程序中嵌入/显示 PDF。到目前为止,我已经尝试了这些解决方案,但没有成功:

  1. Display the PDF in a WindowsFormsHosthosting an AxAcroPdfcontrol, similarly to what's shown here. The problem is that my application sets AllowsTransparency = Trueto 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 WebBrowsercontrol. 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 = Falseto 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.

  1. WindowsFormsHost托管AxAcroPdf控件中显示 PDF ,类似于此处显示的内容。问题是我的应用程序设置AllowsTransparency = True为创建类似于Modern UI的样式,但这与 a WindowsFormsHost(控件变得不可见)不能很好地融合。

  2. WebBrowser控件中显示 PDF 。问题是一样的。

  3. Set AllowsTransparency = False,但这会导致应用程序出现呆滞感。由于我特意使用 WPF 来增强我们的业务应用程序的外观和感觉以使我们的最终用户受益,因此这不能成为解决方案。

  4. 使用与第二个窗口AllowsTransparency = False来显示WindowsFormsHost,并攻击它,使它看起来像在主窗口的子控件,因为它描述了这里。但是,我不喜欢代码隐藏方法,因为我使用的是 MVVM。

  5. 查找 WPF 的本机 PDF 控件。然而,我只找到了几个商业的,现在不是一个选择。

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 (AllowsTransparencymust stay True).
  • Use an approach respecting the principles of MVVM (preferably no code-behind).
  • Include it in my application for free (for commercial usage).
  • 在 WPF 应用程序中显示 PDF 或其表示(即图像或转换为另一种格式)。
  • 保持我的风格在视觉上完整和流畅(AllowsTransparency必须保持True)。
  • 使用尊重 MVVM 原则的方法(最好没有代码隐藏)。
  • 免费将其包含在我的应用程序中(用于商业用途)。

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

我对手工制作的解决方案、开源库甚至完全不同的方法完全开放。

采纳答案by Nathan M

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.

如果您对开源解决方案开放态度,我会推荐GhostScript。您可以将 PDF(在大多数情况下具有不错的质量)转换为您可能想要使用的大多数任何格式的单个图像文件。

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

另一种选择是使用pdf2htmlEX将 PDF 转换为 HTML ,但它目前只能为 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).

我在 Linux 和 Windows 上的几个应用程序中都使用了上述两种解决方案。HTML方式的优点是可以复制和粘贴文本。GhostScript 方式的优点是图像可能更便携(更小)。

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.

与任何开源解决方案一样,您需要了解发布每个产品所依据的许可条款,以及这可能如何影响您的最终结果。

回答by Raúl Ota?o

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.

  1. 打开您的 .pdf 文件,然后打印为 .xps(您也必须能够在代码中执行此操作),然后您可以将此文件嵌入您的应用程序中,并将其显示为 xps 文档。请参阅:WPF 中的文档 - MSDN - Microsoft (XPS)

  2. 要使用免费库,我不太确定这是否允许显示 pdf,但它会生成它们,您可以查看EO-Pdf

Hope this tips helps to solve the problem.

希望这些提示有助于解决问题。

回答by Mahdi Ataollahi

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.

我之前使用的一个很好的解决方案是使用 CefSharp。它是支持预览 PDF 文档的 Chrome 浏览器引擎。访问快速启动页面

It is recommended to visit Troubleshooting pageto 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:

如果您想在本地驱动器上显示 PDF 文件,您还应该对浏览器使用这些设置,然后使用file:///协议:

 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;