Author Topic: How to use the PDF-ShellTools DLL Interface, from AutoHotKey?  (Read 11285 times)

0 Members and 1 Guest are viewing this topic.

RTT

  • Administrator
  • *****
  • Posts: 918
Someone asked for, and I took a pleased time studding the subject, so here I leave my findings, in the hope this may give you, that are reading this, clever ideas to take full potential from the PDF-ShellTools scripting functionalities.

It's a useful subject for who wants to use a much more fast and versatile interface, than the command line, when developing scripts that need to call the PDF-ShellTools exported functions in batch mode, because this interface has the possibility to initialize the PDF-ShellTools DLL only once, and them call all the function it needs, when the command line interface has to initialize it on every function call, leading to script slowness.

What is AutoHotKey?
Same question arrived to me when subject question posed. As I discovered, it's a powerful, clever, and free, Windows automation and script language, encapsulated in a very portable tool. Much more at official site
http://www.autohotkey.com/

The basic idea of using the DLL interface, and this apply to any coding language able to use it, comprehend three basic steps:

  1º - Load, initializing, the PDF-ShellTools DLL.
  2º - Call all the PDF-ShellTools exported function our script needs to use.
  3º - Finalize the DLL, when not needed anymore.

As explained in the PDF-ShellTools user's guide, all the PDF-ShellTools exported functions have the same prototype, only the function entry point (the function name) changes, and uses the stdcall calling convention.

In C language:
void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow);

And the function parameters are as follows:

 hwnd - window handle that should be used as the owner window for any windows our DLL creates.
 hinst - NOT USED
 lpszCmdLine - ASCIIZ command line, composed using the same schema as for the command line interface.
 nCmdShow - NOT USED

Knowing this, and with the help of the AHK documentation, the use of the PDF-ShellTools DLL interface from AHK scripts is straightforward, as the next, self explanatory and dumb, example describe.
(Please take note this is a simple example, coded by someone who only now learned a bit of AHK)

Code: [Select]
; we start with two simple AHK function defines, just to simplify related PDF-ShellTools exported functions calls from AHK script code

;----------------------------------------------------------------------------
; First function define, used to call the PDF-ShellTools Merge function
;----------------------------------------------------------------------------
Merge(WindowHandle, OutputFilename, SilentMode, FilesList){

;Check passed parameters and construct command line string to pass to DLL called function
cmd:=""
if ("%OutputFilename%"!="")
cmd =%cmd% Filename=%OutputFilename%

if (SilentMode==1)
cmd =%cmd% -s

cmd= %cmd% %FilesList%

; Our PDF-ShellTools DLL interface function prototype, translated to AHK language.
return DllCall("PDFShellTools.dll\Merge", "UInt", WindowHandle, "UInt", 0 , "Str", cmd, "UInt", 0)
}

;----------------------------------------------------------------------------
; Second function, used to call the PDF-ShellTools TextExtract function
;----------------------------------------------------------------------------
TextExtract(WindowHandle, PageRange, PageBreak, Encoding, OutputPath, SilentMode, FilesList){

;Check passed parameters and construct command line string to pass to DLL called function
cmd:=""
if ("%PageRange%"!="")
cmd =%cmd% PageRange=%PageRange%

if ("%PageBreak%"!="")
cmd =%cmd% PageBreak=%PageBreak%

if ("%Encoding%"!="")
cmd =%cmd% Encoding=%Encoding%

if ("%OutputPath%"!="")
cmd =%cmd% OutputPath=%OutputPath%

if (SilentMode==1)
cmd =%cmd% -s

cmd= %cmd% %FilesList%

return DllCall("PDFShellTools.dll\TextExtract", "UInt", WindowHandle, "UInt", 0 , "Str", cmd, "UInt", 0)
}

;----------------------------------------------------------------------------
;Our "useful" script begins here
;----------------------------------------------------------------------------

#space:: ; This script will be invoked by the "Win (Windows logo key) + Space" hotkey

dllpath=%ProgramFiles%\PDF-ShellTools\PDFShellTools.dll ; a simple way to define a universal path to the PDFShellTools.dll, when PDF-ShellTools installed at installer default folder.

; and our DLL interface basic three steps start here

; 1º - load and initialize the library
hModule:=DllCall("LoadLibrary", "STR", dllpath)

; 2º - Call any sequence of exported PDF-ShellTools functions. In this case we are using above ahk defined functions help

result1:=Merge(0,"c:\temp\t.pdf", 0 ,"C:\temp\a*.pdf")
result2:=TextExtract(0, "1-2", "-[p]/[P]-","2","C:\Temp", 0 ,"C:\temp\t.pdf")

; 3 º - Now that we don't need it anymore, unload the PDF-ShellTools.dll
DllCall("FreeLibrary", "UInt", hModule)  ; unload the library to free memory

I think the above example is very simple to understand, but, if not, or you have doubts on how to develop a related idea you may have, post your question.

._.._.._.._.._.._.._.._.._.._.._.