简体   繁体   English

将 PDF 查看器添加到 WPF 应用程序

[英]Add a PDF viewer to a WPF application

I am new to WPF, and am trying to add a PDF viewer to my WPF application, but can't seem to work out how to do it... I have tried following a couple of tutorials/ examples that I have found online, but they don't seem to work for me for varying reasons...我是 WPF 的新手,正在尝试将 PDF 查看器添加到我的 WPF 应用程序中,但似乎无法弄清楚如何去做......我尝试按照我在网上找到的几个教程/示例进行操作,但由于各种原因,它们似乎对我不起作用......

For example, I tried following the tutorial at: https://documentation.devexpress.com/#WPF/CustomDocument114328 to add a PDF Viewer at Design Time- it says to例如,我尝试按照以下教程操作: https ://documentation.devexpress.com/#WPF/CustomDocument114328 在设计时添加 PDF 查看器 - 它说

drag the PdfViewerControl from the DX.15.2: Data & Analytics Toolbox tab and drop it onto the main window将 PdfViewerControl 从 DX.15.2: Data & Analytics Toolbox 选项卡拖放到主窗口

However, I don't seem to have a Data & Analytics tab in the toolbox... there's a Data tab, but that just has items like Pointer, Chart, ListView, etc. Is there something I need to do to add/ enable the Data & Analytics toolbar in Visual Studio?但是,我的工具箱中似乎没有Data & Analytics选项卡...有一个Data选项卡,但只有指针、图表、列表视图等项目。我需要做些什么来添加/启用Visual Studio 中的Data & Analytics工具栏?

I tried following the tutorial at: https://documentation.devexpress.com/#WPF/CustomDocument114329 to add a PDF Viewer via code- it says to我尝试按照以下教程进行操作: https ://documentation.devexpress.com/#WPF/CustomDocument114329 通过代码添加 PDF 查看器 - 它说

Open the Solution Explorer, right-click References and choose Add Reference... to add the PDF Viewer Library.打开解决方案资源管理器,右键单击参考并选择添加参考...以添加 PDF 查看器库。

Then, locate the DevExpress.Data.v15.2, DevExpress.Pdf.v15.2.Core, DevExpress.Xpf.DocumentViewer.v15.2.Core, and DevExpress.Xpf.PdfViewer.v15.2 assemblies and activate their check boxes.然后,找到 DevExpress.Data.v15.2、DevExpress.Pdf.v15.2.Core、DevExpress.Xpf.DocumentViewer.v15.2.Core 和 DevExpress.Xpf.PdfViewer.v15.2 程序集并激活它们的复选框.

But when I go to Add Reference , I can't find the assemblies it mentions anywhere, and if I 'search' for them, no items are found...但是当我去Add Reference时,我在任何地方都找不到它提到的程序集,如果我“搜索”它们,也找不到任何项目......

Am I missing an include , or do I need to import some libraries from somewhere or something in order to use these?我是否缺少include ,或者我是否需要从某处或其他地方导入一些库才能使用这些库?

Another one I have tried is: http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app which says:我试过的另一个是:http: //www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app它说:

Once this control is added to the project, the Windows Forms Designer should be open with a blank canvas.将此控件添加到项目后,Windows 窗体设计器应以空白画布打开。 You will need to open the tool box (CTRL + W, X).您需要打开工具箱 (CTRL + W, X)。 As a first step it is a good idea to add a new tab for custom controls- this is an option from the context menu on the toolbox.作为第一步,为自定义控件添加一个新选项卡是个好主意——这是工具箱上下文菜单中的一个选项。 With this new tab expanded, select “choose items” from the context menu.展开这个新选项卡后,从上下文菜单中选择“选择项目”。 When the Choose Toolbox Items dialog appears, select the COM Components tab and select Adobe PDF Reader (this will add the AcroPDF.DLL to the toolbox).出现“选择工具箱项目”对话框时,选择“ COM组件”选项卡,然后选择Adobe PDF读取器(这将添加Acropdf.dll到工具箱中)。

But I can't seem to find the Choose Toolbox Items or COM Components it talks about...但我似乎找不到它所说的Choose Toolbox ItemsCOM Components ......

Can anyone point me to a clearer tutorial, or explain how I would add a PDF viewer to my WPF application?谁能指出我更清晰的教程,或解释如何将 PDF 查看器添加到我的 WPF 应用程序? I am using Visual Studio 2015.我正在使用 Visual Studio 2015。

Edit编辑

I have tried to display the PDF file inside my application window, by doing the following:我试图通过执行以下操作在我的应用程序窗口中显示 PDF 文件:

