Show PDF-Documents in a C# WPF Application

The easiest way to do this is to display the Adobe Reader Browser Plugin. This can be done with a UserControl which can be hosted in a WindowsFormsHost control in a WPF Form. You require a reference to the dlls AxInterop.AcroPDFLib.dll and Interop.AcroPDFLib.dll which you can find in the Reader SDK on the Adobe Developer Homepage. The code for a UserControl is shown below:

PDFControl.Designer.cs:

namespace WPFDocPoc2
{
    partial class PDFControl
    {
        /// 
        /// Required designer variable.
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// Clean up any resources being used.
        /// 
        /// true if managed resources should be disposed; otherwise, false.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// 
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// 
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(PDFControl));
            this.axAcroPDF1 = new AxAcroPDFLib.AxAcroPDF();
            ((System.ComponentModel.ISupportInitialize)(this.axAcroPDF1)).BeginInit();
            this.SuspendLayout();
            //
            // axAcroPDF1
            //
            this.axAcroPDF1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.axAcroPDF1.Enabled = true;
            this.axAcroPDF1.Location = new System.Drawing.Point(0, 0);
            this.axAcroPDF1.Name = "axAcroPDF1";
            this.axAcroPDF1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPDF1.OcxState")));
            this.axAcroPDF1.Size = new System.Drawing.Size(194, 193);
            this.axAcroPDF1.TabIndex = 0;
            //
            // PDFControl
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.axAcroPDF1);
            this.Name = "PDFControl";
            this.Size = new System.Drawing.Size(194, 193);
            ((System.ComponentModel.ISupportInitialize)(this.axAcroPDF1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private AxAcroPDFLib.AxAcroPDF axAcroPDF1;
    }
}

 

PDFControl.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WPFDocPoc2
{
    public partial class PDFControl : UserControl
    {
        public PDFControl()
        {
            InitializeComponent();
        }

        public void LoadFile(string filename)
        {
            this.axAcroPDF1.LoadFile(filename);
        }
    }
}

In the WPF Form you can host it like:

<WindowsFormsHost Name="winFormHost" Visibility="Collapsed" Width="488" Height="469" />

And initialize it like:

this.winFormHost.Child = new WPFDocPoc2.PDFControl();

To show a document:

PDFControl pdfC = winFormHost.Child as WPFDocPoc2.PDFControl;
                        pdfC.LoadFile(file); // file is a string containing the path and name of the file

Leave a comment

Your email address will not be published. Required fields are marked *