Open PDF using ComObject

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Open PDF using ComObject

19 Feb 2024, 13:27

Hello!

I still have a very limited understanding of how to use COM with Adobe, but I've gotten some things to work in the past. I'm trying to do a seemingly easy action of just opening a specific PDF, but I can't seem to get it to work. I assume the code will be something like this:

Code: Select all

:*:yyyy::
{
OpenFile := A_Desktop '\Test.pdf'
AcroAPP := ComObject('AcroExch.App')
acrobatAVDoc := ComObject('AcroExch.AVDoc')
AcrobatAVDoc.Open(OpenFile)
}
What am I missing or doing wrong? The ultimate goal is to have this Test document added/merged to a currently-open PDF (basically adding a cover page). Looking through the forums I've seen a script which should help with the merging, but my understanding is that all documents to be merged will need to be open first. Thanks in advance for the help!
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Open PDF using ComObject

19 Feb 2024, 15:56

mwpriest wrote:
19 Feb 2024, 13:27
I'm trying to do a seemingly easy action of just opening a specific PDF, but I can't seem to get it to work.

Here is code to open a PDF in Acrobat:

Code: Select all

PDDoc := ComObject('AcroExch.PDDoc')	; create new document object
PDDoc.Open(A_Desktop '\Test\Test.pdf')	; open document
AVDoc := PDDoc.OpenAVDoc('') 			; view document

Also, you can add/merge pages without actually displaying the document. You can work with a PDDoc object to do many things. You only need a AVDoc object for viewing

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: 1907
Joined: 30 Sep 2013, 22:25

Re: Open PDF using ComObject

19 Feb 2024, 18:44

Here is a script that merges a PDF from a file at the beginning of the active PDF.

Code: Select all

; Insert file PDF at the beginning of currently active PDF
; Verbose
F12::
{
	AcroApp := ComObject('AcroExch.App')
	AcroAVDoc_Active := AcroApp.GetActiveDoc()
	AcroPDDoc_Active := AcroAVDoc_Active.GetPDDoc()
	AcroPDDoc_Merge := ComObject('AcroExch.PDDoc')
	AcroPDDoc_Merge.Open(A_Desktop '\Test\Test.pdf')
	AcroPDDoc_Active.InsertPages(-1, AcroPDDoc_Merge, 0, AcroPDDoc_Merge.GetNumPages, 1) ; index-0, add at beginning all pages with bookmarks
	AcroPDDoc_Merge.Close()
}

