Adobe Acrobat DC on Powershell

Trebor905 11 Reputation points
2022-02-16T00:14:04.357+00:00

I am a newbie on this forum but has developed some simple powershell scripts for file manipulation in the past. Part my task right now is to combine .pdf files into a portfolio using adobe acrobat through powershelll. My code looks like this

cls
$app = New-Object -ComObject AcroExch.App
$Doc = New-Object -ComObject AcroExch.PDDoc
$JSODoc = New-Object -ComObject AcroExch.PDDoc

$doc.Create()
$JSODoc = $doc.GetJSObject()
$Coll = $JSODoc.app.newcollection

$Coll.importDataObject("C:\myfile_.pdf")

Unfortunately I am getting an error 'You cannot call a method on a null-valued expression.' on the importDataObject command. Does anyone have success on using this type of code? TIA!

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,588 questions
{count} vote

3 answers

Sort by: Most helpful
  1. MotoX80 34,856 Reputation points
    2022-02-16T00:32:54.23+00:00

    You can use Get-Member to show the available properties and methods of objects. Like this.

    For methods it shows what parameters each call accepts.

    PS C:\> $ObjFSO = New-Object -ComObject Scripting.FileSystemObject
    PS C:\> $ObjFSO | get-member
    
    
       TypeName: System.__ComObject#{2a0b9d10-4b87-11d3-a97a-00104b365c9f}
    
    Name                MemberType Definition
    ----                ---------- ----------
    BuildPath           Method     string BuildPath (string, string)
    CopyFile            Method     void CopyFile (string, string, bool)
    CopyFolder          Method     void CopyFolder (string, string, bool)
    CreateFolder        Method     IFolder CreateFolder (string)
    CreateTextFile      Method     ITextStream CreateTextFile (string, bool, bool)
    DeleteFile          Method     void DeleteFile (string, bool)
    DeleteFolder        Method     void DeleteFolder (string, bool)
    DriveExists         Method     bool DriveExists (string)
    FileExists          Method     bool FileExists (string)
    FolderExists        Method     bool FolderExists (string)
    GetAbsolutePathName Method     string GetAbsolutePathName (string)
    GetBaseName         Method     string GetBaseName (string)
    GetDrive            Method     IDrive GetDrive (string)
    GetDriveName        Method     string GetDriveName (string)
    GetExtensionName    Method     string GetExtensionName (string)
    GetFile             Method     IFile GetFile (string)
    GetFileName         Method     string GetFileName (string)
    GetFileVersion      Method     string GetFileVersion (string)
    GetFolder           Method     IFolder GetFolder (string)
    GetParentFolderName Method     string GetParentFolderName (string)
    GetSpecialFolder    Method     IFolder GetSpecialFolder (SpecialFolderConst)
    GetStandardStream   Method     ITextStream GetStandardStream (StandardStreamTypes, bool)
    GetTempName         Method     string GetTempName ()
    MoveFile            Method     void MoveFile (string, string)
    MoveFolder          Method     void MoveFolder (string, string)
    OpenTextFile        Method     ITextStream OpenTextFile (string, IOMode, bool, Tristate)
    Drives              Property   IDriveCollection Drives () {get}
    
    0 comments No comments

  2. Trebor905 11 Reputation points
    2022-02-16T01:35:05.103+00:00

    MotoX80 - Yeah I have tried that on but to no avail. the 'importDataObject' is part of the adobe acrobact dc automation which I believe is a java script embeded on the com object. I am not sure if I am doing the line '$Coll = $JSODoc.app.newcollection' correctly.

    I have tried searching the definition of the app.newcollection but it works only in VBA. Thanks!


  3. Kira 0 Reputation points
    2024-12-15T11:01:22.73+00:00

    I can't take total credit for this answer, but I figured I would submit this to try and help others in the search. I came across the answer at https://stackoverflow.com/questions/30400527/cannot-use-acrobat-xi-jsobject-from-powershell

    I couldn't get the jsoObject to function as it does in VBA or VBScript. In PowerShell you have to first get the PDDoc object, then get the jsObject, and then set a variable equal to Type::GetType of the jsObject.

    You can find more on type objects and invoking the members here.

    https://zcusa.951200.xyz/en-us/dotnet/api/system.type?view=net-9.0

    https://zcusa.951200.xyz/en-us/dotnet/api/system.type.invokemember?view=net-9.0

    Below is an example of getting the number of pages in a document.

    You can find references for all Adobe Acrobat javascript below.

    https://opensource.adobe.com/dc-acrobat-sdk-docs/library/jsapiref/toc.html

    $PDDoc  =  New-Object -ComObject  "AcroExch.PDDoc" 
    $PDDoc.Open( " ${env:USERPROFILE} \Desktop\Blank.pdf" )
    $jso  =  $gPDDoc.GetJSObject ()
    $jType  =  [Type] :: GetType( $jso )
    $numPages = $jType.InvokeMember("numPages", [System.Reflection.BindingFlags]::GetProperty, $null, $jsObject, $null)
    Write-Host $numPages
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.