Kon uses the command line tool "PdfInfo" (download link).
I ask in German forum for a RegEx variant which parsed the output comfortably into a key: value object.
Swagfag gives me a very nice variant. Thx again, Swagfag.
If you then take the useful function (StdOutToVar) of cocobelgica, the command line output is written into a variable that can be used right away.
Code: Select all
PdfInfo2Obj(PdfPath, pdfinfopath) { ;using xpdf's pdfinfo to get metadata from pdf files
;original post: https://autohotkey.com/boards/viewtopic.php?t=15880&start=20
;original by: kon - 16.4.2016
;modified by: swagfag and taken by Ixiko V1.0 for AHK V1 (tested with Autohotkey.exe v1.1.30.00)
;Link: https://autohotkey.com/boards/viewtopic.php?f=9&t=59294&hilit=pdfinfo
;Description: function now returns an object (thx to swagfag for his help on RegExMatch)
; a space char in keys will be replaced from "Page size" to "Pagesize"
;Dependancies: function StdOutToVar - https://github.com/cocobelgica/AutoHotkey-Util/blob/master/StdOutToVar.ahk
; pdfinfo.exe from xpdf-toolkit - https://www.xpdfreader.com/download.html]download link
q := Chr(0x22)
Results := Object()
static XpdfPath
If !XpdfPath
XpdfPath := q . pdfinfopath . "\xdf\pdfInfo.exe" . q
CmdString := XpdfPath . " " . q . PdfPath . q
StdOut:= StdOutToVar(CmdString)
If Instr(StdOut, "Error")
return StdOut ;StdOut contains a description of error
while(Pos := RegExMatch(StdOut, "`aimO)^([^:]+):\s*(.*)$", M, A_Index == 1 ? 1 : Pos + StrLen(M[0])))
{
Results[StrReplace(M[1], " ")]:= M[2]
}
return Results
}
PdfInfo output looks like this in standard way:
Creator: PFU ScanSnap Manager 4.2.14
Producer: Adobe PDF Scan Library 2.3
CreationDate: 11/13/18 13:48:22
ModDate: 11/13/18 13:48:41
Tagged: no
Form: none
Pages: 3
Encrypted: no
Page size: 591.36 x 830.4 pts (rotated 0 degrees)
File size: 541231 bytes
Optimized: no
PDF version: 1.3
Here's an example how to use it. You only have to use the colored marked keys.
Code: Select all
#include %A_ScriptDir%\PdfInfo2Obj.ahk
PdfInfo:= Object()
FileSelectFile, pdffile,, C:\,, *.pdf ;change path to your Pdf files
PdfInfo:= PdfInfo2(pdffile)
MsgBox, % PdfInfo.Pages
MsgBox, % PdfInfo.PageSize ;leave space to have the key name
ExitApp
StdOutToVar(cmd) {
;https://github.com/cocobelgica/AutoHotkey-Util/blob/master/StdOutToVar.ahk
DllCall("CreatePipe", "PtrP", hReadPipe, "PtrP", hWritePipe, "Ptr", 0, "UInt", 0)
DllCall("SetHandleInformation", "Ptr", hWritePipe, "UInt", 1, "UInt", 1)
VarSetCapacity(PROCESS_INFORMATION, (A_PtrSize == 4 ? 16 : 24), 0) ; http://goo.gl/dymEhJ
cbSize := VarSetCapacity(STARTUPINFO, (A_PtrSize == 4 ? 68 : 104), 0) ; http://goo.gl/QiHqq9
NumPut(cbSize, STARTUPINFO, 0, "UInt") ; cbSize
NumPut(0x100, STARTUPINFO, (A_PtrSize == 4 ? 44 : 60), "UInt") ; dwFlags
NumPut(hWritePipe, STARTUPINFO, (A_PtrSize == 4 ? 60 : 88), "Ptr") ; hStdOutput
NumPut(hWritePipe, STARTUPINFO, (A_PtrSize == 4 ? 64 : 96), "Ptr") ; hStdError
if !DllCall(
(Join Q C
"CreateProcess", ; http://goo.gl/9y0gw
"Ptr", 0, ; lpApplicationName
"Ptr", &cmd, ; lpCommandLine
"Ptr", 0, ; lpProcessAttributes
"Ptr", 0, ; lpThreadAttributes
"UInt", true, ; bInheritHandles
"UInt", 0x08000000, ; dwCreationFlags
"Ptr", 0, ; lpEnvironment
"Ptr", 0, ; lpCurrentDirectory
"Ptr", &STARTUPINFO, ; lpStartupInfo
"Ptr", &PROCESS_INFORMATION ; lpProcessInformation
)) {
DllCall("CloseHandle", "Ptr", hWritePipe)
DllCall("CloseHandle", "Ptr", hReadPipe)
return ""
}
DllCall("CloseHandle", "Ptr", hWritePipe)
VarSetCapacity(buffer, 4096, 0)
while DllCall("ReadFile", "Ptr", hReadPipe, "Ptr", &buffer, "UInt", 4096, "UIntP", dwRead, "Ptr", 0)
sOutput .= StrGet(&buffer, dwRead, "CP0")
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, 0)) ; hProcess
DllCall("CloseHandle", "Ptr", NumGet(PROCESS_INFORMATION, A_PtrSize)) ; hThread
DllCall("CloseHandle", "Ptr", hReadPipe)
return sOutput
}