Adding a <Grid> to display the PDF to the GUI in the XAML:添加<Grid>以在 XAML 中将 PDF 显示到 GUI:

<StackPanel>
    <Grid x:Name="browserHost" Height="300" Width="525" Margin="0,0,0,0"></Grid>
</StackPanel>

Adding a WebBrowser to the <Grid> in the C#, and pointing that to the location of the PDF I want to display:WebBrowser添加到 C# 中的<Grid>中,并将其指向我要显示的 PDF 的位置:

        System.Windows.Controls.WebBrowser browser = new System.Windows.Controls.WebBrowser();

public MainWindow()
    {
        InitializeComponent();

        try
        {
            //browser.Navigate("C:\\...\\sample.pdf");
            browserHost.Children.Add(browser);

            //browser.Visible = true;
            browser.Navigate("C:\\...\\sample.pdf");
            browserHost.Opacity = 200;
        }catch(Exception e)
        {
            Console.WriteLine("browser is visible/ not: " + browserHost.Visibility);
        }
    }

    private void Window_Loaded_1(object sender, RoutedEventArgs e)
    {
        /*Create the interop host control */
        //System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowFormsHost();

        /*Create the MaskedTextBox control */
        //browser.Navigate("C:\\...\\sample.pdf");
        //host.Child = browser;
        browserHost.Children.Add(browser);
    }

But currently, when I run my application, as soon as it loads, the browser that I've added to it displays a page that says:但是目前,当我运行我的应用程序时,一旦它加载,我添加到它的browser就会显示一个页面,上面写着:

Navigation to the webpage was canceled网页导航已取消

