.IntroductionThis article shows how to use the Adobe PDF Reader COM Component in a WPF application with no code-behind (thanks to a little help from Expression Blend Behaviours). Although Expression Blend is used in this article, it is not essential as the same result can be achieved by and typing in the required XAML manually (it just requires a bit more effort- importing the correct namespaces etc). BackgroundThe application we will be creating is very simple- it allows users to view a PDF in a WPF Window, invoke a print action and change the current PDF displayed.
This is straightforward enough to demonstrate the concepts in this article. It is assumed you already know the benefits of using MVVM; understand basic details such as the usage of and and the advantages of avoiding writing logic in the code behind (although sometimes code-behind is appropriate).
If you don’t understand the basics of MVVM, I’d suggest checking out some of the links in the external links section first.This article is in the format of a tutorial- it is assumed you are using Visual Studio 2010 and Expression Blend 4 (or have the Expression Blend 4 SDK installed) as well Adobe PDF Reader. For those without a paid licence for Visual Studio, Visual C# 2010 Express should be sufficient. Step 1 Create a New WPF Application in Visual StudioThe first step is to create a new WPF application in Visual Studio. When you have done this you will need to create a Windows Forms User Control to host the Adobe PDF Reader.You may be wondering why this is necessary? WPF cannot directly use an ActiveX control. To make an ActiveX control usable in WPF it must be hosted in a Windows Forms control. To use Windows Forms controls in WPF they themselves must be hosted in the.
This relationship is depicted on the diagram below. To create the Windows Forms User Control, choose add new item from the project’s context menu, enter 'User Control' in the Search Installed Templates textbox and then name the file PdfViewer.cs:As an alternative you could put this control in a Windows Forms Control Library, but to keep things simple we won’t.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). To the right of the DataContext property, select New and then choose MainWindowViewModel class.
This simple adjusts our XAML, so the Window's Data Context is set: Next add the following XAML (replacing the default Grid) to define the look of our page (feel free to use the designer or type the XAML in directly): This XAML defines the layout of our Window- a DockPanel that is filled by our Custom PdfViewHost and a StackPanel with two buttons- one for printing and the other for changing the PDF- pretty simple. In the Objects and Timeline Panel, select our PDFViewerHost and then find the PDFPath property (comes under Miscellaneous). Choose advanced options, Data Binding and then CurrentPdf. Alex Comerford 7-Jun-13 0:097-Jun-13 0:09Thanks for this article! This is the simplest solution I have found so far and am eager to make use of itHowever, I am struggling to get a pdf to display within my GridAt first, I followed all instructions to create Viewer, Host, ViewModel etc and could not get the PDF to display. I then downloaded the source and am referencing the AcrobatViewerMvvm project directly and using the code as in MainWindow.xaml in my own MainWindow.xamlAs you can see from Screen1.png, the viewer is previewed in the design frame to the right of my MainWindowThe xaml for this is shown below (the DockPanel is contained within the root Grid element)Screen2.png shows the application in debug mode; no pdf is displayed to the right as expected.
However, if I use the Print PDF button, the PDF appears to have been loaded as expected as it is displayed in the print preview dialogue (Screen3.png)Any suggestion as to the cause of my problem would be greatly appreciatedMy list of project references is as below, and I am able to compile and run the solution from the head of this article as expectedThanks in advance, AlexReferences:. YassJD 22-Apr-13 5:2422-Apr-13 5:24Hi,Thank you for this article! It was really helpful.I just found a small bug with newer versions of Adobe Reader (Reader XI specially). I kept looking for a couple of days before finding out what's happening so I hope it will be useful for someone else.Adobe Reader XI is actually freezing during the 'Initializing' step due to the following lines of code:acrobatViewer.LoadFile(PdfFilePath);acrobatViewer.src = PdfFilePath;I think both lines are performing the same operation and causes Reader XI to bug. Even though it was working perfectly with Reader X. I just find out after an auto-update of Adobe that the line to keep is the first one:acrobatViewer.LoadFile(PdfFilePath);I hope it will help.Cheers,Yass.