Jump to content

Recommended Posts

Posted (edited)

This UDF is because I'm tired of trying to use UI Automation and Send to automate Adobe Acrobat. I often need to read the contents of PDFs and Acrobat is not easy to work with as a window.  The functions are based on the API Reference from Adobe located here.

Acrobat Pro is required for all functions.

It's very beta right now, but it still seems to work. Currently, the functions are based around page level manipulation of PDF documents: re-arranging, swapping, deleting, and moving pages as this is what I use the most.

Please feel free to request/suggest features! :)

 

 

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

You mean that you can't create a AcroExch.PDDoc without Pro? (_Acro_DocOpen would return error 1) I can't test this at work as I have Pro installed everywhere 😐

I guess I assumed that Reader would use the reference too. I might be able to work on this at home where I don't own Pro, but I'm not nearly as familiar with Reader


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites
Posted (edited)

TIL: Adobe Acrobat Pro is required for creating objects through AcroExch.App and AcroExch.PDDoc :(

There may be a way to automate Reader through DDE messages, but I'm unclear on what that is and if it's possible. Looking into it.

Edit: Read way too much on Reader...

Adobe Reader implements a different interface, resulting in a different set of functionality (Read: greatly reduced). I'm running out of ideas with Reader, but I'm pretty sure it implements IAcroAxDocShim (and a few more? C has multiple inheritance?) and can be created with AcroPDF.PDF or AcroPDF.PDF.1 - I think both, but it's something to do with the version of the type library you have.

I also found that not all of the methods are documented. There are three that I found: execCommand, postMessage, and messageHandler (get/set). I have no idea what they are/do. You'll see the methods if you find "Adobe PDF Reader" in the OLE/COM Object Viewer under "Object Classes\Grouped By Component Category\Automation Objects\" and right click to "View Type Information". From there, select 'Dispinterface IAcroAXDocShim' to see the methods available.

Aaand that's enough for now. I'll be dreaming in code tonight for sure.

Edited by seadoggie01

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Updated to version 0.0.0.2. I found that the JavaScript Object can be accessed and used, meaning that bookmarks are edit-able and so much more.

Added Functions:

  • _Acro_DocDisplay
  • _Acro_PageRotate
  • _Acro_AppShow
  • _Acro_DocJSObject
  • _Acro_DocBookmarkAdd

Fixed:

  • _Acro_PageCount - Actually returns page count now

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Bug: _Acro_PageGetText uses the wrong values to get the size of a page... dyslexia at work again...

Local $oRect = __Acro_Rect(0, $aPoint[0], 0, $aPoint[1])
;            Should be
Local $oRect = __Acro_Rect(0, $aPoint[1], 0, $aPoint[0])

Yeah... that's a good one. I'll try to update this soon. End of the month in accounting with a slow network. Nuff said? :D


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Updated to version 1.0.0.0

Added _Acro_DocAppend, which is my most used function now. I'm constantly combining PDFs at work using this. :)

If you have any suggestions or notice any issues, don't hesitate to let me know!


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

seadoggie01

I can't seem to get _Acro_PageMove to work.  I have Acrobat 8 Pro.  Here's the code.  I'm not sure why it's not working.  I'm using the iac_api_reference.pdf file for checking but also just created a simple 4 page pdf to try it with the same result.  Any help/hints appreciated.

;Acro.au3 test program
#AutoIt3Wrapper_run_debug_mode=Y    ; use this to debug in console window <--- LOOK


#include <Acro.au3>

MsgBox(0, "Test", "Testing Acro UDF")

;Reverse order of pages e.g. 1,2,3,...,n-1,n to n,n-1,...3,2,1  - ALWAYS work from bottom to top
;open it
;$sFullPath = "C:\Program Files (x86)\AutoIt3\AH Code\PDF Processing\Acro UDF\Reverse test document pp 1-4.pdf"
$sFullPath = "C:\Program Files (x86)\AutoIt3\AH Code\PDF Processing\Acro UDF\iac_api_reference - WORKING COPY.pdf"
$oPdDoc = _Acro_DocOpen($sFullPath)
If $oPdDoc = False Then
    If @error = 1 Then MsgBox(0, "Error", "Could not find file: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Document not opened: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document opened: '" & $sFullPath &"'")
EndIf


;get page count so we do all pages
$iPages = _Acro_PageCount($oPdDoc)
If $iPages = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Can't get number of pages in: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", $iPages & " pages in document: '" & $sFullPath &"'")
EndIf

$j = 1  ;init value
For $i = $iPages to 2 Step -1   ;skip last move
    $iFromPage = $iPages    ;always move from last page
    $iToPage = $j
    $x = _Acro_PageMove($oPdDoc, $iFromPage, $iToPage)
    If $x = False Then
        If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
        If @error = 2 Then MsgBox(0, "Error", "Unable to move page: " & $iFromPage & " to page: " & $iToPage & " in: '" & $sFullPath &"'")
    Else
        MsgBox(0, "Okay", "Old Page: " & $iFromPage & " moved to New Page: " & $iToPage & " in document: '" & $sFullPath &"'")
    EndIf
    $j = $j + 1     ;increment to next $iToPage
Next
MsgBox(0, "Status", "Reversed pages in: '" & $sFullPath &"'")


;be polite and close document
;close document
$x = _Acro_DocClose($oPdDoc)
If $x = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oDoc isn't an AV/PD Doc object: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document closed: '" & $sFullPath &"'")
EndIf


Exit

 

Reverse test document pp 1-4.pdf

Share this post


Link to post
Share on other sites

The pages in a PDF document are 0-based, so try changing this:

$x = _Acro_PageMove($oPdDoc, $iFromPage, $iToPage)
; To this...
$x = _Acro_PageMove($oPdDoc, $iFromPage - 1, $iToPage - 1)

You'll also want to call _Acro_DocSave before closing the document, otherwise your changes won't be saved.

And finally, you do want to move that first page to the end if you're truly trying to reverse the whole document, otherwise you'll end up with pages like (1, 4, 3, 2) instead :)

