如何将现有PDF查看器添加到我的WPF项目?

时间:2023-01-27 10:39:50

I have an application which I am developing in WPF and C#. I am currently looking into adding the functionality to be able to load and view PDF files (and files with custom extensions) inside my application.

我有一个我在WPF和C#开发的应用程序。我目前正在考虑添加功能,以便能够在我的应用程序中加载和查看PDF文件(以及带有自定义扩展名的文件)。

I have managed to get PDF files displayed inside my application, using a System.Windows.Controls.WebBrowser to display the documents, however, this does not give me the control I want over the documents...

我已经设法在我的应用程序中显示PDF文件,使用System.Windows.Controls.WebBrowser显示文档,但是,这并没有给我对文档的控制...

For example, when viewing the documents using the WebBrowser, the user can save/ print the document, or select & copy text, etc on these documents, but since I am going to be using this to display some confidential information, I want to 'disable' these features on the document that I am displaying.

例如,当使用WebBrowser查看文档时,用户可以保存/打印文档,或者在这些文档上选择和复制文本等,但由于我将使用它来显示一些机密信息,我想'禁用我正在显示的文档上的这些功能。

Basically, I want to display the document on screen, from within my application, but not allow the user to have any other interaction with the document.

基本上,我想在我的应用程序中在屏幕上显示文档,但不允许用户与文档进行任何其他交互。

I am aware that this won't prevent the user from taking screenshots/ photos of the content, but at least I am then doing all I can to ensure that the information remains confidential.

我知道这不会阻止用户截取内容的截图/照片,但至少我正在尽我所能确保信息保密。

However, by using the WebBrowser to display the PDF files, as I have done already, the user automatically has the built in features provided by the WebBrowser available to them (save, print, highlight text, copy), and I can't find a way of disabling these features.

但是,通过使用WebBrowser显示PDF文件,就像我已经做的那样,用户自动拥有WebBrowser提供的内置功能(保存,打印,突出显示文本,复制),我找不到一种禁用这些功能的方法。

