-2

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?

Noble-Surfer
  • 3,052
  • 11
  • 73
  • 118

1 Answers1

2

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

Nawed Nabi Zada
  • 2,819
  • 5
  • 29
  • 40
  • Thanks for your suggestion- I've given this a go, but I'm getting a compile error in my XAML on the line `` at the moment, which says that `the name 'MoonPdfPanel' does not exist in the namespace 'mpp'`... When I try running the application, I get a `XamlParseException`- if I 'View Detail' on that exception, the InnerException says "Could not load file or assembly MoonPdfLib... or one of its dependencies. An attempt was made to load a program with an incorrect format."... Any ideas why this is? How can I fix this? – Noble-Surfer May 25 '16 at 15:16
  • The `XamlParseException` that I get when trying to run the application is not on the same line as the compile error- it's on the assembly reference that I'm using for the `MoonPdfPanel`- the line: `xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"`. – Noble-Surfer May 25 '16 at 15:36
  • You have to compile the other project and reference it in your. While you are referencing it in your project make sure all .DLL's are copied with your released program. – Nawed Nabi Zada May 26 '16 at 06:06
  • You can do that manually, or you can in the properties of the reference choose to copy. – Nawed Nabi Zada May 26 '16 at 06:07
  • I downloaded the source for the `MoonPdf` project from https://sourceforge.net/projects/moonpdf/, but when I try to build it, I get an error that says: `Severity Code Description Project File Line Suppression State Error The command "xcopy C:\...\moonpdf\src\..\bin\MuLib\AnyCPU\libmupdf.dll C:\...\MoonPdf\bin\Debug\ /Y" exited with code 4.`. If I just try to run it, a popup window shows the error: `A project with an Output Type of Class Library cannot be started directly. In order to debug this project, add an executable project to this solution which references the library project.` – Noble-Surfer May 26 '16 at 08:39
  • `Set the executable project as the startup project.` I don't really know what to do here... What executable project should I be adding to the solution for `MoonPdf`? Does it mean to add the solution that I want to use MoonPdf in? i.e. my own application? – Noble-Surfer May 26 '16 at 08:41
  • Hey, thanks for the edit. So I have successfully downloaded MoonPdf, built it, and copied the DLLs across to my project. However, when I run my application, although it should automatically display a PDF (I have hardcoded the filepath into my application), no PDF is displayed. If I try to load one (File->Load), my application throws a `BadImageException`, and says it occurred in MoonPdfLib.dll... any ideas why this is? – Noble-Surfer May 27 '16 at 08:56
  • Yes, You have to use same `platform target` for both projects. eg. `32bit/64bit`. You can change that by right clicking on the projects and choosing `properties`, under `Build` you have that option – Nawed Nabi Zada May 27 '16 at 09:24
  • Ah- forgot to do that. I've built the DLLs again with the right platform target, and tried running my application- but had the same issue with the PDF not being loaded automatically. When I tried loading it from the menu, I got the `BadImageFormatException was unhandled` message again, however, this time it is highlighting the line: `Context = NativeMethods.NewContext(IntPtr.Zero, IntPtr.Zero, FZ_STORE_DEFAULT);` in `private sealed class PdfFileStream : IDisposable` in `MuPdfWrapper.cs`... Is there something I would need to do to build this particular class or something? – Noble-Surfer May 27 '16 at 09:50
  • The `InnerException` on that exception just says `null`, and the `message` says 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT:0x8007000B)`, but I don't think there's any more information than that available about the exception or why I'm getting it... – Noble-Surfer May 27 '16 at 09:54
  • Now I don't use the `MoonPdf` myself, but I just had a quick look at the `source`, and they use another `DLL` in `NativeMethods` class. The dll is called `libmupdf.dll`. I could't really find that one in the source from `gethub`. Although it was in the `binaries` in `Sourceforge`. So I would say **sorry** for making you download the source and compile it yourself, but I guess the if you download the correct version of the binaries from `sourceforge` then it should work – Nawed Nabi Zada May 27 '16 at 10:22
  • Yeah, thanks- I have `libmupdf.dll` in my solution, which I built when following the example at http://www.codeproject.com/Articles/579878/MoonPdfPanel-A-WPF-based-PDF-Viewer-Control . I have added a reference to this in my application, but when I run my app, and try to load a PDF file using File->Load, and exception that says `DLLNotFoundException was unhandled in MoonPdfLib.dll`. It says that it was unable to load `libmupdf.dll`, and that "the specified module could not be found", even though I can see it there in the root of my project directory. – Noble-Surfer May 27 '16 at 11:04
  • Since this one is not referenced and is loaded during run time you have to copy it to your output folder. Normally BIN\DEBUG or BIN\RELEASE – Nawed Nabi Zada May 27 '16 at 11:52
  • Here I made a simple project where it is working. You will get some exceptions if you run it as DEBUG but that's because I copied the dll's and pdf to RELEASE folder, as release it will work http://s000.tinyupload.com/index.php?file_id=84184534024272652000 – Nawed Nabi Zada May 27 '16 at 12:31
  • I downloaded your source, but when I try to build the project, I get an error that says `Source file 'C:\...\NewWPFApp\ColumnWidthToDoubleConverter.cs' could not be found`- I've had a look in that location, and the file does not exist there... I also get `The name "MoonPdfPanel" does not exist in the namespace "clr-namespace:MoonPdfLib;assembly=MoonPdfLib".`, even though I have copied the three DLLs (libmupdf.dll, MoonPdfLib.dll & MouseKeyboardActivityMonitor.dll) to NewWPFApp\bin\Release and added the references to the project. – Noble-Surfer May 27 '16 at 13:03
  • Ahh... The columnWidthToDoubleConverter.cs shouldn't be there, if you in your Solution Explorer delete it from the list then the error will disappear, and the other error with "...does not exist in the namespace..." you can ignore, it's just for the designer, your project will build. But you should only use this to see how to implement the 3 DLL's – Nawed Nabi Zada May 27 '16 at 13:20
  • So it seems that I am using the 3 DLLs in exactly the same way in my project... and yet I get this runtime error when trying to run the application... – Noble-Surfer May 27 '16 at 15:19
  • Ah- found the problem... I had copied the DLLs to `bin\release`, but should have copied them to `bin\x86\release`. Copying them to this location has worked – Noble-Surfer May 27 '16 at 15:41