and a dialog box pops up asking me if I want to open or save the file ( sample.pdf - the one I'm trying to display in the browser)...并弹出一个对话框,询问我是否要打开或保存文件( sample.pdf - 我试图在浏览器中显示的文件)...

Why is it trying to download the file, rather than display it?为什么它试图下载文件,而不是显示它? How can I get the browser to display the file instead of trying to download it?如何让browser显示文件而不是尝试下载它? Or should I be using something other than a System.Windows.Controls.WebBrowser here?还是我应该在这里使用System.Windows.Controls.WebBrowser以外的东西?

As already suggested by @NawedNabiZada, one tried and straightforward way is to use embedded InternetExplorer to show Adobe PDF Reader ActiveX control.正如@NawedNabiZada 已经建议的那样,一种尝试过且直接的方法是使用嵌入式 InternetExplorer 来显示Adobe PDF Reader ActiveX 控件。 So it assumes you are running on Windows and have Adobe PDF Reader installed.因此它假定您在 Windows 上运行并安装了Adobe PDF Reader

Then you create a user control, window etc. that contains following control:然后创建一个包含以下控件的用户控件、窗口等:

<WebBrowser x:Name="pdfWebViewer"></WebBrowser>

In the constructor navigate to blank page:在构造函数中导航到空白页:

pdfWebViewer.Navigate(new Uri("about:blank"));

To load a PDF document to that control use this simple code:要将 PDF 文档加载到该控件,请使用以下简单代码:

pdfWebViewer.Navigate(fullPathToPDF);

This approach is used by many Windows software not only WPF apps including SAP client, but has a hidden problem, see this question .许多 Windows 软件都使用这种方法,不仅包括 SAP 客户端的 WPF 应用程序,而且有一个隐藏的问题,请参阅这个问题

The Adobe PDF Reader Addon in Internet Explorer must be enabled for this to work.必须启用 Internet Explorer 中的 Adob​​e PDF Reader Addon 才能正常工作。 There are various problems with Acrobat Reader XI, better to use DC version. Acrobat Reader XI 存在各种问题,最好使用 DC 版本。 To enable Adobe PDF go to IE settings, add-ons and find Adobe PDF Reader and enable it (AR XI and above).要启用 Adob​​e PDF,请转到 IE 设置、加载项并找到 Adob​​e PDF Reader 并启用它(AR XI 及更高版本)。

For me this was the preferred way compared to the code project article you linked.对我来说,与您链接的代码项目文章相比,这是首选方式。

For anyone stumbling upon this, and in need of a litte bit more control than with the WebBrowser: It's quite easy to make your own PDF viewer with Windows 10 APIs.对于任何偶然发现这一点并且需要比使用 WebBrowser 更多控制的人:使用 Windows 10 API 制作自己的 PDF 查看器非常容易。 I wrote a blog on how to do it.我写了一篇关于如何做的博客。 You can easily add other features to it like drawing on top (signature) of it and so on.您可以轻松地向其添加其他功能,例如在其顶部(签名)绘图等。

The code is available on github .代码可在github上找到。

However for super advanced features, you probably will need one of those fancy expensive libraries.但是,对于超高级功能,您可能需要其中一个昂贵的库。

The answer from @LanderV works nicely. @LanderV 的回答效果很好。 The only challenge is that his solution on github uses VS2014 and .NET Framwork 4.7.1, whereas I am using Visual Studio 2019 and .NET 5. It took me a day to get the Framwork 4.7 solution to run on my PC, but to make it run under .NET 5 was very easy.唯一的挑战是他在github上的解决方案使用 VS2014 和 .NET Framwork 4.7.1,而我使用的是 Visual Studio 2019 和 .NET 5。我花了一天时间才让 Framwork 4.7 解决方案在我的 PC 上运行,但是让它在 .NET 5 下运行非常容易。 I explain here the steps to take for both configurations.我在这里解释了两种配置的步骤。

.NET 5 .NET 5

Read docs.microsoft.com: modernize desktop to uwp if you want to know what is going on.如果您想知道发生了什么,请阅读docs.microsoft.com:将桌面现代化为 uwp

Create a new WPF .Net 5 solution in VS.在 VS 中创建一个新的 WPF .Net 5 解决方案。

In the WPF project file, change在 WPF 项目文件中,更改

<TargetFramework>net5.0-windows</TargetFramework> 

to

<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>

If the Windows on your PC is a different build, change 19041 accordingly.如果您 PC 上的 Windows 是不同的版本,请相应地更改 19041。 You might get an error message about an incorrect .json file, but clean the solution, rebuild it and there should be no more errors.您可能会收到有关不正确的 .json 文件的错误消息,但请清理解决方案,重新构建它,应该不会再出现错误。

Now just copy over the source code from Githup:现在只需从 Githup 复制源代码:
MainWindow.xaml主窗口.xaml
MainWindow.xaml.cs主窗口.xaml.cs
PdfViewer.xaml PdfViewer.xaml
PdfViewer.xaml.cs PdfViewer.xaml.cs

Now you should be able to run the application.现在您应该可以运行该应用程序了。

.NET Framwork 4.7.1 .NET 框架 4.7.1

If you can, use .NET 5 or higher, as described above, it's much easier.如果可以,请使用 .NET 5 或更高版本,如上所述,它会容易得多。 To run the Github code as it is, I had to do the following steps:要按原样运行 Github 代码,我必须执行以下步骤:

Clone from https://github.com/LanderVe/WPF_PDFDocumenthttps://github.com/LanderVe/WPF_PDFDocument克隆

Download .NET Framework 4.7.1, developer pack https://dotnet.microsoft.com/download/dotnet-framework/net471下载 .NET Framework 4.7.1,开发包https://dotnet.microsoft.com/download/dotnet-framework/net471

Read help https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance阅读帮助https://docs.microsoft.com/en-us/windows/apps/desktop/modernize/desktop-to-uwp-enhance

Use NuGet Package Manager to install Microsoft.Windows.SDK.Contracts使用 NuGet 包管理器安装 Microsoft.Windows.SDK.Contracts

There will be an error message about a .dll being installed twice.将出现关于 .dll 安装两次的错误消息。 Keep the one in the nuget location, remove the one under References which is stored in the .NET framework location.将一个保留在 nuget 位置,删除存储在 .NET 框架位置的引用下的一个。

Project should compile and run项目应该编译并运行

Remove Windows from References, it cannot be found and is no longer needed.从引用中删除 Windows,它找不到并且不再需要。

It is also possible by the cefsharp Web Browser. cefsharp Web 浏览器也可以。

it includes embedded modules for PDF, so you dont need Acrobat-Reader, or any other ActiveX.它包括用于 PDF 的嵌入式模块,因此您不需要 Acrobat-Reader 或任何其他 ActiveX。

Get-Started CefSharp in WPF WPF 中的 CefSharp 入门

对于WPF pdf查看器,您可以使用http://pdfprinting.net/

Posting an answer with @GeersJ 's comment for better visibility.发布带有@GeersJ 评论的答案以获得更好的可见性。

Microsoft now does this with WebView2 .微软现在用WebView2做到这一点。

This approach is by far the easiest and probably what most of the devs are looking for moving forward.到目前为止,这种方法是最简单的,并且可能是大多数开发人员正在寻找的前进方向。

In OP's context, the solution would look like在 OP 的上下文中,解决方案看起来像

<wv2:WebView2 Name="webView" Source="C:\sample.pdf"/>

Add the xmlns添加 xmlns

xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"

For this you need the nuget package:为此,您需要 nuget 包:

<PackageReference Include="Microsoft.Web.WebView2" Version="X.X.X.X" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM