Use VC to display PDF files

Source: Internet
Author: User

1 PDF Introduction
PDF is the abbreviation of "Portable Document Format". It is an electronic document format developed by Adobe and has become an international de facto standard for electronic document exchange. PDF files can be read, edited, and published on various platforms. The file format supports embedding fonts, images, and even any attachments.
Adobe provides two types of software for processing PDF documents: Acrobat and Acrobat Reader. The former is powerful and can be used not only to read but also to create or edit PDF documents. However, the fee is charged; the latter only provides the reading function and is a free software.
2. Display PDF files using ActiveX Controls
Acrobat (Reader) provides an ActiveX control pdf. ocx (version 7. X is release pdf. dll) that allows you to display PDF files in your program. For software of earlier versions (4.x and earlier versions), you only need to follow the standard programming steps of ActiveX controls:
(1) Add the control pdf. ocx to the VC project and generate the control class
(2) Place the control on the dialog box, or call the member function create of the control class to create a control object.
(3) Call the properties or methods of the control to open the PDF file.
This is not part of the content to be discussed in this article. For details, refer to the Chapter "Using ActiveX controls" in Visual C ++ technical insider.
However, for the new version of software (5.x/ 6.x/ 7.x), the above method is no longer applicable: the new version of the control is not a complete ActiveX control, it lacks the type library information necessary to generate the control class, the control cannot be added to the VC project.
In this case, you need to call the function cwnd: createcontrol to create the control object, obtain the control's idispatch interface pointer, and access the control's attributes and methods through this pointer.
It is extremely cumbersome to directly use the idispatch pointer to call the properties and methods of the control. Fortunately, the new version of the software provides a type library, you can use it to generate an automatic component Driver Class (derived from the coledispatchdriver ). This class greatly simplifies the call to control attributes and methods.
Although the PDF file can be displayed with controls, it is only a proxy and the real work is still completed by Acrobat (Reader). Therefore, one of the two software must be installed.
3. Implementation steps
The following uses Acrobat (Reader) 5.x/ 6.x as an example to describe how to use the new control to display PDF files.
Run VC to create a dialog box-based MFC Application viewpdf. When setting the generate option, select "ActiveX controls" and use the default values for the rest. Delete all controls in the dialog box.
Next, create an automatic component Driver Class: Open the Class Wizard, click the "add class" button, select "from a Type Library" in the pop-up menu, and use the Type Library File PDF. TLB generates the required component class. The file is located in the ActiveX subdirectory under the Acrobat software installation directory.
Edit viewdomaindlg. h to add two data members to the class cviewdomaindlg:
# Include "pdf. H" // header file of the component class
Class cviewpdfdlg: Public cdialog
{
Protected:
_ Dpdf m_drvpdf; // PDF component driver object; _ dpdf is the component class we just generated
Cwnd m_wndpdf; // PDF Control Window
......

Insert the following statement into the class member function cview1_dlg: oninitdialog:
// Add the ws_clipchildren style to the main window; otherwise, the control is not displayed normally.
Modifystyle (0, ws_clipchildren );
// Obtain the client rectangle in the dialog box
Crect RT;
Getclientrect (& RT );
// Create a PDF Control Window Using cwnd: createcontrol
// The control class ID of Acrobat (Reader) 5.x/ 6. x is "pdf. Export ctrl.5", and 7. X is "acropdf. pdf.1"
M_wndpdf.createcontrol ("pdf. Export ctrl.5", null, ws_child | ws_visible, RT, this, 0 );
// Obtain the idispatch interface pointer
Lpunknown = m_wndpdf.getcontrol unknown ();
Lpdispatch;
Lpunknown-> QueryInterface (iid_idispatch, (void **) & lpdispatch );
// Pass the obtained interface pointer to the component driver object m_drvpdf, and call the properties and methods of the control through it
// When m_drvpdf is destroyed, the interface pointer is automatically released
M_drv2.16.attachdispatch (lpdispatch );
// A "open" dialog box is displayed. Select the PDF file to be displayed.
Cfiledialog DLG (true );
If (DLG. domodal () = idok)
// Use the LoadFile method of the control to read and display the PDF file
M_drv).loadfile (DLG. getpathname ());
Else
Oncancel (); // exit

For Acrobat (Reader) 7.x, the implementation steps are basically the same, the difference is:
(1) Use Alibaba pdf. DLL to generate an automatic component driver class. The obtained class is iacroaxdocshim and the header file is Alibaba pdf. h.
(2) when creating the control window, the class ID is pdf. 20..1.
In addition to the LoadFile used above, the control also provides many useful methods, including:
Print File
Setzoom sets the page percentage
Setshowtoolbar display/hide Toolbar
Go to the first page
Go to the last page
Go to the next page
Gotopreviouspage jumps to the previous page
Because the usage is simple, I will not describe them here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.