Click here to Skip to main content
Click here to Skip to main content

Adobe ActiveX Control with MFC

, 9 Feb 2005
Rate this:
Please Sign up or sign in to vote.
How to use the Adobe Acrobat 7 Reader ActiveX control inside a C++ application.

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".

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.

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

Share

About the Author

rocky_pulley
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

 
SuggestionHow to use this example in a dialog window PinmemberSepp Obermayer15-Apr-13 21:09 
GeneralCrash on Tab-out Pinmemberchaau10-Jun-10 14:08 
GeneralActiveX Control wrapper class(es) for Acrobat reader PinmemberAnita Banerjee28-Aug-09 12:38 
QuestionQuery PinmemberMember 319901212-Aug-09 19:14 
Questionhow to update pdf source file... Pinmemberraj2313621-Jun-09 21:31 
QuestionHow to create pdf file ? Pinmemberjasuja_moris31-Oct-08 18:38 
GeneralAdobe Acrobat 8 Reader PinmemberDJM8-Apr-08 3:17 
GeneralBookmarks PinmemberEng.Habeba5-Mar-08 22:18 
GeneralPDF files not displayed while opened with a deny write share Pinmembertriendl.kj6-Jan-08 23:02 
GeneralRe: PDF files not displayed while opened with a deny write share Pinmembertriendl.kj7-Jan-08 1:15 
Generalscrolling the pdf file Pinmemberparisaeslambolchi21-Aug-07 8:06 
QuestionDisable right click menu from PDF PinmemberRaj248120-Aug-07 23:55 
AnswerRe: Disable right click menu from PDF PinmemberBryan_Han7-Oct-10 20:40 
AnswerRe: Disable right click menu from PDF PinmemberBryan_Han7-Oct-10 21:37 
GeneralVC++ 6.0 PinmemberFahad Ahmed6-Jun-07 9:34 
QuestionDoes it really work? Pinmembermark-w15-Apr-07 17:46 
AnswerRe: Does it really work? Pinmembereusto6-Jul-07 2:21 
QuestionHow to refresh the dialog? Pinmemberrootdial22-Mar-07 8:12 
GeneralQuery for finished PinmemberMGDBP8-Mar-07 23:10 
GeneralOpening password protected pdfs Pinmemberflumb22-Feb-07 0:32 
GeneralPrint & Right Click Pinmembereusto5-Feb-07 11:36 
GeneralIAcroAXDocShim.LoadFile COMException Pinmemberjim_little_20001-Feb-07 13:44 
Questioncan i do the same with ATL only without needing MFC? Pinmembercordoval20-Jan-07 11:29 
GeneralLinks to other PDFs and named destination Pinmemberpavelmaj4-Aug-06 6:18 
QuestionRe: Links to other PDFs and named destination PinmemberDotNetBrian16-Feb-07 8:51 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 9 Feb 2005
Article Copyright 2005 by rocky_pulley
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid