4

I have Acrobat XI Pro installed on my machine.

I'm trying to write PowerShell code that will extract pages from PDF documents using techniques such as the ones at https://groups.google.com/forum/#!topic/comp.text.pdf/DNtcbUgjas4 and Convert pdf to Word document

When I invoke GetJSObject() on the output from GetPDDoc() I get an System._ComObject that doesn't expose any of the methods used in the various pieces of sample code I have found:

PS> $JavaScriptObject
System.__ComObject

PS> $JavaScriptObject.GetType()
Value does not fall within the expected range.
At line:1 char:1
+ $JavaScriptObject.GetType()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException


PS> $JavaScriptObject.numPages -eq $null
True

Any clue?

Note: the exact same logic works in VBS using the code from https://groups.google.com/forum/#!topic/comp.text.pdf/DNtcbUgjas4

Community
  • 1
  • 1
sba923
  • 537
  • 1
  • 5
  • 9

2 Answers2

3

So, this is an old question, and you've probably either long since moved on from this, or figured it out. I was trying to do some tasks in PowerShell involving Acrobat automation, ran into the same issue, and figured a trusty Google search would solve my issue for me. Unfortunately, yours was the only relevant result that I could find and you never seemed to have gotten an answer.

In case anyone else winds up here, here is what ended up working for me, in this instance. I was doing this on a Windows 10 machine, with PowerShell v5. I strongly suspect the issue is the way the com-object interfaces with PowerShell v5. If I opened a command line prompt and initiated "powershell -version 2", the $jsObject was fine, and I can invoke extractPages, numPages, etc. from the object to my heart's content, without the "does not fall within expected range" error being returned by GetType(). I know you're not supposed to answer based on opinion, but that's my suspicion, and would explain (to me) why loading an older version of powershell works. shrugs. I could be wrong, and the issue lies somewhere else, but hopefully this helps someone, somewhere.

Oh, and btw I believe you need to change the way you invoke methods from the $jsObject, and use reflection. A brief snippet example:

    $jType = $jsObject.GetType()
    $extractPagesBFs = [System.Reflection.BindingFlags]"InvokeMethod","Public","Instance"
    $jType.InvokeMember("extractPages", $extractPagesBFs, $null, $jsObject, $extractPagesParam)

I was never able to get $jsObject.<function> to work (maybe I missed something though...). It is unclear to me why vbs can directly access the functions like that, such as in the example you linked.

Chris
  • 31
  • 2
0

I found the fix. $t = $j.GetType(); replace with $t = [Type]::GetType($j); https://github.com/ohtake/WindowsPowerShell/blob/master/Acrobat.txt#L18

Reference https://dy100ms.hatenadiary.jp/entry/2022/01/22/000000