A few weeks ago we looked at creating a table of contents for a PDF containing nine individual reports. The article ended by asking a somewhat rhetorical question…
Now that we have a table of contents for our master report, wouldn’t it be nice if we could hyperlink each TOC entry to its corresponding report within the PDF?
…and today we’re going to dig into the nuts and bolts of what it takes to make this happen.
As usual, we have a demo file (Embedded PDF Links), but since it will only work for developers having licensed copies of the above-mentioned products, here is a PDF showing the end result: Report with Hyperlinked TOC.pdf
As before, we’re going to concentrate on this particular script…
…which hasn’t changed since last time except for the addition of these steps:
We’ll take a closer look some of these steps in just a minute. First, though, I want to say a little bit about PDF page linking… how it works in general, and the particular approach I’m taking here.
A PDF page link is created by specifying a set of rectangular coordinates, and, in effect, “drawing” an invisible rectangle at a certain location on a given page of a PDF, and then specifying the target page to go to (within that same PDF) when that link is clicked. In today’s article and demo this is accomplished via a function called “DynaPDF.PageLink“.
As chance would have it, a table of contents is ideally suited to this approach, because the entries are predictably located on the T.O.C. page, making it easy to programmatically determine where to place the invisible page link rectangles.
The layout dimensions can help us determine the width and height of the rectangle, as well as the starting position (top, left).
Based on the above we’re going to use these coordinates for width, height, left and top.
The first three will remain constant, but $T (top) will be incremented by $H (the rectangle height, i.e., 15) for each TOC entry, as the script works its way down the page drawing invisible page link rectangles.
As mentioned last time, the PDF will contain either eight or nine reports, so the TOC will have either eight or nine entries (and corresponding page values, which we will use as the target for our page links)… and, conveniently, our script has already loaded these values into variables, as part of the basic TOC generation process, as per these two highlighted steps.
Once the above portion of the script has completed, $totalEntries and the repeating variable, $col_2, will contain something like this.
Okay, let’s take a closer look at some of the page link script steps.
Step 63 transforms the “Save As PDF”-friendly path + filename in $$fileSpec, e.g.,
file:/C:/Users/Kevin/Desktop/report.pdf (Windows) file:/MacHD/Users/Kevin/Desktop/report.pdf (Macintosh)
…into one that DynaPDF can use, e.g.,
C:\Users\Kevin\Desktop\report.pdf (Windows) /Users/Kevin/Desktop/report.pdf (Macintosh)
The $counter variable initialized in step 65 will be used in step 80 to help extract page number values from the $col_2[repetitions] discussed above.
Step 71 begins the DynaPDF process, and as per the “DynaPDF.New” help page, there should only be one instance of this running in memory at a time (see step 90 note below).
Step 72 tells DynaPDF to begin coordinates at the top left of the page (instead of using the native PDF coordinate system, which starts at the bottom left)…
…and step 73 determines what the user will see when they click on a PDF link (roughly equivalent to the button “pressed” state in FileMaker), e.g.,
Step 79 ensures that the rectangle is invisible, i.e., does not display a border, if the user opens the PDF in Acrobat or Acrobat Reader. (The rectangle is always invisible in Preview.)
Step 80 draws the page link rectangles and assigns the target page numbers.
Step 87 is necessary on the Windows OS, but not on the Mac.
Step 90 frees up memory claimed by “DynaPDF.New” in step 71, and is not optional.
And now you know everything I know (as of Feb 2017, at any rate) about generating embedded PDF page links.
Postscript: MBS + DynaPDF Installation and Registration
On the Macintosh, I’ve chosen to install the DynaPDF library file in the FileMaker Extensions folder, along with the MBS plug-in.
On Windows, DynaPDF consists of these two files, and again, I’ve chosen to install them in the FileMaker Extensions folder along with the MBS plug-in.
The startup script in the demo file calls this script (obviously, you will need to substitute the xxxxxxxxxx-es with your registration info — and note that there are four versions of DynaPDF, and the methodology we’re looking at today requires at least the “Lite” version).
And, after running the “register” script, the Data Viewer confirms that all is well.