Click here to Skip to main content
15,885,767 members
Articles / Desktop Programming / MFC
Article

Adobe ActiveX Control with MFC

Rate me:
Please Sign up or sign in to vote.
4.44/5 (23 votes)
9 Feb 20055 min read 333.7K   5.3K   74   76
How to use the Adobe Acrobat 7 Reader ActiveX control inside a C++ application.

Image 1

Introduction

This article is a simple how-to article on using the Adobe Acrobat 7 ActiveX control within a C++ application using MFC. The sample project included uses a minimal MDI MFC application to show that you can use multiple instances of the control within the same application. The code was kept simple and slim so that it focused more on the actual content. The source code and project file was written using Microsoft Visual C++ .NET 2003, but the code can easily be integrated into any version of Visual C++.

Background

Previous versions of Adobe Acrobat did not allow the ActiveX control to be used within an external application. According to the Acrobat Developer FAQ, the ActiveX control was only developed for use in the Microsoft Internet Explorer Web Browser and was not supported or licensed for use in any other application. With the release of Adobe Acrobat 7, the Acrobat Reader ActiveX control is now fully supported and documented, this gives application writers much more flexibility when distributing PDF files to customers. The PDFs that are opened with this control can also take advantage of the Acrobat JavaScript to communicate with each other just as they would if they were opened within a web page.

Using the code

The code is very simple and straightforward to use, it is mainly intended to show you how to import the control into your own application and not just for copying and pasting the code from the demo source. I chose to use a CWnd as the view instead of a CFormView for the demo project, this will help you understand how to create the control programmatically instead of just plopping it on a dialog from a toolbox.

The first step to using the Acrobat Reader 7 control is to create an MFC class from an ActiveX control using the Class Wizard. While in the Class View tab, right click on your project and select "Add->Add Class..." and choose the option "MFC Class From ActiveX".

Image 2

Now click "Open" and a new dialog will appear. Under the section labeled "Available ActiveX Controls", find and select the "Adobe Acrobat 7.0 Browser Document <1.0>" control from the combo box. The interface "IAcroAXDocShim" will appear in the list of interfaces. Select the interface and click the ">" arrow to add a class for it. The wizard will then create the class CAcroAXDocShim and add it to your project.

Image 3

You are almost finished, now you just need to use this generated class in your own window. Add a member variable in your window view class that is of type "CAcroAXDocShim":

#include "CAcroAXDocShim.h"

class CAcroViewer : public CWnd
{
public:
    ...

// Attributes
private:
    CAcroAXDocShim m_ctrl;
};

Now that you have added the variable, you just need to create it and use it to open PDF Files. To create it, first create a control ID, which is just an integer that is not currently being used by another control in your application. Now, at the end of your window's OnCreate handler, you call the control's Create function. The first parameter needed is a string for the window name, you can use anything for this value, I chose "AdobeWnd". The next parameter is the window style, you need WS_CHILD to make it a child and WS_VISIBLE to make it visible. Next is a RECT for the window position, I used all 0's because I made a WM_SIZE handler to resize the control when the window is resized. The next parameters are the window parent (this), and the control ID that was created.

const int CTRL_ID = 280;

int CAcroViewer::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    //Create the control, just make sure to use WS_CHILD and WS_VISIBLE.
    if (!m_ctrl.Create("AdobeWnd", WS_CHILD | WS_VISIBLE, 
                                      CRect(0, 0, 0, 0), this, CTRL_ID))
    {
        AfxMessageBox("Failed to create adobe wnd");
        return -1;
    }

    return 0;
}

The ActiveX control is now created and visible in your application. The only problem now is that its size is 0. To fix this we add a WM_SIZE handler to the window and resize the control to match the window's client rect. We must make sure that the window is valid first by calling IsWindow(m_hWnd). After this call we just simply call GetClientRect() and pass that rect to MoveWindow() for our control.

void CAcroViewer::OnSize(UINT nType, int cx, int cy)
{
    CWnd::OnSize(nType, cx, cy);

    //Resize the control with the window.
    if (IsWindow(m_hWnd))
    {
        CRect rc;
        GetClientRect(rc);
        m_ctrl.MoveWindow(rc);
    }
}

The only thing left now is to open a PDF file. This can be done with one simple call to LoadFile on the control. The file parameter can be either a file on the local file system or a URL to a file on an HTTP server.

void CAcroViewer::Open(const char *file)
{
    //Just load the file that is opened.
    m_ctrl.LoadFile(file);
}

Points of interest

Unfortunately, even though this control is now outside of the Microsoft Internet Explorer Browser, it still seems to suffer from the bugs that it has had in all other versions of the Acrobat Reader. Occasionally it seems necessary to open up Task Manager and "End Task" the "AcroRd32.exe" application to get things work properly again. It's not a problem related to this application, it's a problem with the control itself. When opening and closing several PDF files from within Internet Explorer, you occasionally run into a problem where the PDF viewer just freezes. I was hoping that Adobe would have fixed this bug with the release of Acrobat Reader 7, but it appears to be still there.

