xlogI125’s blog

パソコン作業を効率化したい

Acrobat IAC練習 JavaScript デバッガー を開く

メモ

JavaScript デバッガーを表示するのにショートカットキーや しおり を使用してはいけない場合、OLEでJSObjectを経由して console.show() を行う方法を考える。

参考資料リンク

使い捨てスクリプト

PowerShell

  • Acrobatがバックグラウンド プロセスに残った場合は、タスク マネージャーで終了させてください。
  • エラー処理やCOMオブジェクトの解放を気にしてないので練習以外での実行は危険。
Type.InvokeMember
  • PDDoc.GetJSObject に関する部分に Type.InvokeMember を使う。
# PowerShell 5.1, Windows 11
# Acrobat Pro DC  2022年1月頃のバージョン

using namespace System.Reflection

Set-StrictMode -Version Latest

$gPDDoc = New-Object -ComObject "AcroExch.PDDoc"
$gPDDoc.Open("${env:USERPROFILE}\Desktop\白紙.pdf")
$gPDDoc.OpenAVDoc("").BringToFront()

$jso = $gPDDoc.GetJSObject()

$tJso = [Type]::GetType($jso)
$jsoCon = $tJso.InvokeMember(
    "console", 
    [BindingFlags]::GetProperty, 
    $null, 
    $jso, 
    $null
)
$tJso = $null

$tJsoCon = [Type]::GetType($jsoCon)
$tJsoCon.InvokeMember(
    "show", 
    [BindingFlags]::InvokeMethod, 
    $null, 
    $jsoCon, 
    $null
)
$tJsoCon.InvokeMember(
    "println", 
    [BindingFlags]::InvokeMethod, 
    $null, 
    $jsoCon, 
    @("/* 実行方法はテキストを範囲選択して Ctrl + Enter */")
)
$tJsoCon = $null

$jsoCon = $null
$jso = $null
$gPDDoc = $null

[System.GC]::Collect()
Interaction.CallByName
  • PDDoc.GetJSObject に関する部分を Add-Type -Language VisualBasic とする。
# PowerShell 5.1
# Windows 11, Acrobat Pro DC  2022年1月頃のバージョン

Set-StrictMode -Version Latest

$gPDDoc = New-Object -ComObject "AcroExch.PDDoc"

# 既存のPDFファイルを開く
$gPDDoc.Open("${env:USERPROFILE}\Desktop\白紙.pdf")

# PDFファイルを表示する
$gPDDoc.OpenAVDoc("").BringToFront()

# Visual Basicを使う
Add-Type @"
Public Module MyModule
  Public Sub JSObjectInterface(ByVal PDDoc As Object)
    Dim jso As Object
    jso = PDDoc.GetJSObject()
    jso.console.show()
    jso.console.println("/* 実行方法はテキストを範囲選択して Ctrl + Enter */")
  End Sub
End Module
"@ -Language VisualBasic

[MyModule]::JSObjectInterface($gPDDoc)

$gPDDoc = $null

[System.GC]::Collect()
# PowerShell 5.1, Windows 11
# Acrobat Pro DC  2022年1月頃のバージョン

using assembly Microsoft.VisualBasic

Set-StrictMode -Version Latest

$gPDDoc = [Microsoft.VisualBasic.Interaction]::CreateObject("AcroExch.PDDoc")

# $gPDDoc = [Activator]::CreateInstance([Type]::GetTypeFromProgID("AcroExch.PDDoc"))

$gPDDoc.Open("${env:USERPROFILE}\Desktop\白紙.pdf")
$gPDDoc.OpenAVDoc("").BringToFront()

$jso = $gPDDoc.GetJSObject()

$con = [Microsoft.VisualBasic.Interaction]::CallByName(
  $jso, 
  "console", 
  [Microsoft.VisualBasic.CallType]::Get, 
  $null
)

[Microsoft.VisualBasic.Interaction]::CallByName(
  $con, 
  "show", 
  [Microsoft.VisualBasic.CallType]::Method, 
  $null
)

[Microsoft.VisualBasic.Interaction]::CallByName(
  $con, 
  "println", 
  [Microsoft.VisualBasic.CallType]::Method, 
  @("/* 実行方法はテキストを範囲選択して Ctrl + Enter */")
)

$con = $null
$jso = $null
$gPDDoc = $null

[System.GC]::Collect()

VBA

  • VBAによる実装では、VBAを開くまでに手間がかかる。
' Excel 2019, Windows 11
' Acrobat Pro DC  2022年1月頃のバージョン

' 参照設定
' Acrobat
' (Adobe Acrobat 10.0 Type Library)

Option Explicit

Public Sub Main()
  Dim gPDDoc As Acrobat.AcroPDDoc
  Dim jso As Object

  Set gPDDoc = VBA.Interaction.CreateObject("AcroExch.PDDoc")

  ' 既存のPDFファイルを開く
  gPDDoc.Open VBA.Interaction.Environ("USERPROFILE") & "\Desktop\白紙.pdf"

  ' PDFファイルを表示する
  gPDDoc.OpenAVDoc("").BringToFront

  Set jso = gPDDoc.GetJSObject()
  jso.console.Show ' Visual Basic Editor が show を Show に変換してしまう
  jso.console.println "/* 実行方法はテキストを範囲選択して Ctrl + Enter */"

  Set jso = Nothing
  Set gPDDoc = Nothing
End Sub