Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

drag the PdfViewerControl from the DX.15.2: Data & Analytics Toolbox tab and drop it onto the main window

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?

I tried following the tutorial at: https://documentation.devexpress.com/#WPF/CustomDocument114329 to add a PDF Viewer via code- it says to

Open the Solution Explorer, right-click References and choose Add Reference... to add the PDF Viewer Library.

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.

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

Am I missing an include, or do I need to import some libraries from somewhere or something in order to use these?

Another one I have tried is: http://www.codeproject.com/Articles/380019/Using-Adobe-Reader-in-a-WPF-app which says:

Once this control is added to the project, the Windows Forms Designer should be open with a blank canvas. You will need to open the tool box (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).

But I can't seem to find the Choose Toolbox Items or COM Components it talks about...

Can anyone point me to a clearer tutorial, or explain how I would add a PDF viewer to my WPF application? I am using Visual Studio 2015.

Edit

I have tried to display the PDF file inside my application window, by doing the following:

Adding a <Grid> to display the PDF to the GUI in the XAML:

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

        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:

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

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? Or should I be using something other than a System.Windows.Controls.WebBrowser here?

like image 413
Noble-Surfer Avatar asked May 24 '16 12:05

Noble-Surfer


People also ask

How do I view a PDF in WPF?

Open PDF file from the local disk using toolbarAdd the PdfViewerControl in the MainWindow. xaml and run the project. Click the Open button in the toolbar, as shown in the following picture. In the open file dialog, enter the file name or browse the file from the local disk and select Open.

What is PDF plugin?

Description. PDF Plugin for 4D is a plugin for the 4th Dimension programming language that allows you to generate files in Adobe's Portable Document Format (PDF).


1 Answers

As already suggested by @NawedNabiZada, one tried and straightforward way is to use embedded InternetExplorer to show Adobe PDF Reader ActiveX control. So it assumes you are running on Windows and have Adobe PDF Reader installed.

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:

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.

The Adobe PDF Reader Addon in Internet Explorer must be enabled for this to work. There are various problems with Acrobat Reader XI, better to use DC version. To enable Adobe PDF go to IE settings, add-ons and find Adobe PDF Reader and enable it (AR XI and above).

For me this was the preferred way compared to the code project article you linked.

like image 80
Vojtěch Dohnal Avatar answered Sep 20 '22 09:09

Vojtěch Dohnal