; Condensed
F11::
{
	(PDDoc := ComObject('AcroExch.PDDoc')).Open(A_Desktop '\Test\Test.pdf')
	ComObject('AcroExch.App').GetActiveDoc().GetPDDoc().InsertPages(-1, PDDoc, 0, PDDoc.GetNumPages, 1) ; index-0, add at beginning all pages with bookmarks
	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
mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: Open PDF using ComObject

20 Feb 2024, 20:38

Thanks, FG! The first script definitely solved the problem with opening the PDF.

For some reason the second script for merging didn't work. It's not merging and it's also not opening the second document (the Test document). I've tried both the verbose and the condensed versions. Any idea why? You mentioned there is a way to merge without opening the second document. Is this script doing it that way, or what would that look like?

And, was there a guide that you used to help accumulate your COM knowledge? Do you recommend a good resource?

Thanks!
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Open PDF using ComObject

20 Feb 2024, 21:34

mwpriest wrote:
20 Feb 2024, 20:38
For some reason the second script for merging didn't work. It's not merging and it's also not opening the second document (the Test document). I've tried both the verbose and the condensed versions. Any idea why? You mentioned there is a way to merge without opening the second document. Is this script doing it that way, or what would that look like?

And, was there a guide that you used to help accumulate your COM knowledge? Do you recommend a good resource?

The script I posted works for me. You have to have an active PDF open in Acrobat, then hit F12, and the file at A_Desktop '\Test\Test.pdf' will be merged at the beginning of the active PDF document. Of course, you have to change the path and filename to match what you want. You also have to have a PDF open and activated. It cannot be minimized or anything like that. Basically, you are reviewing a PDF and then hit a key to add a cover page.

It does not display the file to be merged. You should just see it appear as a new page in your PDF that is active.

I learned pretty much everything I know about COM from the forums. At least that is the starting point, then you have to look up stuff in the object model. For Office programs, https://learn.microsoft.com/en-us/office/vba/api/overview/

You can also just type something like, "msdn excel saveas" in google and it will take you to the saveas object for Excel. MSDN stands for "Microsoft Developer Network". You can do that for most commands and office programs, then branch from there. Also, study VBA code. If you want to do something in Office, google for the VBA code and then convert it to AHK. There is tons of VBA code out there for Office and it is very similar to AHK as far as the COM commands.

For Acrobat, Adobe has a bunch of stuff as part of their Software Development Kit (SDK). If you can find the old PDFs they are great to have. I believe all their new stuff is online. The API or COM stuff has not changed in many years to keep compatibility. So, the old PDF references are easier to use in my opinion.

COM for more obscure applications is even harder to find, like AutoCAD has COM support but it is poorly documented.

Here is a list of sources: https://www.the-automator.com/com-and-autohotkey/
That site in general has quite a bit of good information about AHK.

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
mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: Open PDF using ComObject

21 Feb 2024, 20:50

Thanks again, FG. I'm still not sure why the script isn't working on my end. If I add in an "OpenAVDoc" line it'll successfully open the doc trying to be inserted, so everything around it seems to be working. I think the "InsertPages" command might be failing, but I still haven't pinpointed what the issue might be. It looks right, but when I execute the script nothing happens. Do you have any suggestions for troubleshooting?
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Open PDF using ComObject

22 Feb 2024, 13:32

mwpriest wrote:
21 Feb 2024, 20:50
Thanks again, FG. I'm still not sure why the script isn't working on my end. If I add in an "OpenAVDoc" line it'll successfully open the doc trying to be inserted, so everything around it seems to be working. I think the "InsertPages" command might be failing, but I still haven't pinpointed what the issue might be. It looks right, but when I execute the script nothing happens. Do you have any suggestions for troubleshooting?

Post your code that is not working. Is it exactly the same as the code I posted except for the path? Have you tried it on different PDFs? Is there anything unusual about the PDFs?

This code works for me:

Code: Select all

F12::
{
	AcroApp := ComObject('AcroExch.App')
	AcroAVDoc_Active := AcroApp.GetActiveDoc()
	AcroPDDoc_Active := AcroAVDoc_Active.GetPDDoc()
	AcroPDDoc_Merge := ComObject('AcroExch.PDDoc')
	AcroPDDoc_Merge.Open(A_Desktop '\Test\Test.pdf')
	AcroPDDoc_Active.InsertPages(-1, AcroPDDoc_Merge, 0, AcroPDDoc_Merge.GetNumPages, 1) ; index-0, add at beginning all pages with bookmarks
	AcroPDDoc_Merge.Close()
}

I have a PDF open and active. I hit F12 and the PDF at A_Desktop '\Test\Test.pdf' is inserted into the PDF I have open.

You can put the line MsgBox AcroPDDoc_Active.GetFileName '`t' AcroPDDoc_Merge.GetFileName just before the InsertPage line to see if it returns the file names properly of the two files. This will tell you if the script has gotten a handle to the PDDoc objects correctly.

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
mwpriest
Posts: 20
Joined: 03 Oct 2022, 07:36

Re: Open PDF using ComObject

23 Feb 2024, 17:23

Here's my script:

Code: Select all

:*:yyyy::
{
	AcroApp := ComObject('AcroExch.App')
	AcroAVDoc_Active := AcroApp.GetActiveDoc()
	AcroPDDoc_Active := AcroAVDoc_Active.GetPDDoc()
	AcroPDDoc_Merge := ComObject('AcroExch.PDDoc')
	AcroPDDoc_Merge.Open(A_Desktop '\CheckIn.pdf')
	MsgBox AcroPDDoc_Active.GetFileName '`t' AcroPDDoc_Merge.GetFileName	
	AcroPDDoc_Active.InsertPages(-1, AcroPDDoc_Merge, 0, AcroPDDoc_Merge.GetNumPages, 1)
	AcroPDDoc_Merge.Close()
}
It's the same code you provided but with the actual file path/name. I've also added in the MsgBox command and it is providing the desired results. The merge still isn't happening for some reason. I've tried changing the "-1" just for fun to see if that changes anything. I've tried removing the GetNumPages command and just entering "1" (the number of pages in the AcroPDDoc_Merge document. Those changes don't seem to fix the problem. It's a mystery!
User avatar
FanaticGuru
Posts: 1907
Joined: 30 Sep 2013, 22:25

Re: Open PDF using ComObject

23 Feb 2024, 19:22

mwpriest wrote:
23 Feb 2024, 17:23

Code: Select all

:*:yyyy::
{
	AcroApp := ComObject('AcroExch.App')
	AcroAVDoc_Active := AcroApp.GetActiveDoc()
	AcroPDDoc_Active := AcroAVDoc_Active.GetPDDoc()
	AcroPDDoc_Merge := ComObject('AcroExch.PDDoc')
	AcroPDDoc_Merge.Open(A_Desktop '\CheckIn.pdf')
	MsgBox AcroPDDoc_Active.GetFileName '`t' AcroPDDoc_Merge.GetFileName	
	AcroPDDoc_Active.InsertPages(-1, AcroPDDoc_Merge, 0, AcroPDDoc_Merge.GetNumPages, 1)
	AcroPDDoc_Merge.Close()
}
It's the same code you provided but with the actual file path/name. I've also added in the MsgBox command and it is providing the desired results. The merge still isn't happening for some reason. I've tried changing the "-1" just for fun to see if that changes anything. I've tried removing the GetNumPages command and just entering "1" (the number of pages in the AcroPDDoc_Merge document. Those changes don't seem to fix the problem. It's a mystery!

I did your code above exactly, using your path and everything, and it worked fine.

I did try securing the document with a password and then the script failed silently with no error. The Msgbox works but no merger. Kind of like it is read only. Even if when I added the restriction, I unchecked everything so that the password was not really restricting anything. Also, if I turn on any security for the file to be merged it fails. There might be a security issue with your PDFs.

Can you manually drag the CheckIn.pdf into the active PDF? You should be able to make the pages visible on the left and then drag a PDF into that page area and it will merge manually.

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

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Draken, Giresharu, TAC109 and 34 guests