
Path.Combine(currentFolder, " SamplePDF1.pdf"), String currentFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase) If (DesignerProperties.GetIsInDesignMode( new DependencyObject()))
Adobe pdf toolkit examples code#
Our ViewModel class in all of its glory is as follows:Ĭopy Code public class MainWindowViewModel : INotifyPropert圜hanged Normally I don’t use this and favour using the Rela圜ommand (as this is more widely used and supports generics), however as we will be referencing Expression Blend SDK assemblies, I figured we might as well use it in our app. This library contains the class ActionCommand, an ICommand implementation- similar to Josh Smith’s Rela圜ommand or PRISM’s DelegateCommand. For the purposes of this article though we will create a contrived ViewModel that simply implements INotifyPropert圜hanged.īefore creating the ViewModel you need to add a reference to the assembly (available via the Expression Blend SDK). In a real world application, I would recommended at least creating a base class for your ViewModels or better yet, use one of the popular frameworks like MVVM Light or Microsoft PRISM. Now that we have our classes to make the ActiveX control play nicely with WPF and its binding system, we need to create a simple ViewModel.
Adobe pdf toolkit examples windows#
The control also has a Print method, which simple delegates to the underlying Windows Forms Control. It is worth noting that when using XAML at runtime the SetValue method is called directly, hence there is no code to change the Windows Forms User control property in the. This is a standard DependencyProperty that uses a Propert圜hangedCallback Delegate to change the wrapped object property each time the value of the dependency property changes.

The main part of this control is a dependency property that wraps the Windows Forms Control’s PdfFilePath property. We now have a class that wraps the Windows Forms User Control, which in turn wraps the Adobe Reader COM component: Private static void PdfPathPropert圜hanged(DependencyObject d, DependencyPropert圜hangedEventArgs e) Return ( string)GetValue(PdfPathProperty) Private readonly PdfViewer wrappedControl " PdfPath", typeof( string), typeof(PdfViewerHost), new PropertyMetadata(PdfPathPropert圜hanged)) Public static readonly DependencyProperty PdfPathProperty = DependencyProperty.Register( Create a new class called PdfViewerHost, and addĬopy Code public class PdfViewerHost : WindowsFormsHost The next step is to subclass WindowsFormsHost to wrap our custom Windows Forms Component and add a DependencyProperty so that our custom WPF element is easy to use with WPF DataBinding.įirst add a reference in the project to WindowsFormsIntegration assembly.

The private method ChangeCurrentDisplayedPdf simply changes the current PDF displayed in the Viewer and sets the scroll position to be the top of the document.Īt this stage we have enough functionality in the Windows Forms component to allow it to be used from WPF. In the rest of the class we add a property “PdfFilePath” so that the current PDF that is showing can be changed as well as a method that allows the PDF to be printed showing the printer options dialog. In the constructor we tell the control to hide the toolbar and set the reader to use FitH (Fit Horizontal). Private string pdfFilePath public PdfViewer()ĪtViewScroll( " FitH", 0) This relationship is depicted on the diagram below:Ĭopy Code public partial class PdfViewer : UserControl To use Windows Forms controls in WPF they themselves must be hosted in the WindowsFormsHost element. To make an ActiveX control usable in WPF it must be hosted in a Windows Forms control. You may be wondering why this is necessary? WPF cannot directly use an ActiveX control. When you have done this you will need to create a Windows Forms User Control to host the Adobe PDF Reader. The first step is to create a new WPF application in Visual Studio.

Step 1 Create a New WPF Application in Visual Studio For those without a paid licence for Visual Studio, Visual C# 2010 Express should be sufficient. 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. If you don’t understand the basics of MVVM, I’d suggest checking out some of the links in the external links section first.

It is assumed you already know the benefits of using MVVM understand basic details such as the usage of INotifyPropert圜hanged and ICommand and the advantages of avoiding writing logic in the code behind (although sometimes code-behind is appropriate).
