2013-10-30 173 views
1

我試圖閱讀PDF的頁面大小例如height X weight。頁面大小可在AutoIt - 如何閱讀pdf文檔屬性

File -> Properties -> Page Size 

我用下面的代碼來獲取屬性值中找到:

$path = FileOpenDialog("Select a file to read attributes",@ScriptDir,"All (*.*)") 
$prop = _GetExtProperty($path,-1) 
_ArrayDisplay($prop,"Property Array") 

Func _GetExtProperty($sPath, $iProp) 
    Local $iExist, $sFile, $sDir, $oShellApp, $oDir, $oFile, $aProperty, $sProperty 
    $iExist = FileExists($sPath) 
    If $iExist = 0 Then 
     SetError(1) 
     Return 0 
    Else 
     $sFile = StringTrimLeft($sPath, StringInStr($sPath, "\", 0, -1)) 
     $sDir = StringTrimRight($sPath, (StringLen($sPath) - StringInStr($sPath, "\", 0, -1))) 
     $oShellApp = ObjCreate ("shell.application") 
     $oDir = $oShellApp.NameSpace ($sDir) 
     $oFile = $oDir.Parsename ($sFile) 
     If $iProp = -1 Then 
      Local $aProperty[35] 
      For $i = 0 To 34 
       $aProperty[$i] = $oDir.GetDetailsOf ($oFile, $i) 
      Next 
      Return $aProperty 
     Else 
      $sProperty = $oDir.GetDetailsOf ($oFile, $iProp) 
      If $sProperty = "" Then 
       Return 0 
      Else 
       Return $sProperty 
      EndIf 
     EndIf 
    EndIf 
EndFunc ;==>_GetExtProperty 

通過使用上面的代碼我設法MB獲得文件大小,創建日期,修改日期,位置等,但不是頁面大小。讚賞,如果任何人都可以建議我如何獲得頁面大小。任何參考,建議或示例代碼非常感謝。

+0

我也不能獲得具有這種功能的PDF屬性...你限制在「只的AutoIt」,爲什麼從AU3腳本你不'run'外部工具,如xpdf中的'pdfinfo',捕獲它的標準輸出並解析頁面大小? – user2846289

回答

1

幾乎相同,但也許它有幫助。

 #include <Array.au3> 
;=============================================================================== 
; Function Name.....: _FileGetProperty 
; Description.......: Returns a property or all properties for a file. 
; Version...........: 1.0.2 
; Change Date.......: 2008-07-28 
; AutoIt Version....: 3.2.12.1 
; 
; Parameter(s)......: $S_PATH - String containing the file path to return the property from. 
;      $S_PROPERTY - [optional] String containing the name of the property to return. (default = "") 
; Requirements(s)...: None 
; Return Value(s)...: Success: Returns a string containing the property value. 
;      If $S_PROPERTY is empty, an two-dimensional array is returned: 
;       $av_array[0][0] = Number of properties. 
;       $av_array[1][0] = 1st property name. 
;       $as_array[1][1] = 1st property value. 
;       $av_array[n][0] = nth property name. 
;       $as_array[n][1] = nth property value. 
;      Failure: Returns 0 and sets @error to: 
;      1 = The folder $S_PATH does not exist. 
;      2 = The property $S_PROPERTY does not exist or the array could not be created. 
;      3 = Unable to create the "Shell.Application" object $objShell. 
; 
; Author(s).........: - Simucal <[email protected]> 
;      - Modified by: Sean Hart <[email protected]> 
;      - Modified by: teh_hahn <[email protected]> 
; Company...........: None 
; URL...............: None 
; Note(s)...........: None 
;=============================================================================== 

Global $re = _FileGetProperty(@ScriptDir & '\1Tutorial - AutoItWiki1.pdf') 
If @error Then MsgBox(16, 'ERROR', 'Error: ' & @error & @CRLF & '1 = The folder $S_PATH does not exist.' & @CRLF & _ 
     '2 = The property $S_PROPERTY does not exist or the array could not be created.' & @CRLF & _ 
     '3 = Unable to create the "Shell.Application" object $objShell.') 
_ArrayDisplay($re) 

Func _FileGetProperty(Const $S_PATH, Const $S_PROPERTY = "") 
    If Not FileExists($S_PATH) Then Return SetError(1, 0, 0) 

    Local Const $S_FILE = StringTrimLeft($S_PATH, StringInStr($S_PATH, "\", 0, -1)) 
    Local Const $S_DIR = StringTrimRight($S_PATH, StringLen($S_FILE) + 1) 

    Local Const $objShell = ObjCreate("Shell.Application") 
    If @error Then Return SetError(3, 0, 0) 

    Local Const $objFolder = $objShell.NameSpace($S_DIR) 
    Local Const $objFolderItem = $objFolder.Parsename($S_FILE) 

    If $S_PROPERTY Then 
     For $i = 0 To 99 
      If $objFolder.GetDetailsOf($objFolder.Items, $i) = $S_PROPERTY Then Return $objFolder.GetDetailsOf($objFolderItem, $i) 
     Next 
     Return SetError(2, 0, 0) 
    EndIf 

    Local $av_ret[1][2] = [[1]] 
    For $i = 0 To 99 
     If $objFolder.GetDetailsOf($objFolder.Items, $i) Then 
      ReDim $av_ret[$av_ret[0][0] + 1][2] 
      $av_ret[$av_ret[0][0]][0] = $objFolder.GetDetailsOf($objFolder.Items, $i) 
      $av_ret[$av_ret[0][0]][1] = $objFolder.GetDetailsOf($objFolderItem, $i) 
      $av_ret[0][0] += 1 
     EndIf 
    Next 

    If Not $av_ret[1][0] Then Return SetError(2, 0, 0) 
    $av_ret[0][0] -= 1 

    Return $av_ret 
EndFunc ;==>_FileGetProperty 
+0

我沒有得到任何輸出。沒有輸出 –

+0

您是否更改了文件的路徑? (@ScriptDir&'\ Tutorial - AutoItWiki1.pdf')在這裏你需要把你的路徑。 – Xenobiologist

+0

是的,我願意。我應該彈出一個彈出窗口嗎? –