Jump to content

Acrobat InterApplication Communication API


JohnBailey
 Share

Recommended Posts

I just started this UDF. Any suggestions, critiques, geek-slaps, or improvements are VERY welcome and requested! :)

This is VERY helpful for me! In lots of different ways! :) I just wanted to share. In the tech docs for the AICAPI, there is explanation on controlling the buttons and so forth in Acrobat and even using it to embed it in a GUI.

UPDATE HISTORY:

1.0.0 - initial

1.0.1 - fixed obj.exit issue by adding obj.hide

1.2.0 - new func _AcrobatIC_JSGetObject

new func _AcrobatIC_DocGetWinTitle

;===============================================================================
;
; Function Name:    _AcrobatIC_Create()
; Description:    Create Acrobat Window
; Parameter(s):  
;                   $aic_Show               - Optional: specifies whether or not the Acrobat Window is visible
;                                               True  = (Default) Show window
;                                               False = Leave window hiden
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns the AcroExch.App Object Ref 
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = Object not created
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AppCloseAllDocs()
; Description:    Closes all open acrobat docs
; Parameter(s):  
;                   $aic_obj        -   ref     - The obj ref var
;                   $aic_Exit       -   boolean - Optional: specifies whether or not to exit Acrobat App
;                                                   False = (Default) Leave open Acrobat App
;                                                   True  = Exit/Close Acrobat App
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns -1
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;                               - 1 = Did not close
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AppExit()
; Description:      Exits Acrobat app
; Parameter(s):  
;                   $aic_obj        -   ref     - The obj ref var
;                   $aic_OnlyIfNoDocs - boolean - Optional: specifies whether to close if only no docs are open in the App
;                                                   False = (Default) Exit regardless
;                                                   True  = Exit/Close Acrobat App only if docs are not open
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns -1 = "This includes closing any open documents, releasing OLE references, and finally exiting the application."
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;                               - 1 = couldn't close because documents were still open (only if $aic_OnlyIfNoDocs=True)
;                   @Extended   - 
; CallTip:
; Note:             "This method does not work if the application is visible (if the user is in control of the application). 
;                       In such cases, if the Show method had previously been called, you can call Hide and then Exit." This is why the obj.hide is used first
;                       before the obj.exit.
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AVDocCreate()
; Description:      Creates AVDoc
; Parameter(s):  
;                   $aic_obj        -   ref     - The obj ref var
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns the AVDoc Obj Ref
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AVDocOpenNew()
; Description:      Opens a PDF
; Parameter(s):  
;                   $aic_oApp   -   ref     - The obj ref var (Must be an App Obj)
;                   $aic_FiletoOpen -   string  - specifies what file to open
;                   $aic_DocTitle   -   string  - Optional: specifies the document title
;                                                   ''    = (Default) blank like this means the file name and location will be used as the name
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns the AVDoc Obj Ref
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = $aic_oApp is not an obj
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AVDocClose()
; Description:      Closes a PDF
; Parameter(s):  
;                   $aic_oAVDoc     -   ref     - The obj ref var (Must be a AVDoc Obj)
;                   $aic_FiletoOpen -   string  - specifies what file to open
;                   $aic_DocTitle   -   string  - Optional: specifies the document title
;                                                   ''    = (Default) blank like this means the file name and location will be used as the name
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns 1
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;                               - 1 = obj is not a valid AcroExch.AVDoc obj
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_AVDocPrint()
; Description:      Prints a PDF
; Parameter(s):  
;                   $aic_obj        -   ref     - The obj ref var (Must be a AVDoc Obj)
;                   $aic_nFirstPage -   number  - Optional: specifies which page to start printing
;                                                   0 = (Default) The first page in a PDDoc object is page 0.
;                   $aic_nFirstPage -   number  - Optional: specifies which page to end printing
;                                                   -1 = (Default) The last page in the document will be used.
;                   $aic_nPSLevel   -   number  - Optional: specifies which PostScriptLevel to use
;                                                   2 = (Default) PostScript Level 2 operators are used.
;                                                   3 = PostScript Language Level 3 operators are also used.
;                   $aic_nPSLevel   -   number  - Optional: specifies to use binary data included in the PostScript program
;                                                   0 = (Default) all data is encoded as 7-bit ASCII.
;                                                   greater than zero = binary data may be included in the PostScript program.
;                   $aic_bShrinkToFit   number  - Optional: specifies whether the page content should be shrunk (if necessary) to fir within the print area
;                                                   0 = (Default) no shrink
;                                                   greater than zero = the page is shrunk (if necessary) to fit within the imageable area of the printed page.
;
;                   --- PostScript printing Only --- Below params are only ---
;                   $aic_bReverse   -   number  - Optional: specifies whether the to reverse the print order (Post
;                                                   0 = (Default) no
;                                                   greater than zero = print the pages in reverse order. If false, print the pages in the regular order.
;                   $aic_bFarEastFontOpt -number- Optional: specifies whether the destination printer has multibyte fonts
;                                                   0 = (Default) no 
;                                                   greater than zero = the destination printer has multibyte fonts.
;                   $aic_bEmitHalftones -number - Optional: specifies whether the page content should be shrunk (if necessary) to fir within the print area
;                                                   0 = (Default) no
;                   --- End PostScriptOnly ----- end
;
;                                                   greater than zero = emit the halftones specified in the document
;                   $aic_iPageOption    number  - Optional: specifies Pages in the range to print.
;                                                   0 = (Default) no shrink
;                                                   ? = Pages in the range to print. Must be one of: PDAllPages, PDEvenPagesOnly, or PDOddPagesOnly.
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns 1
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;                               - 1 = obj is not a valid AcroExch.AVDoc obj
;                               - 2 = there were any exceptions while printing or no document was open
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_DocGetWinTitle()
; Description:      Gets the windows title.
; Parameter(s):  
;                   $aic_oAVDoc         -   ref     - The obj ref var (Must be a AVDoc Obj)
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns The Doc's Win Title
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_PDDocCreateNew()
; Description:      Gets the AcroExch.PDDoc associated with an AcroExch.AVDoc.
; Parameter(s):  
;                   $aic_oPDDoc         -   ref     - The obj ref var (Must be a PDDoc Obj)
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns -1
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = the $aic_oPDDoc is not an obj
;
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================



;===============================================================================
;
; Function Name:    _AcrobatIC_PDDocGetObject()
; Description:      Gets the AcroExch.PDDoc associated with an AcroExch.AVDoc.
; Parameter(s):  
;                   $aic_oAVDoc         -   ref     - The obj ref var (Must be a AVDoc Obj)
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns obj id for the PDDoc
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_JSGetObject()
; Description:      Gets a dual interface to the Javascript object associated with the PDDoc
; Parameter(s):  
;                   $aic_oPDDoc         -   ref     - The obj ref var (Must be a PDDoc Obj)
;                   $aic_showConsole    -   boolean - Optional: specifies whether or not to show the js console
;                                                   true    = (Default) makes the console visible
;                                                   false   = leaves the console hidden
;                   $aic_consoleClear   -   boolean - Optional: specifies whether or not to clear text in the console
;                                                   true    = (Default) clears console text
;                                                   false   = leaves the console text
;
; Requirement(s):   
; Return Value(s):  On Success  - Returns The interface to the Javascript object
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = not an obj
;
;                   @Extended   - 
; CallTip:
; Author(s):        John Bailey
;
;===============================================================================


;===============================================================================
;
; Function Name:    _AcrobatIC_OpenFileFresh()
; Description:    Open an Acrobat File by creating everything needed (see Note)
; Parameter(s):  $aic_FiletoOpen    -   string  - specifies what file to open
;                   $aic_Show       -   boolean - Optional: specifies whether or not the Acrobat Window is visible
;                                                   True  = (Default) Show window
;                                                   False = Leave window hiden
;                   $aic_DocTitle   -   string  - Optional: specifies the document title
;                                                   ''    = (Default) blank like this means the file name and location will be used as the name
;                                                   
;                   $aic_ReturnObjRef   number  - Optional: specifies which obj to return
;                                                   0 = (Default) Returns AcroExch.PDDoc Object Ref
;                                                   1 = Returns AcroExch.AVDoc Object Ref
;                                                   2 = Returns AcroExch.App Object Ref
; Requirement(s):   
; Return Value(s):  On Success  - Returns either
;                                   the AcroExch.App Object Ref (2)
;                                   the AcroExch.AVDoc Object Ref (1)
;                                   the AcroExch.PDDoc Object Ref (0) Default
;                  On Failure   - Returns 0 and sets @ERROR
;                   @ERROR      - 0 = AcroExch.App obj not created
;                               - 1 = AcroExch.AVDoc obj not created
;                   @Extended   - 
; CallTip:          
; Note(s):          This is a "self contained" file open, so _Create() and _AVDoc() are not needed.
; Author(s):        John Bailey
;
;===============================================================================

EXAMPLE

#include <Acrobat_InterApplication_Communication_API.au3>

Global $Aapp = _AcrobatIC_Create()
Local $printFile = FileOpenDialog('Select a PDF',@MyDocumentsDir,'PDF (*.pdf)')
Global $AVDoc = _AcrobatIC_AVDocOpenNew($Aapp,$printFile)

Local $iMsgBoxAnswer = MsgBox(36,"Print","Do you want to Print?")
Select
    Case $iMsgBoxAnswer = 6;Yes
        _AcrobatIC_AVDocPrint($AVDoc)
    Case $iMsgBoxAnswer = 7;No

EndSelect

Local $iMsgBoxAnswer = MsgBox(36,"Close Doc","Do you want to?")
Select
    Case $iMsgBoxAnswer = 6;Yes
        _AcrobatIC_AVDocClose($AVDoc)
    Case $iMsgBoxAnswer = 7;No

EndSelect

RESOURCES

http://www.adobe.com/devnet/acrobat/interapplication.html

http://developer.hoops3d.com/adobepdf/docu...APIOverview.pdf

http://partners.adobe.com/public/developer...nParameters.pdf

http://www.adobe.com/devnet/acrobat/pdfs/i...i_reference.pdf

http://partners.adobe.com/public/developer...eveloperFAQ.pdf

http://www.acrobatusers.com/blogs/thomp/index.php?paged=2

Acrobat_InterApplication_Communication_API.au3

Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

What does it do? All it asked me is if I wanted to print and then if I wanted to close after opening a pdf file.

It only works with a "fullversion" of Adobe Acrobat. It's sort of like a "COM" (nobody hate for using that here) for Acrobat.

This is VERY helpful for me! In lots of different ways! :) I just wanted to share. In the tech docs for the AICAPI, there is explanation on controlling the buttons and so forth in Acrobat and even using it to embed it in a GUI.

