Adobe Acrobat Pro via COM

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Adobe Acrobat Pro via COM

12 Jun 2018, 17:08

Hi MannyKSoSo, I've been messing with this for a little while, and haven't got the COM part of it working yet.
This code, while not COM, does work.
Spoiler
I'm not sure if the JS object for Acrobat takes the same arguments as the AHK Com object, but the code you provided:

Code: Select all

oAcrobat.saveAs(A_ScriptDir "FileToRead.xml", "com.adobe.acrobat.xml-1-00")
appears to be for JavaScript, which Acrobat of course can use extensively.

If I get any closer, I'll post my results.
Regards,
burque505
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Adobe Acrobat Pro via COM

12 Jun 2018, 17:45

MannyKSoSo wrote:Specifically it has to do with the saveAs part of the code

Code: Select all

oAcrobat.saveAs(A_ScriptDir "FileToRead.xml", "com.adobe.acrobat.xml-1-00")
At a quick glance without looking at any of your other code, you have to have a "\" between your path and filename.
oAcrobat.saveAs(A_ScriptDir "\FileToRead.xml", "com.adobe.acrobat.xml-1-00")
But there appears to be more going wrong than that. You have to have two full valid paths.

Code: Select all

DocSaveAs
Saves an open file to a new path. The user is not warned if there are any problems saving the file.

Syntax
[DocSaveAs(char* fullPath, char* newPath)]

Parameters
fullPath
The full path of the existing file.

newPath
The full path of the new file.

Returns
true if the document is saved successfully, false if the document does not exist or is not saved successfully.
Maybe something like this:

Code: Select all

App := ComObjCreate("AcroExch.App")
App.DocSaveAs("C:\Test.pdf", "C:\Text.xml")
I don't know if that will work but it is looking like something closer to what would work based on just looking at the API Reference.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Adobe Acrobat Pro via COM

12 Jun 2018, 18:21

burque505 wrote:I'm not sure if the JS object for Acrobat takes the same arguments as the AHK Com object, but the code you provided:

Code: Select all

oAcrobat.saveAs(A_ScriptDir "FileToRead.xml", "com.adobe.acrobat.xml-1-00")
appears to be for JavaScript, which Acrobat of course can use extensively.
Oh, I see now. I was a little confused. There is a DDE command SaveAs which was what I, at a glance, though you were trying to do.

Here is a tested JavaScript solution that works for me.

Code: Select all

App := ComObjCreate("AcroExch.App")
AVDoc := App.GetActiveDoc()
PDDoc := AVDoc.GetPDDoc()
JSO	:= PDDoc.GetJSObject
JSO.SaveAs(A_Desktop "\Test\TestXML.xml", "com.adobe.acrobat.xml-1-00")
This uses AHK to create a COM object for Acrobat. Then uses Acrobat to create a JavaScript object that can execute the Javascript version of SaveAs to save the active document as XML.

You can look up alot of other file formats and SaveAs lots of different formats.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Adobe Acrobat Pro via COM

12 Jun 2018, 20:18

:bravo:
Thanks, FG, fine advice as always.
regards,
burque505

EDIT: I had worked out this solution that requires two scripts. It works, but there must be a better way using COM. The problem I'm trying to overcome is the line

Code: Select all

oAcrobat.MenuItemExecute("SaveAs")	
which is modal, I guess, and stops the script dead in its tracks. The other problem, of course, is that it's visible.

Script 1: acro.ahk
Spoiler
Script 1 runs Script 2, acrohelper.ahk:
Spoiler
EDIT: I changed both scripts just a little. This DOES work, but both scripts have to be in the same directory, and you have to run acro.ahk first, as it starts the second script. (I've tried using SetTimer to combine this into one script, but I haven't been able to kill Acrobat after the XML file is saved, although it does successfully save the XML file.)
Last edited by burque505 on 13 Jun 2018, 11:52, edited 1 time in total.
MannyKSoSo
Posts: 440
Joined: 28 Apr 2018, 21:59

Re: Adobe Acrobat Pro via COM

13 Jun 2018, 07:01

Thanks FG, I indeed tested the code this morning and it works as I intended it too. I had also tested with just the PDDoc := AVDoc.GetPDDoc() part to see if it would work (but seems I was more off than I thought LOL).
burque505, I indeed tried to do something similar to what you had, but when you run the MenuItemExecute part of the code, it stops because it waits for you to input a name and type of document (which is why I changed my approach to the one FG provided).
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Adobe Acrobat Pro via COM

13 Jun 2018, 12:05

Kudos again to FG, as usual :) Manny, did your final code look anything like this?

Code: Select all

App := ComObjCreate("AcroExch.App")
oAcrobatdoc := ComObjCreate("acroExch.avdoc")      ; create an document object
;oAcrobat.maximize(1)      ; maximize the application window (-1 = true)
oAcrobatdoc.Open(A_ScriptDir "\FileToRead.pdf", "")
sleep, 1500
AVDoc := App.GetActiveDoc()
PDDoc := AVDoc.GetPDDoc()
JSO	:= PDDoc.GetJSObject
JSO.SaveAs(A_ScriptDir "\FileToReadAsXML.xml", "com.adobe.acrobat.xml-1-00")
I can see where this would be really useful. One thing that comes to mind is extracting data from the PDF, and then using DocumentAssembler from OpenXmlPowerTools to populate a word document with that XML data.
Regards,
burque505
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Adobe Acrobat Pro via COM

13 Jun 2018, 13:20

burque505 wrote:Kudos again to FG, as usual :) Manny, did your final code look anything like this?

Code: Select all

App := ComObjCreate("AcroExch.App")
oAcrobatdoc := ComObjCreate("acroExch.avdoc")      ; create an document object
;oAcrobat.maximize(1)      ; maximize the application window (-1 = true)
oAcrobatdoc.Open(A_ScriptDir "\FileToRead.pdf", "")
sleep, 1500
AVDoc := App.GetActiveDoc()
PDDoc := AVDoc.GetPDDoc()
JSO	:= PDDoc.GetJSObject
JSO.SaveAs(A_ScriptDir "\FileToReadAsXML.xml", "com.adobe.acrobat.xml-1-00")
I can see where this would be really useful. One thing that comes to mind is extracting data from the PDF, and then using DocumentAssembler from OpenXmlPowerTools to populate a word document with that XML data.
Regards,
burque505
Here is a little more streamlined version (with some comments):

Code: Select all

AVDoc := ComObjCreate("acroExch.AVDoc")      ; create an AV document object
AVDoc.Open(A_Desktop "\Test\Test.pdf", "")	; open a pdf into the AVDoc (this is a view but it is hidden by default)
PDDoc := AVDoc.GetPDDoc()	; get the underlining PDDoc of the AVDoc (this is kind of like the HTML of a webpage before it is processed and rendered)
JSO	:= PDDoc.GetJSObject	; this JavaScript object is mainly used by Acrobat to run its scripts engine
JSO.SaveAs(A_Desktop "\Test\TestXML.xml", "com.adobe.acrobat.xml-1-00")	; convert and save as xml
AVDoc.Close(1)	; otherwise there will be a hidden document still running in processes
Acrobat has various different and separate COM objects with separate methods. Everything is not organized under one application object like with Excel or other Microsoft products.

The main ones are App (application), AVDoc (Acrobat View), and PDDoc (Portable Document). There are others. Each can be created directly with ComObjCreate or spawned from each other. You don't always need all of them.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: Adobe Acrobat Pro via COM

13 Jun 2018, 13:43

An even more streamline version.

Code: Select all

PDDoc := ComObjCreate("acroExch.PDDoc")      ; create an PD document object
PDDoc.Open(A_Desktop "\Test\Test.pdf")	; open a pdf into the PDDoc
JSO	:= PDDoc.GetJSObject	; this JavaScript object is mainly used by Acrobat to run its scripts engine
JSO.SaveAs(A_Desktop "\Test\TestXML.xml", "com.adobe.acrobat.xml-1-00")	; convert and save as xml
If you never intend to actually view the PDF then there is no need to create a AVDoc.

Also because there is not really any such thing as a hidden and visible PDDoc (they are never visible by nature), the PDDoc object is automatically destroyed when the AHK script ends and the AHK object is destroyed. If you were using this in some script that is not ending like this example, you can implicatively close the PDDoc with PDDoc.Close().

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
burque505
Posts: 1731
Joined: 22 Jan 2017, 19:37

Re: Adobe Acrobat Pro via COM

13 Jun 2018, 18:08

Thanks, FG, once again, for this super-helpful stuff.
Regards,
burque505

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: garry, mikeyww and 128 guests