Click here to Skip to main content
15,881,600 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.6K   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

 
QuestionProgram stopped working Pin
Davide Ricotti18-Jun-21 4:09
Davide Ricotti18-Jun-21 4:09 
AnswerRe: Program stopped working Pin
kobarry27-Apr-23 21:50
kobarry27-Apr-23 21:50 
QuestionA bug report Pin
Member 857373425-Feb-15 23:52
Member 857373425-Feb-15 23:52 
SuggestionHow to use this example in a dialog window Pin
Sepp Obermayer15-Apr-13 21:09
Sepp Obermayer15-Apr-13 21:09 
GeneralCrash on Tab-out Pin
chaau10-Jun-10 14:08
chaau10-Jun-10 14:08 
GeneralActiveX Control wrapper class(es) for Acrobat reader Pin
Anita Banerjee28-Aug-09 12:38
Anita Banerjee28-Aug-09 12:38 
QuestionQuery Pin
Member 319901212-Aug-09 19:14
Member 319901212-Aug-09 19:14 
Questionhow to update pdf source file... Pin
raj2313621-Jun-09 21:31
raj2313621-Jun-09 21:31 
QuestionHow to create pdf file ? Pin
jasuja_moris31-Oct-08 18:38
jasuja_moris31-Oct-08 18:38 
GeneralAdobe Acrobat 8 Reader Pin
DJM8-Apr-08 3:17
DJM8-Apr-08 3:17 
GeneralBookmarks Pin
Eng.Habeba5-Mar-08 22:18
Eng.Habeba5-Mar-08 22:18 
GeneralPDF files not displayed while opened with a deny write share Pin
triendl.kj6-Jan-08 23:02
professionaltriendl.kj6-Jan-08 23:02 
GeneralRe: PDF files not displayed while opened with a deny write share Pin
triendl.kj7-Jan-08 1:15
professionaltriendl.kj7-Jan-08 1:15 
Generalscrolling the pdf file Pin
shvonnes21-Aug-07 8:06
shvonnes21-Aug-07 8:06 
QuestionDisable right click menu from PDF Pin
Raj248120-Aug-07 23:55
Raj248120-Aug-07 23:55 
How to disable right click menu from the file? I want to disable all the menus and just want user to view the file. Is it possible?
AnswerRe: Disable right click menu from PDF Pin
Bryan_Han7-Oct-10 20:40
Bryan_Han7-Oct-10 20:40 
AnswerRe: Disable right click menu from PDF Pin
Bryan_Han7-Oct-10 21:37
Bryan_Han7-Oct-10 21:37 
GeneralVC++ 6.0 Pin
Fahad Ahmed6-Jun-07 9:34
Fahad Ahmed6-Jun-07 9:34 
QuestionDoes it really work? Pin
mark-w15-Apr-07 17:46
mark-w15-Apr-07 17:46 
AnswerRe: Does it really work? Pin
eusto6-Jul-07 2:21
eusto6-Jul-07 2:21 
QuestionHow to refresh the dialog? Pin
rootdial22-Mar-07 8:12
rootdial22-Mar-07 8:12 
GeneralQuery for finished Pin
MGDBP8-Mar-07 23:10
MGDBP8-Mar-07 23:10 
GeneralOpening password protected pdfs Pin
flumb22-Feb-07 0:32
flumb22-Feb-07 0:32 
GeneralPrint & Right Click Pin
eusto5-Feb-07 11:36
eusto5-Feb-07 11:36 
GeneralIAcroAXDocShim.LoadFile COMException Pin
jim_little_20001-Feb-07 13:44
jim_little_20001-Feb-07 13:44 

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.