Using Visual C + + to achieve the PDF document shows

Author:Anonymous    Updated:2008-10-8 12:29:59

PDF presentation

 

   PDF is "Portable Document Format Portable Document Format," an acronym, which is developed by Adobe an electronic document format has become the international de facto standard for electronic document exchange. PDF files can be read under a variety of platforms, edit, publish. The font file format support, images, even in annex any of the embedded.

 

   Adobe offers two types of software used to handle PDF documents, namely, Acrobat and Acrobat Reader: The former powerful, can not only read but also can create or edit PDF documents, but charges; have read only the latter function for free Software.

 

   Using ActiveX controls display PDF files

 

   Acrobat (Reader) provides an ActiveX control pdf.ocx (7.x version AcroPDF.dll), the user can program the realization of PDF documents show. The old version of the software (4.x and previous versions), only ActiveX controls in accordance with the standard programming steps:

 

   (1) by adding VC Controls pdf.ocx projects, and generate control category

 

   (2) the controls placed in the dialog box, or type of call control functions of the members of the Create to create the control target

 

   (3) call control property or method to open the PDF files

 

   Since this is not to discuss the content of this article, please refer to the details of the "Visual C + + Technical Insider," "the use of ActiveX controls," the chapter, will not go into here.

 

   But the new version of software (5.x/6.x/7.x), the above-mentioned methods are no longer applicable: the new version of control is not a complete sense of ActiveX controls, the lack of it generated by the type of controls necessary to type library information, can not control VC join the project.

 

   In this case, the need to call the function CWnd:: CreateControl control to create the object, and then control access to the IDispatch interface guidelines, the guidelines adopted by the visit to control the properties and methods.

 

   IDispatch pointer with a direct call to control the properties and methods is a very tedious work, the new software provides a good type library, you can use it to generate a category-driven automation components (derived from the COleDispatchDriver). With such, can greatly simplify the control of the properties and methods of call.

 

   Although the control can display PDF files, but it is only a proxy, the real work or Acrobat (Reader) completed, it is necessary to install both a software.

 

   Steps to achieve

 

   To the following Acrobat (Reader) 5.x/6.x, for example, describes how to use the controls to achieve a new version of PDF documents show.

 

   Running VC, a new dialog-based MFC application ViewPDF, set up in the generation of options, select the "ActiveX Controls", the rest can use the default values. Generation after the dialog box to remove all controls.

 

   Then drive to create automated component categories: Open Class Wizard, click on "Add Class" button in the pop-up menu, select "From a type library", with the type of library documents pdf.tlb produce components required for the category, the Located in the Acrobat document software to install ActiveX subdirectory of the directory.

 

   Editor ViewPDFDlg.h, for the Class CViewPDFDlg add two members of the data:

# include "pdf.h" / / components in the first category of documents

 class CViewPDFDlg: public CDialog

 (

  protected:

   _DPdf M_drvPDF; / / PDF-driven component object, _DPdf we just generate the type of components

   CWnd m_wndPDF; / / PDF control window

   ... ...

   In the category of members of the function CViewPDFDlg:: OnInitDialog insert the following statement:

 

 / / Add the main window WS_CLIPCHILDREN style, or do not show the normal controls

 ModifyStyle (0, WS_CLIPCHILDREN);

 / / Dialog box to obtain the customer rectangular area

 CRect rt;

 GetClientRect (& rt);

 / / Use CWnd:: CreateControl create PDF window controls

 / / Acrobat (Reader) 5.x/6.x control the type of ID for the "PDF.PdfCtrl.5", 7.x for "AcroPDF.PDF.1"

 m_wndPDF.CreateControl ( "PDF.PdfCtrl.5", NULL, WS_CHILD | WS_VISIBLE, rt, this, 0);

 / / Obtain IDispatch interface pointer

 LPUNKNOWN lpUnknown = m_wndPDF.GetControl Unknown ();

 LPDISPATCH lpDispatch;

 lpUnknown-> QueryInterface (IID_IDispatch, (void **) & lpDispatch);

 / / Pointer to be passed to the interface of the component object-driven m_drvPDF, through its control of the call attributes and methods

 / / When m_drvPDF destruction, it will automatically release interface pointer

 m_drvPDF.AttachDispatch (lpDispatch);

 / / A pop-up "Open" dialog box, choose to display PDF files

 CFileDialog dlg (TRUE);

 if (dlg.DoModal () == IDOK)

  / / Control the use of the method LoadFile read and display PDF files

  m_drvPDF.LoadFile (dlg.GetPathName ());

 else

  OnCancel (); / / exit

   Compiling run the program, choose to display PDF files, the results shown in Figure 1.

 

 

 Figure 1

   For Acrobat (Reader) 7.x, and the basic steps to achieve the same, the difference is:

 

   (1) the use of automation components AcroPDF.dll generation driver categories, the categories have been named IAcroAXDocShim, the first document to acropdf.h.

 

   (2) Control the creation of a window, type of ID for AcroPDF.PDF.1.

 

   In addition to the above LoadFile used, the control also provides many useful ways, there are common:

 

   Print to print documents

   setZoom set percentage of the page

   setShowToolbar Show / Hide Toolbar

   gotoFirstPage to jump on the first page

   Jump to the last page of gotoLastPage

   gotoNextPage jump to the next page

   gotoPreviousPage jump to the previous page

 

   As the usage is very simple, there is no longer described one by one.

 

   This article routines in the Visual C + + 6, Acrobat (Reader) 5.x/6.x/7.x debugging through, for the convenience of the reader, the routine has been placed on the Internet.

Previous:A Java programmer should master the skills of 10
Next:Using VC 6.0 of the three serial communication methods
User Reviews
Site Search
Related Articles
Recommended article
AD