Search code examples
c#wpfpdfopen-sourcereader

How to add an existing PDF viewer to my WPF project?


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.

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...

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.

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.

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...?

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?

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:

<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:

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:

BadImageFormatException was unhandled

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

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...

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?


Solution

  • .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:

    <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

    1: Go to src folder and open MoonPdf.sln

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

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

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

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

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

    EDIT EDIT:

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

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