Edited by JohnBailey
A decision is a powerful thing
Link to comment
Share on other sites

Oh, no that's ok. I get it. I was just asleep. LOL Just got up. OK, I have acrobat but rarely use it. Thanks for sharing. I could see how it would be useful if you are using pdf files aot.

hahaha yeah. Oh, those moments...

I appreciate your feedback regardless !! :)

My office uses pdfs SO much. One of the programs we have here generates pdfs.

A decision is a powerful thing
Link to comment
Share on other sites

  • 3 weeks later...
  • 2 years later...

Hi! First of all, i would like to say that this is a great help also in me who is also dealing with PDF files. I tried your script and add a specific file in $printFile. Is there a way that the file open dialog window will be hidden and automatically open the specified pdf in Acrobat?

#include <Acrobat_InterApplication_Communication_API.au3>

Global $Aapp = _AcrobatIC_Create(False)
Local $printFile = FileOpenDialog('Select a PDF',@ScriptDir,'PDF (*.pdf)', 1,"Developing apps using IAC.pdf")
Global $AVDoc = _AcrobatIC_AVDocOpenNew($Aapp,$printFile)
;open PDF automatically without the need to click the "Open" button.

Local $iMsgBoxAnswer = MsgBox(36,"Print","Do you want to Print?")
Select
    Case $iMsgBoxAnswer = 6;Yes
        _AcrobatIC_AVDocPrint($AVDoc)
    Case $iMsgBoxAnswer = 7;No

