Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to print PDF file from VFP directly?
Message
De
28/09/2004 16:39:44
 
 
À
28/09/2004 08:59:43
Information générale
Forum:
Visual FoxPro
Catégorie:
Autre
Divers
Thread ID:
00904836
Message ID:
00946921
Vues:
55
I came up with a way to handle this. It may not be elegant, but it works.
=PrintPDF("x:\dssvfp\xfrx\test.pdf")

FUNCTION PrintPDF
   LPARAMETERS tcPDFFile
   ** ToDo: Add Error Checking for Parameter

   LOCAL lcWindowTitle, lnWindowTitleLen, lnVFPhWnd, lnStart, lnReaderHwnd

   *== API Declarations
   DECLARE INTEGER ShellExecute IN "Shell32.dll" ;
       INTEGER hwnd, ;
       STRING lpVerb, ;
       STRING lpFile, ;
       STRING lpParameters, ;
       STRING lpDirectory, ;
       LONG nShowCmd

   DECLARE INTEGER GetForegroundWindow IN win32API
   DECLARE INTEGER Sleep IN WIN32API INTEGER nMSecs   && 1000 ms = 1 sec

   DECLARE SHORT   SetForegroundWindow IN USER32.DLL INTEGER hWnd
   DECLARE INTEGER SetActiveWindow IN USER32 INTEGER hWnd   
   DECLARE INTEGER FindWindow IN Win32API STRING @lpClassName, STRING @lpWindowName
   DECLARE INTEGER GetWindowText IN WIN32API INTEGER hwnd, STRING @lptstr, INTEGER cbmax
   lcWindowTitle = SPACE(50)
   lnWindowTitleLen = LEN(lcWindowTitle)


   *== Get HWND of current VFP session
   IF VERSION(5) >= 800   && VFP8 and later carry hWnd property
      lnVFPhWnd = _vfp.hWnd
   ELSE                 
      lnVFPhWnd = GetForegroundWindow()
   ENDIF
   

   *== Print The PDF
   = ShellExecute(0, "open", "acrord32.exe", " /p /h " + tcPDFFile, "", 0)


   *== Wait For Acrobat TO Become Active
   lnStart = DATETIME()
   DO WHILE .t.
      lnReaderHwnd = FindWindow(0, "Acrobat Reader")
      IF lnReaderHwnd = 0
         lnReaderHwnd = FindWindow(0, "Adobe Reader")
      ENDIF

      IF lnReaderHwnd # 0 OR DATETIME() - lnStart > 30   && 30 seconds max
         EXIT
      ENDIF
      
      Sleep(100)
   ENDDO
   
   IF lnReaderHwnd = 0
      ** Reader Did Not Start in 30 seconds.
      ** Just Set Focus And Bail
      SetForegroundWindow(lnVFPhWnd)
      SetActiveWindow(lnVFPhWnd)
      RETURN
   ENDIF
   

   *== Printing has begun when PDF name appears in title of Reader window
   DO WHILE .t.
      getwindowtext(lnReaderHwnd, @lcWindowTitle, lnWindowTitleLen)
      IF UPPER(JUSTFNAME(tcPDFFile))  $ UPPER(lcWindowTitle)
         EXIT
      ENDIF
   ENDDO

   *== Printing has finished when PDF name no longer in title
   DO WHILE .t.
      lcWindowTitle = SPACE(50)
      getwindowtext(lnReaderHwnd, @lcWindowTitle, lnWindowTitleLen)
      IF ! UPPER(JUSTFNAME(tcPDFFile))  $ UPPER(lcWindowTitle)
         EXIT
      ENDIF
   ENDDO
   

   *== Kill Reader Process
   IF SUBSTR(OS(1),9,1) >= "5"   && 4 = Win 98, 5= 2K & XP
      LOCAL loWMI, loProcess
      loWMI      = GetObject("winmgmts://./root/cimv2")
      loProcesses   = loWMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'AcroRd32.exe'")
      For Each loProcess in loProcesses
         loProcess.Terminate(0)
      Next

      loProcesses   = loWMI.ExecQuery("SELECT * FROM Win32_Process WHERE Name = 'Acrobat.exe'")
      For Each loProcess in loProcesses
         loProcess.Terminate(0)
      Next
   ENDIF

   *== Set Focus to VFP
   SetForegroundWindow(lnVFPhWnd)
   SetActiveWindow(lnVFPhWnd)

ENDFUNC
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform