How do I display a PDF in LiveCode?
There are occasions when you might want to be able to display a PDF document in your LiveCode application, for example a help file. In this lesson we will look at different ways you can do this.
Launching a PDF in an external viewer
The simplest way to display a PDF from within LiveCode is to use the launch document command. This opens a document with the operating system's default program.
launch document "/Users/<username here>/Documents/mydocument.pdf"
Displaying a PDF within a LiveCode stack
But what if you don't want to display your PDF in a external window? You can use a browser widget to display PDFs within a stack.
To see how to do this we will create a new stack called "PDF Viewer", add a browser widget and a button that allows us to select the PDF file we want to view. Drag a browser widget onto your stack and resize it, add a button and name the button "Browse". Open the code editor from the toolbar and add this script (don't forget to click "Apply"):
Selecting a PDF
on mouseUp local tFile answer file "Please choose the file you would like to display" with type "PDF document|pdf|PDF" if it is not empty then put it into tFile // Allow for spaces in filenames replace " " with "%20" in tFile set the url of widget "browser" to tFile end if end mouseUp
Displaying the PDF
Switch to Run mode and try selecting a PDF file.