EndSelect

Local $iMsgBoxAnswer = MsgBox(36,"Close Doc","Do you want to?")
Select
    Case $iMsgBoxAnswer = 6;Yes
        _AcrobatIC_AVDocClose($AVDoc)
    Case $iMsgBoxAnswer = 7;No

EndSelect

_AcrobatIC_AppExit($Aapp)
Link to comment
Share on other sites

" ... the file open dialog window will be hidden and automatically open the specified pdf in Acrobat?"

Sure. Comment out the pure autoit-line:

Local $printFile = FileOpenDialog .....)

and add something like:

$printFile = "C:\ThisOrThatDir\myPdf.pdf"

Untested, but should work,

Reinhard

Edit: here a link for (hidden) printing (without UDF):

http://www.autoitscript.com/forum/index.php?showtopic=107961&st=0&p=826314&hl=Reinhard&fromsearch=1&#entry826314

Edited by ReFran
Link to comment
Share on other sites

" ... the file open dialog window will be hidden and automatically open the specified pdf in Acrobat?"

Sure. Comment out the pure autoit-line:

Local $printFile = FileOpenDialog .....)

and add something like:

$printFile = "C:\ThisOrThatDir\myPdf.pdf"

Untested, but should work,

Reinhard

Edit: here a link for (hidden) printing (without UDF):

