C# - Host Adobe Reader in C# version independent

By , 12/28/2012
(0 ratings)
This small example project shows, how to - with a few lines of code - use the ActiveX control of Adobe Reader (prev. Acrobat Reader), without inserting a COM-reference into the project.

Furthermore it works with different versions of Adobe Reader! It does not matter whether you have installed version 7, 8 or 9.

Who is not annoyed of every customer having another version of Adobe Reader installed?

This is possible, because the CLSID of the different versions of the ActiveX control is always the same (there is one for 32 and another for 64 bit).
Using the abstract base class System.Windows.Forms.AxHost you can write wrappers for ActiveX user controls very easily. You only have to know the CLSID (if you know the ProgId you can look up the CLSID at HKEY_CLASSES_ROOT in your registry).

The example consists of a C#-Project with a Windows.Forms form and a simple Adobe Reader wrapper that can view, load and print.

You can find an entire example project at http://www.mycsharp.de/wbb2/thread.php?postid=3568114#post3568114 (in German).

Author: Rainbird, translation by Michael List
Translate to VB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;

/// <summary>
/// .NET wrapper for the Adobe Reader ActiveX-Control
/// </summary>
public class AdobeReaderControl : AxHost
{
    /// <summary>
    /// Hosts the Adobe Reader ActiveX-Control in Windows.Forms.
    /// </summary>
    public AdobeReaderControl()
        : base("{CA8A9780-280D-11CF-A24D-444553540000}") // CLSID of Adobe Reader (is equal within all 32 bit versions)
    { }

    /// <summary>
    /// Loads a PDF-file.
    /// </summary>
    /// <param name="path">Path</param>
    public void LoadFile(string path)
    {
        // Call the LoadFile method in Adobe Reader
        this.GetOcx().GetType().InvokeMember("LoadFile", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, this.GetOcx(), new object[1] { path });
    }

    /// <summary>
    /// Prints the loaded PDF-file.
    /// </summary>
    public void Print()
    {
        // Call the Print method in Adobe Reader
        this.GetOcx().GetType().InvokeMember("Print", BindingFlags.InvokeMethod | BindingFlags.OptionalParamBinding, null, this.GetOcx(), new object[0]);
    }
}

Comments

 

Log in, to comment!