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.
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.
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 |
Compile and run the program and select the PDF file to be displayed. effect 1 is shown.
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.