http://www.autoitscript.com/forum/index.php?showtopic=107961&st=0&p=826314&hl=Reinhard&fromsearch=1&#entry826314

Great!!! Thanks! Reinhard. Now I can open and get some properties of PDF, like page sizes using JSObject without showing Adobe Acrobat. AutoIt is great!!!

Link to comment
Share on other sites

Hi,

Just a quick question, how can you interpret this code:

app.execmenuItem(Annots:ReaderEnable);

to autoit using JSObject like:

$App = ObjCreate("AcroExch.App")
$App.Hide
$AVDoc = ObjCreate("AcroExch.AVDoc")
$AVDoc.Open("C:\SampleFile.pdf","")
$PDDoc = $AVDoc.GetPDDoc
$JSO = $PDDoc.GetJSObject 
$JSO.execmenuItem(Annots:ReaderEnable)

That code doesn't seem to work. What did I miss? Thanks for the help!

Link to comment
Share on other sites

Hi,

Just a quick question, how can you interpret this code:

app.execmenuItem(Annots:ReaderEnable);

to autoit using JSObject like:

$App = ObjCreate("AcroExch.App")
$App.Hide
$AVDoc = ObjCreate("AcroExch.AVDoc")
$AVDoc.Open("C:\SampleFile.pdf","")
$PDDoc = $AVDoc.GetPDDoc
$JSO = $PDDoc.GetJSObject 
$JSO.execmenuItem(Annots:ReaderEnable)

That code doesn't seem to work. What did I miss? Thanks for the help!

Hi,

I already found a way to solve this problem. I realize I need the word "app" before execMenuItem. Then add FileOpenDialog right next to it. Thanks guys!

Link to comment
Share on other sites

As Remark:

The best Acrobat code examples you will find in the AcroJs helpfile.

I use JSO for simple things, like printing, page number, ..

If it get real complex like in the example "list all bookmarks"

I write the example to a au3 variable and execute the JS-code direct.

Attached you find an quick example for that method.

HTH, Reinhard

$FilePath = "C:\Test.pdf"
$App = ObjCreate("AcroExch.App")     ;; start Adobe Acrobat
$App.Show                            ;; show Acrobat or comment out for hidden mode

$AVDoc = ObjCreate("AcroExch.AVDoc")      ;; connect to Ac Viewer
If $AVDoc.Open($FilePath,"") Then       ;; open the file
   $AForm = ObjCreate("AFormAut.App")       ;;connect to AForm API
   $JsCode = 'var x = this.numPages;' &@lf _      ;;write some AcroJs on variable
       & 'app.alert(x);'
   $AForm.Fields.ExecuteThisJavascript($JsCode)        ;;execute the Js-Code
endif
Link to comment
Share on other sites

As Remark:

The best Acrobat code examples you will find in the AcroJs helpfile.

I use JSO for simple things, like printing, page number, ..

If it get real complex like in the example "list all bookmarks"

I write the example to a au3 variable and execute the JS-code direct.

Attached you find an quick example for that method.

HTH, Reinhard

$FilePath = "C:\Test.pdf"
$App = ObjCreate("AcroExch.App")     ;; start Adobe Acrobat
$App.Show                            ;; show Acrobat or comment out for hidden mode

$AVDoc = ObjCreate("AcroExch.AVDoc")      ;; connect to Ac Viewer
If $AVDoc.Open($FilePath,"") Then       ;; open the file
   $AForm = ObjCreate("AFormAut.App")       ;;connect to AForm API
   $JsCode = 'var x = this.numPages;' &@lf _      ;;write some AcroJs on variable
       & 'app.alert(x);'
   $AForm.Fields.ExecuteThisJavascript($JsCode)        ;;execute the Js-Code
endif

Thanks for a wonderful idea! However, if I tried that and the AcroExch.App is set to Hide ($App.Hide) I get an error:

The requested action with this object has failed.:

$AForm.Fields.ExecuteThisJavascript($JsCode)

