Add a PDF viewer to a WPF application

岁酱吖の 提交于 2020-02-18 05:06:30

问题


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?


回答1:


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.




回答2:


For anyone stumbling upon this, and in need of a litte bit more control than with the WebBrowser: It's quite easy to make your own PDF viewer with Windows 10 APIs. I wrote a blog on how to do it. You can easily add other features to it like drawing on top (signature) of it and so on.

The code is available on github.

However for super advanced features, you probably will need one of those fancy expensive libraries.




回答3:


It is also possible by the cefsharp Web Browser.

it includes embedded modules for PDF, so you dont need Acrobat-Reader, or any other ActiveX.

Get-Started CefSharp in WPF




回答4:


For WPF pdf viewer you can use http://pdfprinting.net/



来源:https://stackoverflow.com/questions/37413861/add-a-pdf-viewer-to-a-wpf-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!