How to open a password protected PDF file via DDE?

Download password protected test file: 20100222.Test.PDF.Password.pdf
;==========================================================================================================================================
;
; How to open a PDF file via DDE?
;
; How to open a password protected PDF file via 'DDE' and 'SendKeysTo' and 'DocGoTo' page?
;
; (c)Detlev Dalitz.20090622.20100222.
;==========================================================================================================================================

DirChange (DirScript ())

; Define the PDF file to read, the password and the starting page.
strFilePDF = FileFullname ("20100222.Test.PDF.Password.pdf") ; Note: Must be absolute filepath.
strPassword = "pdf"
intGoToPage = 1 ; Set starting page two. ; Note: PDF page count is zero based.


; Define the installation path to the Adobe Reader application.
strAppname = ShortCutDir (38) : "\Adobe\Reader 9.0\Reader\AcroRd32.exe"
Terminate (FileExist (strAppname) != 1, "Terminated.", "Application not found or not accessible." : @LF : strAppname)

; Run the Adobe Reader application.
hdlWinId = WinItemProcId (RunShell (strAppname, "", "", @ZOOMED, @GETPROCID), 0, 0)
Terminate (!WinWaitExist (hdlWinId, 10), "Terminated.", "Unable to open application" : @LF : strAppname)


; Do not allow user intervention for the next steps.
Display (2, "Info", "Manual user input will be blocked for a moment.")
IgnoreInput (@TRUE)
strMouseCoords = MouseCoords ("", "")


; Open DDE channel to the Adobe Reader.
DDE_Servername = "acroview"
DDE_Topicname = "control"
DDE_Channel = DDEInitiate (DDE_Servername, DDE_Topicname)
Terminate (!DDE_Channel, "Terminated.", "DDE communication channel cannot be opened." : @LF : "DDE Server: " : DDE_Servername : @LF : "DDE Topic: " : DDE_Topicname)

; Close all other documents for sure.
DDE_Command = '[CloseAllDocs()]'
blnResult = DDEExecute (DDE_Channel, DDE_Command)
Terminate (!blnResult, "Terminated.", 'DDE Execute unsuccessful:' : @LF : DDE_Command)


; Open our document.
DDE_Command = '[DocOpen("' : strFilePDF : '")]'
blnResult = DDEExecute (DDE_Channel, DDE_Command)
; Note: If the pdf file is password protected, then blnResult is @FALSE.
;Terminate (!blnResult, "Terminated.", 'DDE Execute unsuccessful:' : @LF : DDE_Command)


; Send our password to the upcoming password dialog.
; Note: The actual spelling of the dialog title is regional different and has to be adapted.
strWinTitlePWDialog = "Kennwort" ; DE-DE="Kennwort". EN-US="???". Dialog title must be adapted.
If WinWaitExist (strWinTitlePWDialog, 10)
   SendKeysTo (strWinTitlePWDialog, "^a{DEL}" : strPassWord : "{ENTER}")
Else
   Terminate (!blnResult, "Terminated.", 'Dialog "' : strWinTitlePWDialog : '" not found.')
EndIf
TimeDelay (1) ; Give the Adobe Reader some time to decrypt the password and open the document.

; Goto our starting page.
DDE_Command = '[DocGoTo(' : strFilePDF : ',' : intGoToPage : ')]'
blnResult = DDEExecute (DDE_Channel, DDE_Command)
Terminate (!blnResult, "Terminated.", 'DDE Execute unsuccessful:' : @LF : DDE_Command)


; Close DDE channel.
DDETerminate (DDE_Channel)


; Allow user intervention.
MouseMove (ItemExtract (1, strMouseCoords, " "), ItemExtract (2, strMouseCoords, " "), "", "")
IgnoreInput (@FALSE)
Display (2, "Info", "User input is active again.")



TimeDelay (10) ; Just for this example .... give the user time to read ...



; Open DDE channel to the Adobe Reader and tell the application to close itself.
If WinExist (hdlWinID)
   ; Just for this example .... ask the user for automatically closing ...
   Pause ("Note", "Do you allow the application to close itself now?")

   If WinExist (hdlWinID)
      DDE_Servername = "acroview"
      DDE_Topicname = "control"
      DDE_Channel = DDEInitiate (DDE_Servername, DDE_Topicname)
      Terminate (!DDE_Channel, "Terminated.", "DDE communication channel cannot be opened." : @LF : "DDE Server: " : DDE_Servername : @LF : "DDE Topic: " : DDE_Topicname)

      DDE_Command = '[AppExit()]'
      blnResult = DDEExecute (DDE_Channel, DDE_Command)
      Terminate (!blnResult, "Terminated.", 'DDE Execute unsuccessful:' : @LF : DDE_Command)

      DDETerminate (DDE_Channel)
   EndIf
EndIf

:CANCEL
Exit
;==========================================================================================================================================
;
; The PDF test file "20100222.Test.PDF.Password.pdf" has four pages and is protected by the password "pdf".
;
;==========================================================================================================================================
; Additional informations from ...
; "http://partners.adobe.com/public/developer/en/acrobat/sdk/pdf/iac/IACOverview.pdf"
;------------------------------------------------------------------------------------------------------------------------------------------
;
;   For all DDE messages listed in this chapter,
;   the service name is acroview,
;   the transaction type is XTYPE_EXECUTE,
;   and the topic name is control.
;   The data is the command to be executed, enclosed within square brackets.
;   The item argument in the DdeClientTransaction call is NULL.
;
;
;   DDE_SERVERNAME = "acroview";
;   DDE_TOPICNAME = "control";
;   DDE_Command = "[AppHide()]";
;
;   NOTE:The square bracket characters in DDE messages are mandatory.
;   NOTE:DDE messages are case-sensitive and must be used exactly as described.
;
;   You must first open a document using the DocOpen DDE message in order to be able to use other DDE messages on it.
;   You cannot use DDE messages to close a document that a user opened manually.
;
;   You can use NULL for pathnames, in which case the DDE message operates on the front document.
;
;   If more than one command is sent at once, they are executed sequentially, and the results appear to the user as a single action.
;   For instance, you can utilize this feature to open a document to a certain page and zoom level.
;
;   Page numbers are zero-based: the first page in a document is page 0.
;
;   Quotation marks are needed only if a parameter contains white space.
;   The document manipulation methods, such as those for deleting pages or scrolling, only work on documents that are already open.
;
;==========================================================================================================================================