$AForm.Fields^ ERROR

Did I miss something here? Can you please advise what should I do? Thanks!

Link to comment
Share on other sites

Mmmh,

for me it works, but I work with with an older version (6) and Adobe restrict commands from version to version. However I don't can test it.

Sorry, Reinhard

Sorry, but that is not the script that I used. So here it is:

$inputfolder = IniRead (@ScriptDir & "\client.ini", "Folder", "in", "Not Found")
$outputfolder = IniRead (@ScriptDir & "\client.ini", "Folder", "out", "Not Found")
$delay = IniRead (@ScriptDir & "\client.ini", "Delay in milliseconds", "delay", "Not Found")


while 1 = 1

$inpdf = FileFindFirstFile ($inputfolder & "\*.pdf")
If $inpdf = -1 Or @error = 1 Then
Sleep (2000)
ContinueLoop
Else
$procpdf = FileFindNextFile ($inpdf)
Do
$movepdf = FileMove ($inputfolder & "\" & $procpdf, @ScriptDir & "\temp\" & $procpdf, 1)
Sleep (1000)
Until $movepdf = 1

$Aapp = ObjCreate ("AcroExch.App")
$Aapp.Hide
$AVDoc = ObjCreate ("AcroExch.AVDoc")
$AVDoc.Open (@ScriptDir & "\temp\" & $procpdf,"")
$PDDoc = $AVDoc.GetPDDoc
$PDDoc.SetPageMode (3) ;Set view to pages and bookmarks panel 
Sleep ($delay)
$AForm = ObjCreate ("AFormAut.App")
;expand all bookamrks
$EX = "" _
    & " function openBM(bm, nLevel)" & @LF _
    & " {for (var i = 0; i < nLevel; i++) bm.open = true;" & @LF _
    & "   if (bm.children != null)" & @LF _
    & "   {for (var i = 0; i < bm.children.length; i++) openBM(bm.children[i], nLevel + 1); }" & @LF _
    & " }" & @LF _
    & " openBM(this.bookmarkRoot, 1)"
  $AForm.Fields.ExecuteThisJavascript($EX)
  $JSO = $PDDoc.GetJSObject
Sleep ($delay)
$JSO.saveAs ($outputfolder & "\" & $procpdf)
Sleep ($delay)
$PDDoc.Close()
$Aapp.CloseAllDocs()
Sleep ($delay)
EndIf
WEnd

When I change the $Aapp.Hide to $Aapp.Show, I don't encounter any error. Can you please check if this will also work in your version of Acrobat? Thanks!

Link to comment
Share on other sites

Hi,

I don't have the time to test your script in detail.

Attached a kernel-code example, which works for me in both methods.

You may put that in a function and write your script around.

If you have problems with that you may open a new topic under "General help and support",

so that we don't hijack this topic.

HTH, Reinhard

$FilePath = "C:\Testbm.pdf"
$fileOut = "c:\TestBm_New.pdf"
$App = ObjCreate("AcroExch.App")     ;; start Adobe Acrobat
;;$App.Show                           ;; show Acrobat or comment out for hidden mode

$AVDoc = ObjCreate("AcroExch.AVDoc")     ;; connect to Ac Viewer
If $AVDoc.Open($FilePath,"") Then        ;; open the file
    $PDDoc = $AVDoc.GetPDDoc
    $PDDoc.SetPageMode (3) ;Set view to pages and bookmarks panel
    $AForm = ObjCreate("AFormAut.App")            ;;connect to AForm API
    $EX = "" _
        & " var fileOut = """&$fileOut&""";" & @LF _
        & " function openBM(bm, nLevel)" & @LF _
        & " {for (var i = 0; i < nLevel; i++) bm.open = true;" & @LF _
        & "   if (bm.children != null)" & @LF _
        & "   {for (var i = 0; i < bm.children.length; i++) openBM(bm.children[i], nLevel + 1); }" & @LF _
        & " }" & @LF _
        & " openBM(this.bookmarkRoot, 1)" & @LF _
        & " //app.alert('All Bookmarks now open');"
    ;msgbox(0,"",$ex)
    $AForm.Fields.ExecuteThisJavascript($EX)    ;;execute the Js-Code
    $PDDoc.save(1,$fileOut)
    $app.closeAlldocs()
    $app.exit()
Else
    msgbox(0,"","Coudn't open report")

endif
Edited by ReFran
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...