title: How to add an existing PDF viewer to my WPF project?
body: <p>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.</p>
  
  <p>I have managed to get PDF files displayed inside my application, using a <code>System.Windows.Controls.WebBrowser</code> to display the documents, however, this does not give me the control I want over the documents...</p>
  
  <p>For example, when viewing the documents using the <code>WebBrowser</code>, the user can save/ print the document, or select &amp; 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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>However, by using the <code>WebBrowser</code> to display the PDF files, as I have done already, the user automatically has the built in features provided by the <code>WebBrowser</code> available to them (save, print, highlight text, copy), and I can't find a way of disabling these features.</p>
  
  <p>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 (<a href="https://sourceforge.net/projects/pdfsharp/?source=typ_redirect" rel="nofollow">https://sourceforge.net/projects/pdfsharp/?source=typ_redirect</a>). I have downloaded the source for MoonPDF, but I'm not sure how I now incorporate this into my application...?</p>
  
  <p>I have downloaded the source files, but there doesn't appear to be a <code>.sln</code> 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?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>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:</p>
  
  <pre><code>&lt;Window ...
          xmlns:mpp="clr-namespace:MoonPdfLib;assembly=MoonPdfLib"
          ...&gt;
      &lt;DockPanel&gt;
          ...
          &lt;Grid x:Name="host" DockPanel.Dock="Top" Margin="0,0,0,0"&gt;
              &lt;mpp:MoonPdfPanel x:Name="PdfPanel" ... /&gt;
          &lt;/Grid&gt;
      &lt;/DockPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and my .cs file is as follows:</p>
  
  <pre><code>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.
  }
  ...
  </code></pre>
  
  <p>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 <code>openFileMenuItem_click(...)</code> function, my code breaks, and I get a BadImageFormatException which says:</p>
  
  <blockquote>
    <p>BadImageFormatException was unhandled</p>
    
    <blockquote>
      <p>An unhandled exception of type 'System.BadImageFormatException' occurred in MoonPdfLib.dll</p>
    </blockquote>
  </blockquote>
  
  <p>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...</p>
  
  <p>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?</p>