Viewing a PDF using Acrobat Reader 9 in a WPF application in Visual Studio 2010

by Heathesh 30. August 2010 01:27

To do this, you first need to create your WPF application in Visual Studio 2010. Next, you need to ensure that you've added the Adobe PDF reader control to your toolbox (i.e. if you don't see the "Adobe PDF Reader" item in your toolbox that means you need to add it as indicated). If you haven't done this, right click your Toolbox and select "Add Tab". Enter in the name "Adobe" or whatever you want to call the tab and hit enter. Next right click in the area underneath your new tab, and select "Choose Items". Click on the "COM Components" tab and select "Adobe PDF Reader".

Once you've got the item in your tool box you can begin by adding a Windows User Control to your project. Do not add a WPF user control "User Control (WPF)" instead select the plain vanilla "User Control". Once you've added your user control simply click and drag the "Adobe PDF Reader" item from your toolbox (added above) onto your user control.

Now you need to load the relevant PDF into the control. To do this view the code of your User Control and change the default constructor accordingly:

        public UserControl1(string filename)
        {
            InitializeComponent();

            //the default name of the PDF Viewer control is axAcroPDF1, if you've changed it don't forget to change it here
            this.axAcroPDF1.LoadFile(filename);
        }


Next go to the design view of your MainWindow WPF form. You need to click and drag a "WindowsFormsHost" item from your tool box onto your WPF form. This will host the user control you created above.

To load the PDF, go to the code behind of your MainWindow (default WPF form) and change the constructor as follows:

        public MainWindow()
        {
            InitializeComponent();

            //Pass in the relevant PDF filename to the user control, ensure you use the correct path to the file
            var uc = new UserControl1(@"c:\my_pdf.pdf");

            //Set the user control to be hosted in the Windows Forms Host
            this.windowsFormsHost1.Child = uc;
        }


That should be it! Happy WPFing!

Comments are closed



Powered by BlogEngine.NET 1.5.0.7 (with enhancements by Heathesh)
Theme by Mads Kristensen (with tweeks by Heathesh)

Certifications

Microsoft Certified Professional

Microsoft Certified Technology Specialist

Answer Questions

 

Tag cloud

Calendar

<<  May 2017  >>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

View posts in large calendar

http://heathesh.com