Show PDF in AHK GUI

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Show PDF in AHK GUI

01 Jul 2023, 14:15

Hi there. I would like to show a pdf in an ahk gui - embeded in some acitvex or other resource. Whatever works. I tried working with the explorer as in this code:

Code: Select all

MyGui := Gui()
	WB := MyGui.Add("ActiveX", "w980 h640", "Shell.Explorer").Value  ; The last parameter is the name of the ActiveX component.
	WB.Navigate("file:///C:\Test.pdf")  ; This is specific to the web browser control.
	MyGui.Show()
Unfortunately the skript opens an empty explorer on the gui and then opens the default pdf viewer to show the file. I did not find any solution on the forum.

Maybe someone has a solution to this. Thanks Simon
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Show PDF in AHK GUI

01 Jul 2023, 18:39

Your script works fine here using Foxit as default and as default in browser. Check your default pdf program on how to open pdf's in browser. You do not need the "file:///" part.
14.3 & 1.3.7
lexikos
Posts: 9690
Joined: 30 Sep 2013, 04:07
Contact:

Re: Show PDF in AHK GUI

02 Jul 2023, 06:32

On my system, the code at the top shows a GUI with a WebBrowser control, which then shows a download dialog for the PDF file. When I cancel that, it shows one more identical download dialog, and of course doesn't display the PDF.

On the other hand, this code ...

Code: Select all

MyGui := Gui()
MyGui.Add("ActiveX", "w980 h640", "C:\Test.pdf")
MyGui.Show()
... does it more efficiently.

And it only shows one download dialog. :lol:

(I know that's not the purpose. I'd guess that on flyingDman's machine, the more succinct method would probably work too.)
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Re: Show PDF in AHK GUI

03 Jul 2023, 00:57

Thank you guys.

Still no success. I tried changing the default pdf viewer from acrobat to foxit reader and to sumatra pdf viewer but it still keeps opening outside of the gui. Explorer can easily display pdfs in a tab, so why does this not work?
garry
Posts: 3795
Joined: 22 Dec 2013, 12:50

Re: Show PDF in AHK GUI

03 Jul 2023, 01:25

this example in ahk V1

Code: Select all

#Requires AutoHotkey v1.1.33
#Warn
xxa=Shell.Explorer 
F1=%a_scriptdir%\lidl.pdf
ifexist,%f1%
{
stringreplace,f1,f1,\,/,all           ;-- <<<
F2:="file:///" . F1
Gui Add, ActiveX, w980 h750 vWB1,%xxa%
WB1.Navigate(F2)
Gui, Show,x0 y0 w1024 h768, TEST-PDF
}
else
  msgbox, 262208, ,This file NOT EXISTs=`n%f1%
return
Guiclose:
exitapp
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Re: Show PDF in AHK GUI

03 Jul 2023, 04:30

@garry and @lexikos and the others: Thanks!

I dont' do V1 anymore. V2 is just better in so many ways - it's ridiculous!

But garry's skript translated to V2 does not work at my end, unfortunately. Can I embed the pdf-Viewer directly into an activeX-component and omit the explorer alltogether?
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Show PDF in AHK GUI

03 Jul 2023, 12:02

I'd guess that on flyingDman's machine, the more succinct method would probably work too
Yes, no issue here.

Re Foxit; did you do this?: https://kb.foxit.com/hc/en-us/articles/360040658351-How-can-I-open-PDF-files-in-a-Web-Browser-
14.3 & 1.3.7
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Re: Show PDF in AHK GUI

05 Jul 2023, 01:59

Thanks @flyingDman

No I did not. Thanks for the hint!! The problem is that my users won't be able to do it, so this is not an option.

I guess this problem will have to wait to be solved another time. Unless you have an idea how to directly embed a pdf viewer into an ahk gui. I'm thinking, the viewer that the explorer window uses to show previews of files when they are selected... is it possible to use that one?
ahk7
Posts: 577
Joined: 06 Nov 2013, 16:35

Re: Show PDF in AHK GUI

05 Jul 2023, 02:18

Not elegant, but you could convert the PDF to images or HTML using cli tools pdftopng / pdftohtml https://www.xpdfreader.com/about.html
Spitzi
Posts: 315
Joined: 24 Feb 2022, 03:45

Re: Show PDF in AHK GUI

05 Jul 2023, 03:06

No, i need to be able to scroll in it and zoom it etc.
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Show PDF in AHK GUI

05 Jul 2023, 09:10

You could also use webview2 to display the pdf if a gui.
User avatar
mikeyww
Posts: 27366
Joined: 09 Sep 2014, 18:38

Re: Show PDF in AHK GUI

05 Jul 2023, 09:26

Was interesting. Takes a few steps to set up. I downloaded WebView2.ahk. Put ComVar.ahk into its parent directory. Put WebView2Loader.dll into a "64bit" subdirectory. Then ran the following script, which worked.

Code: Select all

#Requires AutoHotkey v2.0
; #Include <WebView2\WebView2>
#Include d:\utils\WebView2\WebView2.ahk
pdf  := 'd:\temp2\test.pdf'
main := Gui('+Resize')
main.OnEvent('Close', (*) => (wvc := wv := 0))
main.Show(Format('w{} h{}', A_ScreenWidth * 0.6, A_ScreenHeight * 0.6))
wvc  := WebView2.create(main.Hwnd)
wv   := wvc.CoreWebView2
wv.Navigate(pdf)
wv.AddHostObjectToScript('ahk', {str:'str from ahk',func:MsgBox})
; wv.OpenDevToolsWindow()
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Show PDF in AHK GUI

05 Jul 2023, 10:24

I agree its a bit overkill but there's just not a ton of options for PDFs. You could certainly convert to an image and then scale the image up and down. Scrolling it would be possible as well but also complicated to implement.
malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: Show PDF in AHK GUI

05 Jul 2023, 11:24

Spitzi, You need to enable adobe pdf reader activex in ie:
https://helpx.adobe.com/acrobat/using/display-pdf-in-browser.html
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Show PDF in AHK GUI

05 Jul 2023, 12:02

Or just skip using IE altogether and use the control itself... Be warned it requires running 32-bit AHK.

Code: Select all

g := Gui()
x := g.AddActiveX("w600 h600", "AcroPDF.PDF").value

g.show()

x.LoadFile(FileSelect(, , "Pick a PDF", "PDF (*.pdf)"))
User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Show PDF in AHK GUI

05 Jul 2023, 18:06

Loadfile did not work for me but this did:

Code: Select all

#Requires AutoHotkey v2.0
g := Gui()
x := g.AddActiveX("w600 h600", "AcroPDF.PDF").value
g.show()
x.navigate(FileSelect(, , "Pick a PDF", "PDF (*.pdf)"))
14.3 & 1.3.7
User avatar
kczx3
Posts: 1649
Joined: 06 Oct 2015, 21:39

Re: Show PDF in AHK GUI

05 Jul 2023, 18:18

If navigate works then the control that AHK has created is actually Shell.Explorer. By default, if the progId isn’t found then AHK creates an instance of the Shell.Explorer control

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Descolada, Vanda_a and 51 guests