Retrieve filename and pagenumber from PDF

Get help with using AutoHotkey and its commands and hotkeys
huismu5
Posts: 4
Joined: 28 Dec 2015, 03:21

Retrieve filename and pagenumber from PDF

28 Dec 2015, 03:32

I searched the forum, but could not find anything about this.

I'm building a text summerizer tool. The idea is that you select a paragraph of text from a PDF file and then click on a button in a GUI. This will then copy the selected text and paste it into a txt document (sofar-so-good). The issue is, that I want it to also include the filename and PDF page number in the pasted text, e.g.:
[Filename.pdf] - [page number]:
[selected text]
Is there any way to do this for a novice AHK programmer?
Peter2
Posts: 244
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Retrieve filename and pagenumber from PDF

28 Dec 2015, 03:44

I suppose this depends on the PDF-Viewer that you are using. PDF-XChange Editor offers a Javascript API, so maybe you can solve your job without AHK?
http://www.tracker-software.com/product ... nge-editor
Peter (AHK Beginner) / Win 7 x64, AHK Version v1.1.22.xx
huismu5
Posts: 4
Joined: 28 Dec 2015, 03:21

Re: Retrieve filename and pagenumber from PDF

28 Dec 2015, 03:56

Thanks for the quick reply Peter, PDF-Xchange Editor looks interesting, I'll have a closer look. Unfortunately, I'm illiterate in all programming languages except AHK and VBA.
Peter2
Posts: 244
Joined: 21 Sep 2014, 14:38
Location: CH

Re: Retrieve filename and pagenumber from PDF

28 Dec 2015, 04:52

huismu5 wrote:...I'm illiterate in all programming languages except AHK ...
Me too, but ask the support in the forum. They are very quick and competent, and maybe they can show you the way ...
Peter (AHK Beginner) / Win 7 x64, AHK Version v1.1.22.xx
pagenumber

Re: Retrieve filename and pagenumber from PDF

28 Dec 2015, 08:20

This works for me with Adobe Acrobat Reader DC

Code: Select all

F12::
WinGetText, AllText, ahk_class AcrobatSDIWindow
WinGetTitle, FullTitle, ahk_class AcrobatSDIWindow
StringReplace, Title, FullTitle,%A_Space%- Adobe Acrobat Reader DC
AllText:=Trim(AllText,"`r`n ")
Page:=SubStr(AllText,InStr(AllText,"`n",,0,1)+1)
MsgBox % "File: " Title "`nPage: " page
Return
Johnny R
Posts: 269
Joined: 03 Oct 2013, 02:07

Re: Retrieve filename and pagenumber from PDF

29 Dec 2015, 08:06

Thie works for Adobe Acrobat IX:

Code: Select all

F12::
ComObjError(false) ; Sonst stürzt das Skrtipt ab, wenn kein Dokument geöffnet ist.
gApp    := ComObjCreate("AcroExch.App") 
gAvDoc  := gApp.GetActiveDoc
gAvPage := gAvDoc.GetAVPageView
PageNo := gAvPage.GetPageNum + 1
gPdDoc  := gAvDoc.GetPDDoc
JSO     := gPdDoc.GetJSObject
AktFile := JSO.Path
MsgBox % "File: " AktFile "`nPage: " PageNo
Return
huismu5
Posts: 4
Joined: 28 Dec 2015, 03:21

Re: Retrieve filename and pagenumber from PDF

29 Dec 2015, 10:36

Thanks for the suggestions, I'll try both of them.

May be premature to ask as ihave not tested the code yet, but for my understanding: the first option looks at the window title, the second one does some com magic. Both retrieve the page number in the same way, but why do you add 1 to the page number?

Thanks for the explanation
Johnny R
Posts: 269
Joined: 03 Oct 2013, 02:07

Re: Retrieve filename and pagenumber from PDF

29 Dec 2015, 11:33

why do you add 1 to the page number?
GetPageNum: Gets the page number of the current page. The first page in a document is page zero.
huismu5
Posts: 4
Joined: 28 Dec 2015, 03:21

Re: Retrieve filename and pagenumber from PDF

06 Jan 2016, 23:49

OK, I've put together the following code: I make a selection of text in Acrobat Reader DC and then press F12. I then go to Notepad or MS Word and paste with ctrl-V.

Code: Select all

F12::
clipboard = ; clear the clipboard
send ^C
ClipWait
Content := Clipboard
WinGetText, AllText, ahk_class AcrobatSDIWindow
WinGetTitle, FullTitle, ahk_class AcrobatSDIWindow
StringReplace, Title, FullTitle,%A_Space%- Adobe Acrobat Reader DC
AllText:=Trim(AllText,"`r`n ")
Page:=SubStr(AllText,InStr(AllText,"`n",,0,1)+1)
;MsgBox % "File: " Title "`nPage: " page
clipboard := % "File: " Title " Page: " Page "`r`n" Content
Return
The results are not consistant. I.e. Sometimes it works, sometimes nothing is copied to the clipboard and sometimes, the previous filename and pagenumber are appended to the top of the clipboard. Any tips on how to improve this script?

Thanks!
pagenumber

Re: Retrieve filename and pagenumber from PDF

07 Jan 2016, 06:28

Perhaps like so?

Code: Select all

F12::
Clipboard = ; clear the clipboard
send ^c ; lower C just in case, upper C implies the SHIFT key being sent as well.
; ClipWait
Sleep 200 ; I prefer not to use clipwait, 200 - 300 ms usually does it for me
Content:=Clipboard
Sleep 50 ; might help
WinGetText, AllText, ahk_class AcrobatSDIWindow
Sleep 50 ; might help
WinGetTitle, FullTitle, ahk_class AcrobatSDIWindow
Sleep 50 ; might help
StringReplace, Title, FullTitle,%A_Space%- Adobe Acrobat Reader DC
AllText:=Trim(AllText,"`r`n ")
Page:=SubStr(AllText,InStr(AllText,"`n",,0,1)+1)
;MsgBox % "File: " Title "`nPage: " page
Clipboard:="File: " Title " Page: " Page "`r`n" Content ; no need for %
AllText:="", Title:="", Page:="", FullTitle:="", Content:="" ; clear all variables
Return

Return to “Ask For Help”

Who is online

Users browsing this forum: Animare, Bing [Bot], Google [Bot], xhoffzz and 214 guests