メモ
- 使用するオブジェクト
- PDDoc と JavaScript object
- ページボックスの座標
- InvokeMember で JavaScript object の getPageBox メソッド
- CSVへの変換
- PSCustomObject の List を ConvertTo-Csv コマンドレット
参考資料リンク
使い捨てスクリプト
Acrobatがバックグラウンドプロセスとして残る場合があるので、タスクマネージャーを確認して終了させてください。
# PowerShell 5.1, Acrobat Standard DC (2022年11月頃), Windows 11 # [ファイル名] # "%USERPROFILE%\Desktop\GetPdfPageSize.ps1" # [エンコード] # UTF-8 (BOM付き) # [ショートカット(.lnk)] # リンク先(T): # PowerShell.exe -NoLogo -NoExit -NoProfile -ExecutionPolicy RemoteSigned -File "%USERPROFILE%\Desktop\GetPdfPageSize.ps1" # 作業フォルダー(S): # "" using namespace System using namespace System.Collections.Generic using namespace System.Reflection Set-StrictMode -Version Latest $VerbosePreference = "Continue" if ($args.Length -eq 0) { throw '$args.Length -eq 0' } Set-Location -LiteralPath (Split-Path -LiteralPath $args[0]) $docPageSizes = [List[PSCustomObject]]::new() $BndFlgInvM = [BindingFlags]::InvokeMethod # PDDocオブジェクトを作成 $pdDoc = New-Object -ComObject AcroExch.PDDoc -Strict for ($i = 0; $i -lt $args.Length; $i++) { $pdfFilePath = $args[$i] # PDFファイルを開く $ret = $pdDoc.Open($pdfFilePath) Write-Verbose "`$pdDoc.Open(`"$pdfFilePath`") ..... $ret" # JavaScript object を取得 $jso = $pdDoc.GetJSObject() # ページ数を取得 $numPages = $pdDoc.GetNumPages() for ($j = 0; $j -lt $numPages; $j++) { # ページの回転を取得 $rot = [int][Type]::GetType($jso).InvokeMember( "getPageRotation", $BndFlgInvM, $null, $jso, $j ) # CropBoxの座標を取得 $cBox = [Type]::GetType($jso).InvokeMember( "getPageBox", $BndFlgInvM, $null, $jso, @("Crop", $j) ).ForEach({$_ * 25.4 / 72}) $xllC = $cBox[0]; $yllC = $cBox[3]; $xurC = $cBox[2]; $yurC = $cBox[1]; # MediaBoxの座標を取得 $mBox = [Type]::GetType($jso).InvokeMember( "getPageBox", $BndFlgInvM, $null, $jso, @("Media", $j) ).ForEach({$_ * 25.4 / 72}) $xllM = $mBox[0]; $yllM = $mBox[3]; $xurM = $mBox[2]; $yurM = $mBox[1]; # ArtBoxの座標を取得 $aBox = [Type]::GetType($jso).InvokeMember( "getPageBox", $BndFlgInvM, $null, $jso, @("Art", $j) ).ForEach({$_ * 25.4 / 72}) $xllA = $aBox[0]; $yllA = $aBox[3]; $xurA = $aBox[2]; $yurA = $aBox[1]; # TrimBoxの座標を取得 $tBox = [Type]::GetType($jso).InvokeMember( "getPageBox", $BndFlgInvM, $null, $jso, @("Trim", $j) ).ForEach({$_ * 25.4 / 72}) $xllT = $tBox[0]; $yllT = $tBox[3]; $xurT = $tBox[2]; $yurT = $tBox[1]; # BleedBoxの座標を取得 $bBox = [Type]::GetType($jso).InvokeMember( "getPageBox", $BndFlgInvM, $null, $jso, @("Bleed", $j) ).ForEach({$_ * 25.4 / 72}) $xllB = $bBox[0]; $yllB = $bBox[3]; $xurB = $bBox[2]; $yurB = $bBox[1]; $docPageSizes.Add([PSCustomObject]@{ FileName = Split-Path -Path $pdfFilePath -Leaf; Page = $j + 1; Rotation = $rot; WidthCrop = $xurC - $xllC; HeightCrop = $yurC - $yllC; xllMedia = $xllM; xllCrop = $xllC; xllBleed = $xllB; xllTrim = $xllT; xllArt = $xllA; yllMedia = $yllM; yllCrop = $yllC; yllBleed = $yllB; yllTrim = $yllT; yllArt = $yllA; xurMedia = $xurM; xurCrop = $xurC; xurBleed = $xurB; xurTrim = $xurT; xurArt = $xurA; yurMedia = $yurM; yurCrop = $yurC; yurBleed = $yurB; yurTrim = $yurT; yurArt = $yurA; }) } # 変数を完全に削除して参照を削除 Remove-Variable jso # PDFファイルを閉じる $ret = $pdDoc.Close() Write-Verbose "`$pdDoc.Close() ..... $ret" } Remove-Variable pdDoc # PSCustomObjectのリストをCSVに変換 $csv = $docPageSizes | ConvertTo-Csv -NoTypeInformation # CSVをクリップボードにコピー $csv | Set-Clipboard # グリッド ビュー に表示 $docPageSizes | Out-GridView # ガベージ コレクション を直ちに強制実行 [GC]::Collect() [GC]::WaitForPendingFinalizers()