Conclusion

I hope that this article has helped those of you who want to use PDF files within your own applications and only have the Acrobat Reader. If you have the full version of Adobe Acrobat, there are many other options open to you for using PDF files within your own applications, such as OLE automation. I have worked pretty extensively with OLE automation of Acrobat within C++ and C# applications and it is a much better approach in most cases but is limited to Acrobat Professional and not supported on systems that only have the Acrobat Reader installed.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I live in the Nothern Virginia/Washington D.C. area. I have been working in the software industry since 1995. My skills are primarily in C++ and Java on Windows and Unix platforms but I also work with C# and some other programming languages (even PL/I and COBOL when necessary!).

Check out my software web site: http://www.dreamsyssoft.com

Comments and Discussions

 
GeneralLinks to other PDFs and named destination Pin
pavelmaj4-Aug-06 6:18
pavelmaj4-Aug-06 6:18 
QuestionRe: Links to other PDFs and named destination Pin
DotNetBrian16-Feb-07 8:51
DotNetBrian16-Feb-07 8:51 
GeneralWrapping Active Control Pin
arnolka20-Apr-06 11:34
arnolka20-Apr-06 11:34 
Generalsomebody help me Pin
oldnorse27-Feb-06 19:24
oldnorse27-Feb-06 19:24 
GeneralRe: somebody help me Pin
CS10127-Feb-06 19:32
CS10127-Feb-06 19:32 
GeneralLicensing for Acrobat 7 ActiveX Control Pin
Anurien19-Jan-06 5:59
Anurien19-Jan-06 5:59 
GeneralPrint pdf file Pin
dragomir14-Sep-05 19:56
dragomir14-Sep-05 19:56 
QuestionCan Acrobat ActiveX load documents via SSL ? Pin
lavrykslava28-Jul-05 4:47
lavrykslava28-Jul-05 4:47 
Helo, I need to show pdf document from a secured location, for example,

https://somelocation/somedocument.pdf

When I set

AxAcroPDF.src = "https://somelocation/somedocument.pdf";

nothing seems to happen, however, when I point viewer to the ordinary
HTTP resource, it displays necessary document:

AxAcroPDF.src = "http://somelocation/somedocument.pdf";

Any hint how to display document from a secured location ?
Thanks in advance.


QuestionHow do I view JUST call a pdf from application? Pin
DiMats13-Jul-05 4:49
DiMats13-Jul-05 4:49 
AnswerRe: How do I view JUST call a pdf from application? Pin
rocky_pulley13-Jul-05 4:55
rocky_pulley13-Jul-05 4:55 
GeneralRe: How do I view JUST call a pdf from application? Pin
DiMats13-Jul-05 6:32
DiMats13-Jul-05 6:32 
Generalview PDF file without installing Acrobat Reader Pin
dkerwood11-Jul-05 20:37
dkerwood11-Jul-05 20:37 
GeneralTrigger CWnd.OnCreate in OCX (plug in MS Powerpoint) Pin
Dankung1-Jun-05 3:26
Dankung1-Jun-05 3:26 
GeneralInvokeHelper IDs Pin
Kaiser Hans-Georg24-May-05 9:10
Kaiser Hans-Georg24-May-05 9:10 
GeneralRe: InvokeHelper IDs Pin
Kaiser Hans-Georg24-May-05 9:38
Kaiser Hans-Georg24-May-05 9:38 
GeneralRe: InvokeHelper IDs Pin
rocky_pulley24-May-05 12:55
rocky_pulley24-May-05 12:55 
GeneralPassword protecting pdf file in VB/C# Pin
kvramana4-May-05 18:59
kvramana4-May-05 18:59 
GeneralRe: Password protecting pdf file in VB/C# Pin
playring8-May-05 3:14
playring8-May-05 3:14 
GeneralRe: Password protecting pdf file in VB/C# Pin
playring8-May-05 3:15
playring8-May-05 3:15 
GeneralVC6 comments Pin
Brad Bruce26-Apr-05 8:28
Brad Bruce26-Apr-05 8:28 
GeneralRe: VC6 comments Pin
rocky_pulley26-Apr-05 8:48
rocky_pulley26-Apr-05 8:48 
GeneralSimple C# version Pin
xinminma24-Apr-05 7:31
xinminma24-Apr-05 7:31 
GeneralRe: Simple C# version Pin
rocky_pulley24-Apr-05 7:48
rocky_pulley24-Apr-05 7:48 
GeneralRe: Simple C# version Pin
xinminma25-Apr-05 1:11
xinminma25-Apr-05 1:11 
GeneralRe: Simple C# version Pin
firewood00120-Feb-06 3:31
firewood00120-Feb-06 3:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.