It seems that I want to use a PDF Viewer control to display the PDFs, rather than the web browser, and I have decided to try an use an open source one called PDFSharp (https://sourceforge.net/projects/pdfsharp/?source=typ_redirect). I have downloaded the source for MoonPDF, but I'm not sure how I now incorporate this into my application...?

似乎我想使用PDF Viewer控件来显示PDF而不是Web浏览器,我决定尝试使用一个名为PDFSharp的开源控件(https://sourceforge.net/projects/pdfsharp/?源= typ_redirect)。我已经下载了MoonPDF的源代码,但我不确定我现在如何将它合并到我的应用程序中......?

I have downloaded the source files, but there doesn't appear to be a .sln file anywhere, so I can't build/ run the PDF viewer on its own... how would I use it inside my application? I wan't to be able to 'create an instance' of the PDF viewer to use inside my application- so that I could 'add it as an object' to the GUI of my application- how would I go about this?

我已经下载了源文件,但是在任何地方似乎都没有.sln文件,所以我无法自己构建/运行PDF查看器......我将如何在我的应用程序中使用它?我不能“创建PDF查看器的实例”以在我的应用程序中使用 - 这样我就可以“将它作为对象添加到我的应用程序的GUI中 - 我将如何解决这个问题?”

Edit

I have made changes to my application to use the MoonPdf viewer- having built MoonPdf, and copied the DLLs over to my project, I have edited my XAML to use the libraries:

我已经对我的应用程序进行了更改以使用MoonPdf查看器 - 已经构建了MoonPdf,并将DLL复制到我的项目中,我编辑了我的XAML以使用这些库:

<Window ...
        xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
        ...>
    <DockPanel>
        ...
        <Grid x:Name="host" DockPanel.Dock="Top" Margin="0,0,0,0">
            <mpp:MoonPdfPanel x:Name="PdfPanel" ... />
        </Grid>
    </DockPanel>
</Window>

and my .cs file is as follows:

我的.cs文件如下:

public partial class MainWindow:Window{
    String docFP = "C:\\...\\abc.pdf";
    public MainWindow(){
        InitializeComponent();
        try{
            host.Children.Add(PdfPanel);
            host.Opacity = 200;
            PdfPanel.OpenFile(docFP, null);
        }catch(Exception e){
            Console.WriteLine("browser is visible/ not: " + host.Visibility);
        }
    }

private void Window_Loaded_1(object sender, RoutedEventArgs e){
    host.Children.Add(PdfPanel);
}

private void openFileMenuItem_click(object sender, RoutedEventArgs e){
    // Working code for opening an 'open file' dialog, selecting the file, and loading it.
}
...

However, when I currently run my code, the application opens, but the PDF that should be opened and displayed from directly within my code is not displayed, and if I try to load one, using the menu item that calls the openFileMenuItem_click(...) function, my code breaks, and I get a BadImageFormatException which says:

但是,当我当前运行我的代码时,应用程序打开,但是不会显示应该直接在我的代码中打开和显示的PDF,如果我尝试加载一个,则使用调用openFileMenuItem_click的菜单项(.. 。)函数,我的代码断了,我得到一个BadImageFormatException,它说:

BadImageFormatException was unhandled

BadImageFormatException未处理

An unhandled exception of type 'System.BadImageFormatException' occurred in MoonPdfLib.dll

MoonPdfLib.dll中发生了未处理的“System.BadImageFormatException”类型异常

But I know that there are not any problems with the file I'm trying to open, as I can open and view it with no problems in Adobe...

但我知道我试图打开的文件没有任何问题,因为我可以在Adobe中打开并查看它没有任何问题...

Any ideas why the application isn't automatically loading and displaying the PDF I'm referencing in the code, or why I'm getting the exception when I try to load it using my 'load file' function?

任何想法为什么应用程序不会自动加载和显示我在代码中引用的PDF,或者为什么当我尝试使用我的'加载文件'函数加载它时我得到异常?

1 个解决方案

#1


1  

.sln file is only the solution. you could create your own solution. Or you could get moonPDF from here and build it. in your own project you have to reference it. And once it is referenced you can add it to your window like this:

.sln文件只是解决方案。你可以创建自己的解决方案。或者你可以从这里获得moonPDF并构建它。在您自己的项目中,您必须引用它。一旦它被引用,您可以将它添加到您的窗口,如下所示:

<Window xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib" ...>
  <mpp:MoonPdfPanel Background="LightGray" ViewType="SinglePage" PageDisplay="ContinuousPages" PageMargin="0,2,4,2" AllowDrop="True"/>
</Window>

EDIT:

DOWNLOAD MOONPDF: https://github.com/reliak/moonpdf

下载MOONPDF:https://github.com/reliak/moonpdf

1: Go to src folder and open MoonPdf.sln

1:转到src文件夹并打开MoonPdf.sln

2: The solution contains 3 projects, you only need MouseKeyboardActivityMonitor and MoonPdfLib

2:解决方案包含3个项目,您只需要MouseKeyboardActivityMonitor和MoonPdfLib

MoonPdf is a WPF application you can use it as example to investigate how it should be implemented, but is not necessary.

MoonPdf是一个WPF应用程序,您可以使用它作为示例来研究它应该如何实现,但不是必需的。

3: Compile/Build MoonPdfLib, even if it gives errors then it still is creating the necessary DLL's.

3:编译/构建MoonPdfLib,即使它给出错误,它仍然在创建必要的DLL。

4: Look in the BIN/RELEASE or BIN/DEBUG folder there should be some DLL's

4:在BIN / RELEASE或BIN / DEBUG文件夹中查看应该有一些DLL

5: Copy those to your project and make a reference to MoonPdfLib.dll

5:将这些复制到您的项目并引用MoonPdfLib.dll

EDIT EDIT:

Here is the binaries: https://sourceforge.net/projects/moonpdf/

这是二进制文件:https://sourceforge.net/projects/moonpdf/

You don't have to compile/build the projects yourself.

您不必自己编译/构建项目。

#1


1  

.sln file is only the solution. you could create your own solution. Or you could get moonPDF from here and build it. in your own project you have to reference it. And once it is referenced you can add it to your window like this:

.sln文件只是解决方案。你可以创建自己的解决方案。或者你可以从这里获得moonPDF并构建它。在您自己的项目中,您必须引用它。一旦它被引用,您可以将它添加到您的窗口,如下所示:

<Window xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib" ...>
  <mpp:MoonPdfPanel Background="LightGray" ViewType="SinglePage" PageDisplay="ContinuousPages" PageMargin="0,2,4,2" AllowDrop="True"/>
</Window>

EDIT:

DOWNLOAD MOONPDF: https://github.com/reliak/moonpdf

下载MOONPDF:https://github.com/reliak/moonpdf

1: Go to src folder and open MoonPdf.sln

1:转到src文件夹并打开MoonPdf.sln

2: The solution contains 3 projects, you only need MouseKeyboardActivityMonitor and MoonPdfLib

2:解决方案包含3个项目,您只需要MouseKeyboardActivityMonitor和MoonPdfLib

MoonPdf is a WPF application you can use it as example to investigate how it should be implemented, but is not necessary.

MoonPdf是一个WPF应用程序,您可以使用它作为示例来研究它应该如何实现,但不是必需的。

3: Compile/Build MoonPdfLib, even if it gives errors then it still is creating the necessary DLL's.

3:编译/构建MoonPdfLib,即使它给出错误,它仍然在创建必要的DLL。

4: Look in the BIN/RELEASE or BIN/DEBUG folder there should be some DLL's

4:在BIN / RELEASE或BIN / DEBUG文件夹中查看应该有一些DLL

5: Copy those to your project and make a reference to MoonPdfLib.dll

5:将这些复制到您的项目并引用MoonPdfLib.dll

EDIT EDIT:

Here is the binaries: https://sourceforge.net/projects/moonpdf/

这是二进制文件:https://sourceforge.net/projects/moonpdf/

You don't have to compile/build the projects yourself.

您不必自己编译/构建项目。