(I'll make sure to add a note to the UDF on the next release about 0 or 1 based pages! Sorry for the confusion there :()


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

seadoggie01,

Thanks- the Zero based info cleared up the loop and it reverses.  Will try out _Acro_DocSave tomorrow.

 

Share this post


Link to post
Share on other sites

seadoggie01,

Okay more confusion on my part.  I indeed get 1,4,3,2, however I don't understand why.

Here's what I thought the move sequence would look like.

original order    after 1st move     after 2nd move        after 3rd move

1                                   4                             4                               4

2                                   1                             3                               3

3                                   2                             1                               2

4                                   3                             2                               1

 

Here's the code.  (Also not sure why _Acro_DocSave isn't saving file as "-saved.pdf")

;
;Acro.au3 test program
#AutoIt3Wrapper_run_debug_mode=Y    ; use this to debug in console window <--- LOOK

#include <Acro.au3>

MsgBox(0, "Test", "Testing Acro UDF")

;Reverse order of pages e.g. 1,2,3,...,n-1,n to n,n-1,...3,2,1  - ALWAYS work from bottom to top
;from seadoggie01 PDF pages are Zero based so for n pages we have 0,1,2,...,n-2,n-1 to n-1,n-2,...,2,1,0
;open it
$sFullPath = "C:\Program Files (x86)\AutoIt3\AH Code\PDF Processing\Acro UDF\Reverse test document pp 1-4.pdf"
$oPdDoc = _Acro_DocOpen($sFullPath)
If $oPdDoc = False Then
    If @error = 1 Then MsgBox(0, "Error", "Could not find file: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Document not opened: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document opened: '" & $sFullPath &"'")
EndIf


;get page count so we do all pages      ;page count is Zero based!! SO it goes from 0 to $iPage-1
$iPages = _Acro_PageCount($oPdDoc)
If $iPages = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Can't get number of pages in: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", $iPages & " pages in document: '" & $sFullPath &"'")
EndIf

