Help with adobe PDF and AHK

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Iferrorgotozero
Posts: 2
Joined: 22 Feb 2017, 19:04

Help with adobe PDF and AHK

22 Feb 2017, 19:17

Hi, long time lurker, first time poster.

I am writing a script that will do three things:

1. Pull variables out of an Excel spreadsheet (figured this out already)

2. Open an Adobe PDF that has been form prepped and now has several form fields and populate said form fields with previously captured variables.

3. Save PDF as something new


I am stuck on number 2. I have not had any luck evoking the form fields. So, I come to you fine gentlemen for advice, links to references, crude humor, and another other assistance you may want to offer.

Thank you.
enter8
Posts: 102
Joined: 05 May 2016, 16:48

Re: Help with adobe PDF and AHK

23 Feb 2017, 11:44

What application are you using to edit the PDF? How would you access the fields without AHK? Can you tab from field to field?

Edit: Have you read this?

https://autohotkey.com/boards/viewtopic.php?t=15193
Iferrorgotozero
Posts: 2
Joined: 22 Feb 2017, 19:04

Re: Help with adobe PDF and AHK

23 Feb 2017, 12:19

enter8 wrote:What application are you using to edit the PDF? How would you access the fields without AHK? Can you tab from field to field?

Edit: Have you read this?

https://autohotkey.com/boards/viewtopic.php?t=15193
1. I am using Adobe Acrobat DC.

2. I would access the fields through Adobe Acrobat.

3. I can tab from field to field in the PDF, yes.

I was hoping to directly call the fields sort of like GetField for a webform in some languages. Open to ideas though.

Thank you.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Help with adobe PDF and AHK

23 Feb 2017, 17:33

The COM methods appear only to work with Adobe Acrobat Pro (possibly the same thing as Adobe Acrobat DC).
Is there another pdf reader than can do this with COM or some other method rather than pressing the tab button?
Thanks.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Gibbons
Posts: 93
Joined: 20 Sep 2016, 20:39

Re: Help with adobe PDF and AHK

23 Feb 2017, 22:10

I have a poorly written program at work that I have to use. I use the TAB method. It sounds like your form doesn't change, so that may work for you.

I didn't like that I couldn't call each box by name, but after using the script with the TAB method hundreds of times now, I can say that I'm always getting the right information in the right place. So maybe you should just go that direction.....
User avatar
JoeWinograd
Posts: 2202
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with adobe PDF and AHK

24 Feb 2017, 00:21

Here's how I've done this in AHK. The overall approach is to create an FDF from the PDF, then modify the FDF (in your case, with the variables pulled from the Excel spreadsheet), then create a new PDF from the modified FDF.

First thing to do is download PDFtk Server from here:
http://www.pdflabs.com/tools/pdftk-server/

Don't be misled by "Server" in the name. I don't know why they called it that, but it's just an executable (pdftk.exe, with a supporting DLL, libiconv2.dll) that runs on "standard" Windows — it does not have to run on a "server" Windows.

In your AHK code, create an FDF from your PDF using PDFtk's generate_fdf parameter. Here's an actual line of code from one of my AHK programs that does it (I'm sure the variable names make it clear what's happening):

RunWait,%PDFtkEXE% "%PDFform%" generate_fdf output "%FDFfile%",,Hide

Now modify the FDF data with the data extracted from the Excel spreadsheet. If you're not familiar with FDF format, a web search will get you everything you need. The quick summary is that it is a plain text file consisting of key-value pairs. It is easy to parse and modify, because each key is preceded by /T and each value by /V. For example, here are a few lines from an FDF file:

/T (FirstName)
/V (John)
/T (LastName)
/V (Smith)
/T (Phone)
/V (708-555-1212)

After modifying the values with the data from the Excel spreadsheet, create a new PDF using PDFtk's fill_form parameter. Again, here's an actual line of code from one of my AHK programs:

RunWait,%PDFtkEXE% "%PDFform%" fill_form "%FDFtemp%" output "%PDFtemp%",,Hide

This technique has worked perfectly here in many AHK scripts, but if you run into any problems, let me know and I'll try to help. Btw, the manual for it is here:
https://www.pdflabs.com/docs/pdftk-man-page/

And examples are here:
https://www.pdflabs.com/docs/pdftk-cli-examples/

You should also take a look at its licensing:
https://www.pdflabs.com/docs/pdftk-license/

As a disclaimer, I want to emphasize that I have no affiliation with this company and no financial interest in it whatsoever. I am simply a happy user/customer. Regards, Joe

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jdfnnl, Joey5 and 351 guests