locked
Acrobat and Powershell -- the basics of opening and working with PDFs with Powershell RRS feed

  • Question

  •  I've been refreshing/expanding my understanding of how to use the Acrobat Interapplication Communication OLE Automation in Powershell to work with PDFs.

    I'd appreciate any feedback on the following code, which I view as the basics of creating the App, PDDoc, and AVDoc COM objects needed to open, work with, and clean up after working with a PDF, viz., are there better/more efficient ways to accomplish the same end?
    # declare a COM object for the Acrobat application
    # this doesn't show up in the Task Manager
    $app = New-Object -ComObject AcroExch.App
    
    # declare a COM object for PDDoc
    $PDDoc = New-Object -ComObject AcroExch.PDDoc
    
    
    # open a PDF
    $PDDoc.Open('\\faxes\christianBahnsen\temp\FaxReceived_2018-11-07-06;27;47.6330_a1e78779-9333-4f0c-b4ea-44196eef3ebf.pdf')
    
    
    # this displays the document in the acrobat instance and creates an AVDoc COM object
    # at this point, acrobat shows up in the task manager
    $AVDoc = $PDDoc.OpenAVDoc("")
    
    # now I can use the FindText method
    $AVDoc.FindText("total",0,1,1)
    
    # Gets the number of open AcroExch.AVDoc objects. 
    $app.GetNumAVDocs()
    
    # this hides the acrobat instance if it's visible
    # acrobat disappears from the Task Manager
    $app.Hide()
    
    # housekeeping on the way out
    $app.CloseAllDocs()
    
    # supposedly "an application must explicitly close any AVDoc that it opens by calling AVDoc.Close", 
    # but I'm getting a False in Powershell that makes me think this step isn't working
    $AVDoc.Close(0)
    
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($app)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($AVDoc)
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($PDDoc)
    
    Remove-Variable app, AVDoc, PDDoc


    • Edited by jrv Friday, November 16, 2018 5:35 PM fixed format
    Friday, November 16, 2018 4:49 PM

Answers

  • jrv,

    Thanks for your forbearance and for reformatting the post.

    I work at a DoD site so disabled functionality (i.e., IE) is a daily obstacle to working efficiently.

    I wanted to make sure I have a solid foundation before expanding this code beyond a clean open and close. I find the Acrobat OLE documentation pretty thin and lacking examples as compared to almost any topic for MSFT products.

    Chris

    It is almost never necessary to force garbage collection I  PowerShell.  PS and the OS do that automatically when needed.

    Releasing COM objects only works if you release all references to the object and to all refrences to the object.

    Keep an array of all objects as you create them and loop on each object in the array until all references are removed.

    While([System.Runtime.Interopservices.Marshal]::ReleaseComObject($app)){'released reference to $app'}

    The release will return true until there are no more references.

    See: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.releasecomobject?view=netframework-4.7.2#System_Runtime_InteropServices_Marshal_ReleaseComObject_System_Object_


    \_(ツ)_/

    Monday, November 19, 2018 1:56 PM

All replies

  • You cannot post your question inside a codebox.  It is unreadable.

    For help with Adobe objects please post to the correct Adobe developer forum.  This is not an Adobe support forum.


    \_(ツ)_/

    Friday, November 16, 2018 5:11 PM
  • Go ahead and delete this post then.

    Maybe it's the browser we use here (IE 11) but I can't paste into this editor.

    Thanks

    Friday, November 16, 2018 5:31 PM
  • Type your question first then open the codebox and paste the code.  We use IE 11 with no  issues.  You may have issues if your account has not been validated,

    I will fix your original post (using IE 1) but the issue of Adobe still stands. Only an Adobe forum can help you with this.

    As for the PowerShell components of your code they are the best you can do.  I see no way to improve on a simple set of steps that basically do nothing.


    \_(ツ)_/


    • Edited by jrv Friday, November 16, 2018 5:37 PM
    Friday, November 16, 2018 5:35 PM
  • It may be just me, but I run into more trouble than not when trying to explicitly ReleaseComObject in PowerShell. I just skip to removing variables followed by a manual garbage collection call these days.

    The Acrobat part is off-topic, but you bringing its API to my attention has personally benefitted me. As a thanks for that I took a look at the Close that keeps giving false and it turns out the API says AVDoc.Close always returns -1 so I think you're fine on that particular step.



    • Edited by A'ziz Sunday, November 18, 2018 6:21 AM
    Sunday, November 18, 2018 6:18 AM
  • A'ziz, thanks for the reply.

    Perhaps I should have started the post "Powershell and Acrobat" because it involves both.  I also posted the same question in the Acrobat DC SDK forum to hedge my bets, but haven't had any replies there.

    I haven't had any issues with ReleaseComObject.  I get a return value of 0 for each call.

    When you refer to "manual garbage collection", are you using?:

    [GC]::Collect()

    Thanks for the tip re the Close method.

    Chris

    Monday, November 19, 2018 12:50 PM
  • jrv,

    Thanks for your forbearance and for reformatting the post.

    I work at a DoD site so disabled functionality (i.e., IE) is a daily obstacle to working efficiently.

    I wanted to make sure I have a solid foundation before expanding this code beyond a clean open and close. I find the Acrobat OLE documentation pretty thin and lacking examples as compared to almost any topic for MSFT products.

    Chris

    Monday, November 19, 2018 1:00 PM
  • jrv,

    Thanks for your forbearance and for reformatting the post.

    I work at a DoD site so disabled functionality (i.e., IE) is a daily obstacle to working efficiently.

    I wanted to make sure I have a solid foundation before expanding this code beyond a clean open and close. I find the Acrobat OLE documentation pretty thin and lacking examples as compared to almost any topic for MSFT products.

    Chris

    It is almost never necessary to force garbage collection I  PowerShell.  PS and the OS do that automatically when needed.

    Releasing COM objects only works if you release all references to the object and to all refrences to the object.

    Keep an array of all objects as you create them and loop on each object in the array until all references are removed.

    While([System.Runtime.Interopservices.Marshal]::ReleaseComObject($app)){'released reference to $app'}

    The release will return true until there are no more references.

    See: https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.releasecomobject?view=netframework-4.7.2#System_Runtime_InteropServices_Marshal_ReleaseComObject_System_Object_


    \_(ツ)_/

    Monday, November 19, 2018 1:56 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    LEE


    Just do it.

    Tuesday, November 27, 2018 2:45 AM