$j = 1  ;init value
For $i = $iPages to 2 Step -1   ;skip last move
    $iFromPage = $iPages    ;always move from last page
    $iToPage = $j
    MsgBox(0, "Debug", "moving["& $iFromPage - 1 &"] to ["& $iToPage -1 & "]")
    $x = _Acro_PageMove($oPdDoc, $iFromPage - 1, $iToPage -1)   ;-1 because Zero based
    If $x = False Then
        If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
        If @error = 2 Then MsgBox(0, "Error", "Unable to move page: " & $iFromPage & " to page: " & $iToPage & " in: '" & $sFullPath &"'")
    Else
        MsgBox(0, "Okay", "Old Page: " & $iFromPage & " moved to New Page: " & $iToPage & " in document: '" & $sFullPath &"'")
    EndIf
    $j = $j + 1     ;increment to next $iToPage
Next
MsgBox(0, "Status", "Reversed pages in: '" & $sFullPath &"'")


;save the document
$sFullPath = StringTrimRight($sFullPath, 4) & "-saved.pdf"  ;name it "-saved.pdf"
MsgBox(0, "Debug", "$sFullPath = '" & $sFullPath &"'")
$x = _Acro_DocSave($oPdDoc, $sFullPath)
MsgBox(0, "Debug", "$x = '" & $x &"'")
If $x = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Document not saved: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document saved: '" & $sFullPath &"'")
EndIf


;be polite and close document
;close document
$x = _Acro_DocClose($oPdDoc)
If $x = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oDoc isn't an AV/PD Doc object: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document closed: '" & $sFullPath &"'")
EndIf


Exit

 

Share this post


Link to post
Share on other sites

Because the pages are 0-based, you'll need to subtract 1 from the number of pages in your For loop (For $i = $iPages -1 To 2 Step -1)

What do you mean by "_Acro_DocSave isn't saving file as "-saved.pdf"? Is it returning an error? 

There's an error in _Acro_DocSave: it won't return True if there's not an error. I'll add that to my ToDo-list when I get back from vacation :) 


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

Enjoy your vacation - ping when you get back from vacation - sorry to bother you now. 

Share this post


Link to post
Share on other sites

@ahha I got back from vacation and completely forgot about this, my sincere apologies. Fortunately, I think I may have solved your _Acro_DocSave issue... there's a minor note in the documentation that explains the PDSaveLinearized flag. In order to use the flag, it appears that changes to the document cannot be made. So, try saving your document like this instead:

_Acro_DocSave($oPdDoc, $sFullPath, $PDSaveFull)

This overrides my default value of $PDSaveFull + $PDSaveLinearized, which worked for me when my documents weren't saving. :) I'll add a note about the flag and the lack of an error when using the flag as well as change the default in my next release. Hopefully I can get that out next week.


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

seadoggie01,

$PDSaveFull did the trick - thanks.

Also trying to understand the move routine.  Can you tell me why an extra move is required after the 3rd - see July 19 sequence.

Thanks

Share this post


Link to post
Share on other sites

seadoggie01,

How do I add a sub bookmark?

Like this

Page 1   <-- main top level bookmark

     Page 2   <-- 1st sub bookmark

          Page 3   <-- 2nd sub bookmark

                Page 4   <-- 3rdsub bookmark

Is it the $oParent somehow?

Here's what I've got.

Thanks

;
;v1k - trying to add bookmarks
;v1j - messing around
;v1i - updated _Acro_DocSave($oPdDoc, $sFullPath, $PDSaveFull) see https://www.autoitscript.com/forum/topic/202026-acroau3-udf/?tab=comments#comment-1449581
;Acro.au3 test program
#AutoIt3Wrapper_run_debug_mode=Y    ; use this to debug in console window <--- LOOK

#include <Acro.au3>

MsgBox(0, "Test", "Testing Acro UDF")

;open it
$sFullPath = "C:\Program Files (x86)\AutoIt3\AH Code\PDF Processing\Acro UDF\Bookmark test document pp 1-4.pdf"
$oPdDoc = _Acro_DocOpen($sFullPath)
If $oPdDoc = False Then
    If @error = 1 Then MsgBox(0, "Error", "Could not find file: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Document not opened: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document opened: '" & $sFullPath &"'")
EndIf


;get page count so we do all pages      ;page count is Zero based!! SO it goes from 0 to $iPage-1
$iPages = _Acro_PageCount($oPdDoc)
If $iPages = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Can't get number of pages in: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", $iPages & " pages in document: '" & $sFullPath &"'")
EndIf

