Adobe COM, edit a word

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Adobe COM, edit a word

01 Apr 2021, 15:02

Hi,

I have a script to read words in a PDF, but I need to edit one. These are not in fields like typical PDFs might have. Any idea on how to do that?

This was my attempt. Here is a document for references https://www.adobe.com/content/dam/acom/en/devnet/acrobat/pdfs/5186AcroJS.pdf

Code: Select all

#SingleInstance, Force

source:=A_Desktop . "\1.pdf"

PDDoc := ComObjCreate("AcroExch.PDDoc")
PDDoc.Open(source)
JSO	:= PDDoc.GetJSObject
w=0
Loop 1000
    {
		Word := JSO.GetPageNthWord(0, w, 0)
		
        If (InStr(Word,"Plate"))
		 	{
                 w--
                 Word := JSO.GetPageNthWord(0, w, 0)
                 msgbox, % "Before: " . Word
                 JSO.SelectPageNthWord(0, w, 0).Value := "Bowl" ;didn't work.
                 PDDoc.Save(1,source)
                 Word := JSO.GetPageNthWord(0, w, 0)
                 msgbox, % "After: " . Word
                 exitapp
            }
        W++
    }
exitapp
Any help is appreciated!

Thanks,
Vh
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Adobe COM, edit a word

01 Apr 2021, 16:38

Vh_ wrote:
01 Apr 2021, 15:02
I have a script to read words in a PDF, but I need to edit one. These are not in fields like typical PDFs might have. Any idea on how to do that?

Code: Select all

#SingleInstance, Force

source:=A_Desktop . "\1.pdf"

PDDoc := ComObjCreate("AcroExch.PDDoc")
PDDoc.Open(source)
JSO	:= PDDoc.GetJSObject
w=0
Loop 1000
    {
		Word := JSO.GetPageNthWord(0, w, 0)
		
        If (InStr(Word,"Plate"))
		 	{
                 w--
                 Word := JSO.GetPageNthWord(0, w, 0)
                 msgbox, % "Before: " . Word
                 JSO.SelectPageNthWord(0, w, 0).Value := "Bowl" ;didn't work.
                 PDDoc.Save(1,source)
                 Word := JSO.GetPageNthWord(0, w, 0)
                 msgbox, % "After: " . Word
                 exitapp
            }
        W++
    }
exitapp

I believe you are going to have real problems doing this. I do not believe that Acrobat through the API or JaveScript supports editing the text outside of fields.

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
Vh_
Posts: 203
Joined: 17 Mar 2017, 22:06

Re: Adobe COM, edit a word

02 Apr 2021, 11:03

Thanks for the feedback. I will abandon the project. :)
User avatar
FanaticGuru
Posts: 1908
Joined: 30 Sep 2013, 22:25

Re: Adobe COM, edit a word

02 Apr 2021, 12:31

Vh_ wrote:
02 Apr 2021, 11:03
Thanks for the feedback. I will abandon the project. :)
Depending on the PDF it might be possible to convert the PDF to Word, edit the Word document, then convert back to a PDF.

This should all be possible programmatically through COM.

If the PDF is fairly straight forward, the results could be satisfactory. I tested converting a few PDFs to Word and back manually and the results looked acceptable in my test. Converting and back is pretty invasive though so it really depends on the complexity of the PDF.

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 (v1)”

Who is online

Users browsing this forum: Bing [Bot], Dart Vanya, Google [Bot], jeves and 142 guests