;v1k - add bookmarks
;For $i = 0 to $iPages-1    ;zero based ;Note works BUT with this 0,...,n-1 the order in the BM panel is n-1,..0 - really want it reversed so start at bottom and work to top BM
For $i = $iPages-1  to 0 step -1    ;zero based
    $sName = $i + 1     ;just bookmark with the page number
    $iPage = $i
    $iIndex = 0         ;zero based - this shifts the BM panel presentation order  ;$iIndex=0 gives 1,2,3,4     ;$iIndex=1 gives 4,1,2,3
    $oParent = Default  ;? (top level) - not sure how to declare an object parent for a bookmark (page?)
    $x = _Acro_DocBookmarkAdd($oPdDoc, $sName, $iPage, $iIndex, $oParent)
    MsgBox(0, "Debug", "$x = '" & $x &"'" & " @error = '" & @error & "'")
    If $x = False Then
        If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
        If @error = 2 Then MsgBox(0, "Error", "_Acro_DocJSObject returned an error: @extended set to it's @error." & @CRLF & "@extended = '"& @extended & "'")
    Else
        MsgBox(0, "Okay", "Page: " & $i & " should be bookmarked with '" & $sName &"'") ;skipped because $x='' and @error ='0'
    EndIf
Next


;save the document
$sFullPath = StringTrimRight($sFullPath, 4) & "-saved.pdf"  ;name it "-saved.pdf"
MsgBox(0, "Debug", "$sFullPath = '" & $sFullPath &"'")
$x = _Acro_DocSave($oPdDoc, $sFullPath, $PDSaveFull)    ;v1i using $PDSaveFull
MsgBox(0, "Debug", "$x = '" & $x &"'")
If $x = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oPdDoc isn't a PDDoc object: '" & $sFullPath &"'")
    If @error = 2 Then MsgBox(0, "Error", "Document not saved: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document saved: '" & $sFullPath &"'")
EndIf


;be polite and close document
;close document
$x = _Acro_DocClose($oPdDoc)
If $x = False Then
    If @error = 1 Then MsgBox(0, "Error", "$oDoc isn't an AV/PD Doc object: '" & $sFullPath &"'")
Else
    MsgBox(0, "Okay", "Document closed: '" & $sFullPath &"'")
EndIf


Exit

 

Bookmark test document pp 1-4.pdf

Share this post


Link to post
Share on other sites

I haven't actually implemented that yet :D I was hoping that the result of _Acro_DocBookmarkAdd would return the bookmark object, but it appears to return nothing or something else undocumented that relates to JavaScript. If you check out the methods inside of that function, you'll see that I'm accessing the JavaScript object of the document to get access to bookmarks. I haven't had much time to play around with them yet, so currently there isn't a built-in way to manipulate them. 

You can get access to the JavaScript object through _Acro_DocJSObject and manipulate it through something like this (untested code):

Local $oPdDoc = _Acro_DocOpen("file")
Local $oJS    = _Acro_DocJSObject($oPdDoc)  ; $oJS is now equal to "this" in the JavaScript reference

; Get the root level bookmark, an imaginary bookmark that doesn't display, but holds all other top level bookmarks
Local $oRootBkmk = $oJS.bookmarkRoot

; Get a 1-d array of child bookmark objects
Local $aoChildBkmk = $oRootBkmk.children

For $i=0 To Ubound($aoChildBkmk) - 1
    
    ; Print the name
    ConsoleWrite($aoChildBkmk[$i].name)
    
    ; Add a child bookmark to it (Name, Javascript to execute when clicked, index of parent)
    $aoChildBkmk[$i].createChild($aoChildBkmk[$i].name & "'s - Child", "this.pageNum++", 0)
Next

I haven't added much code related to bookmarks yet, but I'm happy to add some when I get some time, let me know if you're interested :) I've been thinking about doing this lately anyways.


All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Share this post


Link to post
Share on other sites

seadoggie01,

Yes I would like bookmark manipulation.

My JS skills are lacking however I'll give your code a try.

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...